5

I would like to set a Text into a TextView of my app(lication). This is the code I used:

TextView lonTextView = (TextView) findViewById(R.id.textViewLonWert);
lonTextView.setText(longitude.toString());

Actually it works, but the previous Text is still displayed! For example a 4 overlays an 8 (you cannot read the text any more).

I have a Activity starting a service. Then registering a LocalBroadcastReceiver to the Activity. The service peridically sends location objects to the activity. From the location I get e.g. the longitude and would like to update a textview (just contains the longitude) with the new longitude.

I would be very pleased if anyone has an idea to solve this problem.

UPDATE: The full code is quite long, so here are the relevant fragments:

TrackingActivity.java

public class TrackingActivity extends Activity {

// GPSTracker class
GPSTracker gps; 

public String filename;

/* Um eigene Position anzuzeigen */
ArrayList<OverlayItem> overlayItemArray;
protected MapView mapView;
protected MapController mapController;
public Boolean isPause;
public Boolean isTracking;
public MyLocationOverlay myLocationOverlay;
PathOverlay myPath;
List<GeoPoint> path;

public BroadcastReceiver screenReceiver;

/* Textviews setzen */
TextView lonTextView;
TextView latTextView;
TextView altTextView;
TextView accTextView;

// Initiating Menu XML file (menu.xml)
/*@Override
public boolean onCreateOptionsMenu(Menu menu)
{

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_tracking, menu);
    MenuItem mi_startTracking = menu.findItem(R.id.start_tracking);
    mi_startTracking.setVisible(true);
    return true;
}*/

/**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    ...
}    

@Override

public boolean onPrepareOptionsMenu(Menu menu) {
   ...
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracking);

    isTracking  = false;
    isPause     = false;

    // create GPS-class object
    gps = new GPSTracker(TrackingActivity.this
                        ,null);

    myPath  = new PathOverlay(Color.RED, this);
    path    = new ArrayList<GeoPoint>();

   /* Map anzeigen */
    mapView = new MapView(this, 500);
    mapView.setTileSource(TileSourceFactory.MAPNIK);
    org.osmdroid.views.MapView.LayoutParams mapParams = new                   org.osmdroid.views.MapView.LayoutParams(
            org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
            org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
            null, 0, 0, 0);
    /*löst Fehler aus: mapView.setClickable(true);*/
    mapView.setBuiltInZoomControls(true);
    mapController = new MapController(mapView);
    mapView.getController().setZoom(15);


    //--- Create Overlay
    overlayItemArray = new ArrayList<OverlayItem>();

    DefaultResourceProxyImpl defaultResourceProxyImpl   = new DefaultResourceProxyImpl(this);
    MyItemizedIconOverlay myItemizedIconOverlay         = new MyItemizedIconOverlay(
                                                        overlayItemArray, null, defaultResourceProxyImpl);
    mapView.getOverlays().add(myItemizedIconOverlay);
    //---

    //Add current position
    myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);

    /* Lege Pfad auf Karte */
    mapView.getOverlays().add(myPath);
    mapView.invalidate();
    LinearLayout myLayout = (LinearLayout) findViewById(R.id.trackingLinearLayoutMap);

    myLayout.addView(mapView,mapParams);

}

private BroadcastReceiver gpsDataReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        //Log.d("TrackingActivity", "in Receiver");
        /* alles was der Service liefert */
        String action       = intent.getAction();
        Double altitude     = intent.getDoubleExtra("altitude", 0);
        Double longitude    = intent.getDoubleExtra("longitude", 0);
        Double latitude     = intent.getDoubleExtra("latitude", 0);
        Bundle b            = intent.getExtras();
        Location location   = (Location)b.get("location");

        Log.d("received","Receiver: "+longitude+" ; "+latitude);


        //Log.d("TrackingActivity", "vor GeoPoint");
        GeoPoint locGeoPoint = new GeoPoint(latitude, longitude);

                /* Erstelle Pfad */
        if(isTracking){
            path.add(locGeoPoint);
            myPath.addPoint(locGeoPoint);
        }
        //Log.d("TrackingActivity", "nach GeoPoint: "+locGeoPoint.toString());
        mapController.setCenter(locGeoPoint);

        setOverlayLoc(location);

        /* Textviews Werte setzen */
        lonTextView = (TextView) findViewById(R.id.textViewLonWert);
        lonTextView.setText(longitude.toString());
        //lonTextView.invalidate();

        latTextView = (TextView) findViewById(R.id.textViewLatWert);
        latTextView.setText(latitude.toString());
        //latTextView.invalidate();

        altTextView = (TextView) findViewById(R.id.textViewAltWert);
        altTextView.setText(altitude.toString());
        //altTextView.invalidate();

        accTextView = (TextView) findViewById(R.id.textViewAccWert);
        accTextView.setText(Float.toString(location.getAccuracy())+" m");
        //accTextView.invalidate();

        /* Karte aktualisieren */
        mapView.invalidate();

    }
};
...
@Override
protected void onResume() {
    super.onResume();
    /* Registriere BroadcastReceiver für GPSDaten */
    LocalBroadcastManager.getInstance(this).registerReceiver(gpsDataReceiver, new IntentFilter("gpsdata"));

        ...

}
...

