using mapbox in android studio with function - android-studio

I use MapBox in android studio and show a point in map. Now I want to have a function to get a LatLng variable as an input and show that point on the map.(I want to have a function outside of onMapReady, which, with the call of the function, send points to the function as input and within the function, points appear on the map.). Please guide me
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, YOUR_MAPBOX_ACCESS_TOKEN);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(#NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});

So, Simple. Write below code in onMapReady method and Use this mapboxMap variable to add the marker point on Map.
LatLng latLng = new LatLng(20.5992, 72.9342);
mapboxMap.addMarker(new MarkerOptions().position(latLng).setTitle("set title of marker point"))

Related

How do I show the # character with this string on dialer when I click on check button? [duplicate]

This question already has answers here:
Sending ACTION_CALL Intent in Android containing hash # sign
(4 answers)
Closed last year.
My dialer is just showing "*16001":
private Button checksim;
private String number = "*16001#";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bangladeshsimverification);
checksim = findViewById(R.id.b_b_check);
checksim.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent opendialer = new Intent(Intent.ACTION_DIAL);
opendialer.setData(Uri.parse("tel:" + number));
if (opendialer.resolveActivity(getPackageManager()) != null) {
startActivity(opendialer);
}
}
});
}
I want to add '#' at the end of this string, but it is not working. How can I achieve this?
For your case, Uri.parse method cannot handle special character like '#'. A simple way around is to encode the string with proper formatting before using Uri.parse.
For example, try using:
URLEncoder.encode(value, StandardCharsets.UTF_8.toString());

'this' is not available in variables android studio

I have a class to take data from a database and every time I create a variable in the debug mode the name of the variable appears with the message 'this' is not available and I cannot save any type of data in it.
public class DatosPerfilUsuario {
RequestQueue requestQueue;
SharedPreferencias sharedPreferencias = new SharedPreferencias();
String[] StringSplit;
public void RetirarPerfilUsuario(final Context context, final ImageView imageView, final EditText nombre, final EditText nombreusuario, final EditText sexo, final EditText edad, final EditText email, final EditText bio){
StringRequest getRequest = new StringRequest(Request.Method.POST, DatosBase.CONEXION_DB_RETIRAR_PERFILUSUARIO, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(response.equals("0")){
}else {
StringSplit = response.split("/");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("NombreUsuario",sharedPreferencias.obtenerValorString(context,"Usuario").toLowerCase());
return params;
}
};
requestQueue = Volley.newRequestQueue(context);
requestQueue.add(getRequest);
}
Code
This situation happens to me with each type of variable that I create int, string, bool ...
I have different classes that do the same, but the problem only arises in this.
I have looked for a solution to the problem in google but there is very little information about this problem, if someone has the solution or understands why this problem is due I would appreciate your help.
UPDATED
It looks like this is not preventing you from compiling and running the code, only that you can't view the variable's contents in the debug window. Sorry. This is not uncommon. The debugger can't determine the this in the current context.
It looks like you may be using Android Studio? If so, check this: https://stackoverflow.com/a/37273436/12431728
OLD / WRONG
Your Response.Listener<String> is an anonymous class, so this refers to the anonymous class. Since StringSplit is a field of the outer class, you should be able to access it as:
OuterClassName.this.StringSplit = ...
Where OuterClassName is the name of the class that contains this code (and StringSplit).

AndroidPlot FixedSizeEditableXYSeries How to use

Im new to android and very new to android plot. Can you point me to an example that uses FixedSizeEditableXYSeries?
My goal is to create a streaming plot that shows the latest sensor readings in an android app.
Thanks
===================Update - following discussion with #Nick====================
public class MainActivity extends AppCompatActivity {
// Create the redrawer so that the plot is updated
private Redrawer redrawer;
// create the message receiver - data is received via broadcasts
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("CurrentHR");
Log.d("ReceivedHR ",message);
// Now put the new data point at the end of the FixedSizeEditableXYSeries, move all data points by 1.
for (int index=0;index<9;index++){
if(index<9){
hrHistory.setY(hrHistory.getY(index+1),index);
}else{
hrHistory.setY(Float.parseFloat(message),9);
}
}
}
};
// create a few references
private XYPlot xyPlot;
private FixedSizeEditableXYSeries hrHistory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heart_rate);
// Now find the plot views
xyPlot = (XYPlot)findViewById(R.id.xyPlot);
// Declare the local broadcast manager
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("hrUpdate"));
// now put in some data
hrHistory = new FixedSizeEditableXYSeries("HR",10);
xyPlot.addSeries(hrHistory, new LineAndPointFormatter(Color.GREEN,Color.RED,null,null));
xyPlot.setRangeBoundaries(40, 120, BoundaryMode.FIXED);
xyPlot.setDomainBoundaries(0, 20, BoundaryMode.FIXED);
}
#Override
protected void onResume(){
super.onResume();
// set a redraw rate of 1hz and start immediately:
redrawer = new Redrawer(xyPlot, 1, true);
}
}
This gives me a nice graph but no line. It doesnt look like the plot is being updates as new data is filling the FixedSizeEditableXYSeries.
If you want scrolling behavior then FixedSizeEditableXYSeries would be the wrong choice; as your data scrolls you're essentially enqueueing the newest value and dequeuing the oldest value; a linked list type structure would be a better choice.
You can either implement XYSeries and back it with any suitable data structure you prefer, or you can use SimpleXYSeries, which already supports queue operations a la removeFirst() and addLast(...). There's a great example of of a dynamic scrolling plot in the demo app: OrientationSensorExampleActivity. Lines 235-245 show the specific actions mentioned above.

