0nyx
0nyx

Reputation: 144

Android XML layout bug

I have an XML layout that has a google map, two text views, and a button in a relative layout. The text views are below the google map with the text values "Latitude:" and "Longitude:" that get updated to "Latitude:xxx" and "Longitude"xxx" when the user moves the cursor on the map. The submit button says "Submit" and does just that, it packages lat/lon in an intent and passes it to the next activity.

This was all fine until I started messing with the layout. I noticed that because I had lazily been using the Graphical design tool in eclipse, everything had set dimensions, meaning on bigger screens it looked silly.

I added another relative layout and placed the lat/long texts and the submit button into it, and placed the new layout under the other relative layout that now only has the MapView.

My intent was to use android:layout_weight to assign the bottom relative layout around 15% or so of the screen on the botton, and the rest of the screen would be the google map.

Physically the dimensions were fine. However if I had the textviews dependant on the submit button at all (android:layout_above/below), the submit button's text would take on the value of the textview dependant on it and would get updated, while the textview would be stuck at "lat/long:" and would not get updated.

I currently have the textviews just set at the top of the relative layout and the independent submit button on the bottom and it works fine. I was just curious as to what the heck was happening.

Here is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="80" >

    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="xxx"
        android:clickable="true"
        android:enabled="true" >
    </com.google.android.maps.MapView>
</RelativeLayout>


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="20" >

    <TextView
        android:id="@+id/Latitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Latitude:" />

    <TextView
        android:id="@+id/Longitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Latitude"
        android:text="Longitude:" />

    <Button
        android:id="@+id/submitbutton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="GOTOUPLOADER"
        android:text="Submit" />

</RelativeLayout>

And here is the only (i think) relevant code that works with this xml file. Notice that I don't do anything with the button text here. It isn't even an attribute in this class. The only interaction the button has with this class is that it calls a method to start another activity when it is clicked.

    //setupmap()instantiates mapview and centers on the given location
public void setupmap()
    {
        getcoords();//setup coordinates
        lattext.setText("Latitude: "+String.valueOf(Lat));
        lontext.setText("Longitude: "+String.valueOf(Long));
        mapView = (MapView) findViewById(R.id.mapView);//instantiate mapview
        mapView.setBuiltInZoomControls(true);//able to zoom
        mcontrol = mapView.getController();//mapcontroller contains methods to manipulate mapview
        mcontrol.animateTo(point);//zoom to our geopoint
        mcontrol.setZoom(19);//zoom level 1-21. 1=world view, 21=as zoomed in as possible
        MyOverlay over = new MyOverlay();//instantiate overlay
        listofoverlays = mapView.getOverlays();//create a list of overlays
        listofoverlays.clear();//clear list
        listofoverlays.add(over);//add our overlay
        mapView.invalidate();//invalidate for changes to take effect
    }//setupmap

Please forgive all the comments.

Upvotes: 0

Views: 462

Answers (1)

jnthnjns
jnthnjns

Reputation: 8925

Okay, I am still not certain I understand your issue fully but to start trying to resolve this:

What I have done here is taken your code and placed the relational positioning back on it. Note that if you relate one object to another then you must place the object you want to reference below the object be referred to. This is due to the id not being in the generated R.java until it is declared with android:id=@+id/object. In this instance if you want to declare an object as above another then we build from the bottom up.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="80" >

        <com.google.android.maps.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="xxx"
            android:clickable="true"
            android:enabled="true" >
        </com.google.android.maps.MapView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="20" >

        <Button
            android:id="@+id/submitbutton"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Submit" />

        <TextView
            android:id="@+id/Longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_above="@id/submitbutton"
            android:text="Longitude:" />

        <TextView
            android:id="@+id/Latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/Longitude"
            android:layout_alignParentLeft="true"
            android:text="Latitude:" />
    </RelativeLayout>

</LinearLayout>

Edit:

Judging by your example, you're issue appears to be in your java. How are you calling on TextView to use the setText you show?

See example below:

TextView lattext = (TextView)findViewById(R.id.Latitude); 
lattext.setText("Latitude: " + String.valueOf(Lat));

// And

TextView lontext= (TextView)findViewById(R.id.Longitude);
// Avoid using "Long", Long is reserved... If I am unsure of whether text is reserved
// I will always place my initials in front of it... i.e. "String.valueOf(jjLong));"
lontext.setText("Latitude: " + String.valueOf(Lon));

Upvotes: 1

Related Questions