OSMbonusPack Bubble title doesn't appear - java

Context
I am writing an app for Android, and I use OSMBonusPack to easily display markers.
I display a marker to show the user's position, got with the phone GPS, and I add a FolderOverlay to group other markers (these indicate POIs from Wikipedia, retrieved with a custom API).
I included the bonuspack_bubble.xml layout file in my projet, because I wanted to modify it to scale the "More info" button properly.
Problem
The problem is that the POI markers are correctly added on the map (they show the icon, and when tapped, the Info bubble shows, filled with the title and description), but the first marker indicating the user's position displays an empty Info bubble.
I have tried to remove the FolderOverlay, I tried to add the starterMarker to the same overlay as the POIs, I tried to only display the starterMarker... Nothing works.
What am I doing wrong? If you need clarification on some points or code, feel free to ask!
Thanks!
Note: using osmbonuspack v5.3 (AAR), osmdroid 4.3, Android Studio 1.2.2, and testing on a Galaxy S4 with Android 4.2.2
Code:
Function in main Fragment
public void drawMap(Location location, MapView map, LocationManager locationManager, LocationListener locationListener) {
...
final GeoPoint startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
// Now we add a marker using osmBonusPack
Marker startMarker = new Marker(map);
startMarker.setPosition(startPoint);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
// We can change some properties of the marker (don't forget to refresh the map !!)
startMarker.setInfoWindow(new CustomInfoWindow(map));
startMarker.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_place));
startMarker.setTitle(getString(R.string.you_are_here));
map.invalidate();
...
// We create an Overlay Folder to store every POI, so that they are grouped in clusters
// if there are too many of them
RadiusMarkerClusterer poiMarkers = new RadiusMarkerClusterer(getActivity());
Drawable clusterIconD = ContextCompat.getDrawable(getActivity(), R.drawable.marker_cluster);
Bitmap clusterIcon = ((BitmapDrawable)clusterIconD).getBitmap();
poiMarkers.setIcon(clusterIcon);
map.getOverlays().add(poiMarkers);
// poiList is an ArrayList of custom POIs
for (POI poi:poiList) {
double mLat = poi.getLatitude();
double mLong = poi.getLongitude();
GeoPoint poiWaypoint = new GeoPoint(mLat, mLong);
Marker marker = new Marker(map);
marker.setPosition(poiWaypoint);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
marker.setRelatedObject(poi);
marker.setInfoWindow(new CustomInfoWindow(map));
marker.setTitle(poi.getName());
marker.setSnippet(poi.getSitelink());
Drawable icon = ContextCompat.getDrawable(getActivity(), R.drawable.ic_place);
marker.setIcon(icon);
poiMarkers.add(marker);
}
map.invalidate();
}
CustomInfoWindow.java
public class CustomInfoWindow extends MarkerInfoWindow {
private POI mSelectedPoi;
public CustomInfoWindow(MapView mapView) {
super(R.layout.bonuspack_bubble, mapView);
Button btn = (Button) (mView.findViewById(R.id.bubble_moreinfo));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mSelectedPoi.getSitelink() != null) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mSelectedPoi.getSitelink()));
view.getContext().startActivity(myIntent);
}
}
});
}
#Override
public void onOpen(Object item){
super.onOpen(item);
mView.findViewById(R.id.bubble_moreinfo).setVisibility(View.VISIBLE);
Marker marker = (Marker)item;
mSelectedPoi = (POI)marker.getRelatedObject();
}
}
POI.java
public class POI {
// We define every variable returned by the WikiJourney API
private double latitude;
private double longitude;
private String name;
private String sitelink;
private String type_name;
private int type_id;
private int id;
public POI(...) { }
}
_bonuspack_bubble.xml_
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/bonuspack_bubble" >
<ImageView android:id="#+id/bubble_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/bubble_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:maxEms="17"
android:layout_gravity="start"
android:layout_weight="1"
android:text="Title" />
<Button android:id="#+id/bubble_moreinfo"
android:background="#drawable/btn_moreinfo"
android:visibility="gone"
android:layout_width="25sp"
android:layout_height="25sp"
android:layout_gravity="end"
android:layout_weight="0" />
</LinearLayout>
<TextView android:id="#+id/bubble_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="12sp"
android:maxEms="17"
android:text="Description" />
<TextView android:id="#+id/bubble_subdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="10sp"
android:maxEms="17"
android:text="Address"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>

What is really surprising is that your app doesn't crash:
You set your CustomInfoWindow to the startMarker, without setting any related POI object.
But in CustomInfoWindow.onOpen, you get the related object, and use it without testing if it's not null - and if it's really a POI.
This can not work.
Either define a "StartInfoWindow" class, or ensure your CustomInfoWindow fully supports your startMarker.
A hint: share the same CustomInfoWindow object between all your POI markers. It is faster, and better for memory.

Related

setBackgroundColor is crashing the android application

