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
Latitudethen corresponding to that aTextboxthat will show the current latitude values and another label namedLongitudeand corresponding to that anotherTextboxand 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 labelandboth textboxproperly 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?
It appeared, you may have had your
LinearLayouttags messed up.See if this is closer to what you are looking for:
Picture
What have you attempted?
You need to get the current lat/long.
Then just set the text on the
EditText.