tensa11 tensa11 - 20 days ago 6
Java Question

TextView not updating

I have a silly problem updating text in a

TextView
using a handler class. I'm setting custom Info window in Google Maps.

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}

@Override
public View getInfoContents(Marker marker) {

View v = getLayoutInflater().inflate(R.layout.my_layout, null);
TextView name = (TextView) v.findViewById(R.id.name);


final TextView status = (TextView) v.findViewById(R.id.status);

for(MyItem mi : myItems){
if(mi.getName().equals(marker.getTitle())){

name.setText(mi.getName()); //This one updates normaly

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//this one sets the text, but doesnt update on UI
status.setText("Text");

//Here I call my update text function
updateTv(status, "Text");

}
}, 5000);
break;
}
}

return v;
}
});
}


TextView
"name" is updating normally, while "status"
TextView
is only updating its content and not updating visually on the screen. I've tried invalidating,
runOnUiThread()
, but nothing seems to work. I know the solution might be really obvious, but I don't see it.

Thank you for your help in advance!

EDIT

Here's how i tried runOnUiThread().

public void updateTv(final TextView tv, final String s){

runOnUiThread(new Runnable() {
@Override
public void run() {

tv.setText(s);

tv.invalidate();

}
});
}


My XML:

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/name"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:padding="10dp"/>


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="@+id/status"
android:gravity="center_horizontal"/>

</LinearLayout>

Answer

So you are trying to update the view after it returned from getInfoContents() and that won't work as its not a live view, see this.

Something that might work is calling:

status.invalidate();
Comments