I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000). However, whenever I go past 10000, the application crashes. I am not sure why and can not get around it. I saw that this problem is with background color only because it is otherwise displaying text correctly.
Here is the XML for activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="#string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="#string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Title" />
<Button
android:id="#+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="#string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/money" />
<TextView
android:id="#+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="#string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
background.setBackgroundColor(16776960);
}
}
});
}
}
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
You're finding all the views, except background(ConstraintLayout), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).
You need to find the layout first
background = findViewById(R.id.Trial);
in onCreate.
you haven't defined "background" view in the above.
to use .setBackgroundColor you need to use it with some view
try making object of layout view then change the color of that layout
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
background = findViewById(R.id.Trial);
just find layout and null pointer excpetion will removed

Show value when tapped [MPAndroidChart]

I`ve been looking for a way to enable the MPAndroidChart to only display the value(label) of data point when clicked. But It seems that I could not find it online even in the documentation.
I used the line chart and what I want is to only display the label of the certain point when clicked.
1- Enable touch in the chart
chart.setTouchEnabled(true);
2 - Create MarkerView
public class CustomMarkerView extends MarkerView {
private TextView tvContent;
public CustomMarkerView (Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
#Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("" + e.getVal()); // set the entry-value as the display text
}
#Override
public int getXOffset() {
// this will center the marker-view horizontally
return -(getWidth() / 2);
}
#Override
public int getYOffset() {
// this will cause the marker-view to be above the selected value
return -getHeight();
}
}
3 - Create the tvContent view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/markerImage" >
<TextView
android:id="#+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text=""
android:textSize="12dp"
android:textColor="#android:color/white"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
4. Set the view Marker in the chart
CustomMarkerView mv = new CustomMarkerView (Context, R.layout.custom_marker_view_layout);
chart.setMarkerView(mv);
https://github.com/PhilJay/MPAndroidChart/wiki/MarkerView
Use IMarker Interface (MarkerView has been deprecated since release 3.0.0)
1. Create a new class that implements the IMarker interface
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
#Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("" + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
#Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}}
2. set your marker to the chart
IMarker marker = new YourMarkerView();
chart.setMarker(marker);
Reference: https://github.com/PhilJay/MPAndroidChart/wiki/IMarker-Interface
Example in Kotlin for future you:
Define marker layout i.e. gold_marker_layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="6828.1465"
android:textSize="12sp"
android:textColor="#android:color/white"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Quick note: I set the initial text to 6828.1465 because that's my max-width text - watch out here.
If you don't need getXOffset and getYOffset you can assign marker like that:
lineChart.marker = object : MarkerView(context, R.layout.gold_marker_layout) {
override fun refreshContent(e: Entry, highlight: Highlight) {
(findViewById<View>(R.id.tvContent) as TextView).text = "${e.y}"
}
}
That's it, you should be good to go

Programmatically adding a TextView onto an existing XML file

My main screen displays info from user input.
Now everything on this screen is added using XML apart from "Kool" TextView on the top left corner. Now I want a way to get info from a user and add it underneath the first row of information.
I've sorted out the screen that gets user input the problem is actually getting the layout perfect. Now I added "Kool" TextView programmatically but the layout is wrong.
How can I make it so it so the "Kool" TextView is underneath the date "20/08/15". Every time I use the setHeight method it makes the TextView invisible.
Code:
public class AnalysisPage extends Activity {
Button savebutton,cancelbutton;
EditText dateinput,weightinput,repsinput;
int dd,mm,yy;
private TextView texty = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dumbbellpress);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.addinfo:
Dialog dialog = new Dialog(AnalysisPage.this);
dialog.setTitle(" Enter your STATS brah");
dialog.setContentView(R.layout.datainput);
dateinput = (EditText)dialog.findViewById(R.id.editDate);
final Calendar c = Calendar.getInstance();
dd = c.get(Calendar.DAY_OF_MONTH);
mm = c.get(Calendar.MONTH);
yy = c.get(Calendar.YEAR);
dateinput.setText(new StringBuilder().append(dd).append("/").append(mm+1).append("/").append(yy));
dialog.show();
weightinput = (EditText)dialog.findViewById(R.id.editWeight);
repsinput = (EditText)dialog.findViewById(R.id.editReps);
savebutton = (Button)dialog.findViewById(R.id.btnSave);
cancelbutton = (Button)dialog.findViewById(R.id.btnCancel);
savebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RelativeLayout views = (RelativeLayout)findViewById(R.id.xxx);
texty = new TextView(AnalysisPage.this);
texty.setTextSize(28);
texty.setText("Kool");
views.addView(texty);
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}
XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1" android:id="#+id/statslayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/xxx">
<TableRow
android:layout_width="fill_parent"
android:layout_height="100dp" android:id="#+id/tableRow3" android:layout_alignParentTop="true"
android:layout_alignParentStart="true" android:layout_marginStart="72dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dumbbell Press Stats!"
android:id="#+id/textView"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="100dp" android:id="#+id/tableRow"
android:layout_alignParentTop="true" android:layout_alignEnd="#+id/tableRow3"
android:layout_marginTop="49dp" android:layout_alignBottom="#+id/tableRow3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:layout_weight="0.22"
android:id="#+id/btnDate"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" Weight"
android:layout_weight="0.22"
android:id="#+id/txtWeight"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" Reps"
android:layout_weight="0.22"
android:id="#+id/btnReps"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_below="#+id/tableRow" android:layout_alignParentStart="true" android:id="#+id/tableRow4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="20/08/15"
android:id="#+id/textView2" android:layout_below="#+id/tableRow"
android:layout_alignStart="#+id/tableRow3"/>
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" 8"
android:id="#+id/textView7"
android:layout_marginEnd="82dp" android:layout_below="#+id/tableRow"
android:layout_alignEnd="#+id/tableRow3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="50kg"
android:id="#+id/textView6"
android:layout_marginStart="43dp"
android:layout_below="#+id/tableRow3" android:layout_toEndOf="#+id/tableRow4"/>
</RelativeLayout>
My end (dream) goal for this is by using Java I making complete perfectly formatted rows which contains the Date, Weight and Reps in their respected columns. And multiple rows of this or perfectly formatted using Java.
Image (this is in XML not in Java)
Rather than add TextView(s) programmatically, use a ListView. This will solve your problems entirely. You would use a custom adapter setAdapter() that your ListView would set as it's adapter (that provides the List it's data), and add new row elements each time you click your button that adds the new items. This would mean replacing your TableRow bit, and using a ListView in the RelativeLayout instead. Also, you could add a custom header view to the ListView to replace that header you already use (source: addHeaderView() )
Looking at your rows, it appears that they have the fields: "Date", "weight" and "reps". This would mean create a custom Java object that has those three fields, and make the the object type of your ListView adapter's elements. I can provide starter code if you REALLY need it, but it would help you best to use the documentation I provided to teach your self how to use the ListView.
Tutorial: ListView tutorial
Edit: Another bonus: ListView handles allowing the user to scroll as more, and more rows are added to this List of yours. More reason to just opt to a ListView :)

How can i make this photos dynamic weight by code?

I want that the pic will be dynamically like if I use this method 3 times it will give me 3 photos and the weight will be dynamic
public void addImageToRaw(final String text, final float rate/*, final TextView textView*/, final PhotosRatingInfo photosRatingInfo)
{
ImageView imageView = new ImageView(rawLinear1.getContext());
imageView.setImageBitmap(BitmapFactory.decodeFile(text));
//imageView.setLayoutParams();
//PhotosRatingInfo ratingInfo = new PhotosRatingInfo(rate, text);
rawLinear1.addView(imageView, 150, 150);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//MainMenuActivity.Chasttext();
RateDialog dialog = new RateDialog(text, rate/*, textView*/, photosRatingInfo);
dialog.show(manager, "");
//new RateDialog().show(manager, "");
}
});
}
In your layout.xml you can set a value for weight. You can only do this for one row (you could just create as many seperate rows as you need.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Otherwise, if you really want a grid, you would need to use a GridView, but that requires Android OS 4.3+.
An alternative to achieve this would be using a GridView. That's the best way, as you get the same solution.

Divide the Android Screen in two equal half

I have recently started working with Android. I don't have that much experience with Android. This is my first time working with Android. I need to divide the screen in two equal half and then perform few tasks.
After dividing the screen in two equal half, In second half (means bottom half) I need to make a Label named Latitude then corresponding to that a Textbox that will show the current latitude values and another label named Longitude and corresponding to that another Textbox and that will show current longitude value.
I started working on this, And I made some progress- Below is my XML file that I am using currently to divide the screen in two equal half.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000" >
<!-- Latitude Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" />
<!-- Longitude Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:password="true"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
My Question is-
After using the above XML file, I am not able to see my both label and both textbox properly on the Android Screen, Is there anything I am missing here in my XML file?
Secondly, how I can show the current latitude and longitude value in the corresponding textbox.
Any help will be appreciated as I am new to Android world.
Update:- Below is the diagram that I made in a paint, I want something Like that.
If you look at the screen, it got divided in two parts, and in the bottom half part, there are two Labels (Latitude and Longitude) and it should be like that, and infront of each label, there will be a textbox that will hold the corresponding latitude and longitude values.
Update Code:- For Current Location-
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private static final String TAG = "CurrentLocation";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latituteField = (TextView) findViewById(R.id.lat1);
longitudeField = (TextView) findViewById(R.id.lat2);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
#Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
Log.v(TAG, lat+ " "+ lng);
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
But the below code is not showing any latitude or longitude values on the corresponding text box? ANything wrong I am doing?
"After using the above XML file, I am not able to see my both label and
both textbox properly on the Android Screen, Is there anything I am
missing here in my XML file?"
It appeared, you may have had your LinearLayout tags messed up.
See if this is closer to what you are looking for:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<!-- currently empty -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Latitude Label -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Latitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:layout_weight="0.35"
android:singleLine="true"
android:text="test1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Longitude Label -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Longitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_weight="0.35"
android:password="true"
android:singleLine="true"
android:text="test2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Picture
"Secondly, how I can show the current latitude and longitude value in
the corresponding textbox."
What have you attempted?
You need to get the current lat/long.
Then just set the text on the EditText.

Categories

Resources