using shared peferences to save colors

I have an app that uses a color picking dialog to change the app background color, and the text color. The color picker works fine, but when the app closes for any reason it reverts back to default.
I have checked: http://developer.android.com/guide/topics/data/data-storage.html#pref
and
Android Shared preferences example
the below code is the result of my research. The problem I'm having is all my text fields start off without text color, when i use the color dialog the colors change just fine, but are not saved.
Any pointers on where I'm going wrong would be greatly appreciated.
public class TipCalculator extends ActionBarActivity implements ColorPickerDialog.OnColorChangedListener {
//UI element objects to be manipulated.
TextView tipper;
TextView diner;
/*few TextView items left out to save space*/
int color;
RelativeLayout RLayout;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_calculator);
preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
//color = preferences.getInt("TipCalculator", color); did not change anything
color = preferences.getInt("bg_color", color);
RLayout = (RelativeLayout) findViewById(R.id.RLayout);//layout object for background color manipulation
bill = (EditText) findViewById(R.id.bill_amount_text);//UI elements placed in objects for text color manipulation
billNtip = (TextView) findViewById(R.id.bill_tip_text);
/*few TextView items left out to save space*/
bill.setSelection(bill.getText().length());
bill.addTextChangedListener(billWatcher);
}
#Override
public void onPause(){// onStop does not seem to change how the app currently runs
super.onPause();
/*preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit(); does not work*/
SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
//editor.putInt("TipCalculator", color); does not change anything
editor.putInt("bg_color", color);
editor.commit();
tipper.setTextColor(color);
bill.setTextColor(color);
/*few TextView items left out to save space*/
}
#Override
public void colorChanged(String key, int color) {
color = newColor;
if (decide.equals("font"))
{
tipper.setTextColor(color);
bill.setTextColor(color);
/*few TextView items left out to save space*/
}
else if (decide.equals("background"))
{
RLayout.setBackgroundColor(color);
}
}
Save the color to the SharedPreferences in onPause() instead of onResume(), like this:
#Override
public void onPause() {
SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
editor.putInt("bg_color", color);
editor.commit();
super.onPause();
}
onResume() is called when your app resumes, so if you exit but don't resume, the value won't get saved.
Somewhere in your initialization you will also need to read the color value out of the SharedPreferences e.g:
preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
color = preferences.getInt("bg_color", android.R.color.white); // default to white or whatever color you want
tipper.setTextColor(color);
bill.setTextColor(color);
It doesn't look like you are setting the color on the tipper and bill
objects after you load it. In onCreate() after you call getInt() add
these lines: tipper.setTextColor(color); bill.setTextColor(color); You
will need to initialize these objects first obviously. – samgak
This was the final thing I was missing.
Thanks again for the help.

Actionbarsherlock : How to "keep" it in 1 class

I'm completely new to Java and Android..
While using actionbarsherlock, I'm having a problem that I can't solve.
My app has around 8 Activities in total, and each of them has the SAME action bar,
using ABS.
So I was wondering if there is a way to keep the whole ABS part in 1 class,
and then call it in other activities when needed. Or else, I would have to write the same code in each activity to reach the same action bar, which really doesn't look correct.
I remember before using ABS, I had to use a separate row in XML then inflate it in other activities when needed. But this whole ABS project seems too vast for a newbie like me and I'm
really getting confused.. can any one help me clarify? Thanks in advance.
Use a base class which sets up the action bar.
public abstract class BaseActivity extends SherlockFragmentActivity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSupportActionBar();
ab.whatever....
}
}
And then in your activities...
public class MySweetActivity extends BaseActivity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whatever);
}
}

Resources