Here my layout .xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/trackingLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/trackingStatusOuterWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="kein GPS Signal"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />

    <LinearLayout
    android:id="@+id/trackingStautsInnerWrapper2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
        android:id="@+id/textViewLonLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Länge: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLonWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLatLabel"
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Breite: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLatWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />

    </LinearLayout>
    <LinearLayout
    android:id="@+id/trackingStautsInnerWrapper3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

        <TextView
        android:id="@+id/textViewAltLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Höhe: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAltWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAccLabel"
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Genauigkeit: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAccWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
    </LinearLayout>
</LinearLayout>
<!--  
<org.osmdroid.views.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:clickable="true"/>
-->

<LinearLayout
    android:id="@+id/trackingLinearLayoutMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

Hope it helps!

zuluk
  • 1,557
  • 8
  • 29
  • 49

2 Answers2

4

You'll never believe this, but if you head over to How often can you update a textview without mess you will find a question and answer which I think are addressing the issue.

If these don't resolve the issue, you may need to replace the button with a custom View with the onDraw overridden using canvas.drawText() to put the text in place. You can then be sure to wipe any previous history ...

IF you just want a non transparent black background, create (or add to) the colors.xml file in your res directory:

<resources>
    <color name="black">#ff000000</color>
</resources>

and add:

    android:background="@color/black"

To the xml for the TextView.

Community
  • 1
  • 1
Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
  • Awesome, that is the same problem. Thank you very much! – zuluk May 11 '13 at 14:46
  • That's very strange! didn't hear of it ! There is another way to check this issue. try removing all other text views inside the linear layout . maybe one of them is overlaying on the other. – Udi Oshi May 11 '13 at 14:46
  • @UdiOshi That is always worth checking, but not the case in the question I'm pointing to ... – Neil Townsend May 11 '13 at 15:12
  • @UdiOshi : I removed all TextViews except one, but the same effect occured. – zuluk May 11 '13 at 15:39
  • I realized now, that the background of the layout is black but I have never defined the backgroundcolor for this layout. Same thing with textcolor - it is white. Are black background and white text color default? Or is it possible, that there are complications with other layout.xml files where linearlayouts with these settings are defined? – zuluk May 11 '13 at 15:49
  • If you define the background to a full alpha colour (eg. black) I would expect it to work fine (as per the question I've linked to). If you want the text to have a transparent background, things are more complex. What are you trying to achieve? – Neil Townsend May 11 '13 at 15:53
  • I would like to set teh background to black (no transparency - like the code I posted in my question). – zuluk May 11 '13 at 16:22
  • Updated answer to achieve this. – Neil Townsend May 11 '13 at 16:32
  • 1
    I still think the best way to do this is dynamically load the Widget by casting its outer Containing View to a ViewGroup. See my answer here http://stackoverflow.com/questions/22362360/android-settext-on-a-textview-will-overwrite-previous-text/22362483#22362483 – kandroidj Mar 13 '14 at 18:15
  • @inner_class7 Nice solution, hence upvote, next time I face the issue I will compare the different approaches :-) – Neil Townsend Mar 14 '14 at 07:30
  • @NeilTownsend Thanks, yeah I was pretty happy with the results. This approach gives you a lot more control and you can do this with any widget.. Its just important to note that all child widgets inherit the layout params of the parent View. – kandroidj Mar 14 '14 at 16:10
  • came here to say I also had this issue, on multiple views inside a layout which got a background resource attribute removed in a commit by my team. By replacing the background again, the problem got fixed. This only seems to happen on 4.4 and below, same issue didn't occur on Lollipop'd devices – leRobot Jun 08 '16 at 16:51
-2

The line android:text="" in

    <TextView
    android:id="@+id/textViewLonWert"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="10sp" />

is redundant, you can remove that line.

Neoh
  • 15,906
  • 14
  • 66
  • 78
  • 1
    This is not an answer to the original question. It would be better as a comment instead. – Ben H Mar 14 '16 at 21:12