Search code examples
javaandroidandroid-studiotextviewsettext

Android: Why is subsequent code preventing .setText() for TextView from working


Update: I've discovered that code after my call to updatePercentage is preventing the .setText from working. I've updated my code below to show the call to calcAndDisplayResults from button_Convert, and the code for calcAndDisplayResults. When I remove the call to calcAndDisplayResults the .setText before this call works correctly. Why would later code prevent earlier code from working?

I'm attempting to update the text of a TextView object using .setText() to show an updated completion percentage value on the content_main layout (which is included in the activity_main layout). I'm updating the text via a private method I've written named updatePercentage which is called from the onClick method of MainActivity when various buttons are pressed. The .setText works (i.e. updates the text of the TextView) when updatePercentage is called from some locations within onClick, but not from others. Specifically, it works when entering the onClick method, works when button_3 is clicked, but doesn't work (i.e. doesn't update the text of the TextView) when button_Convert is clicked. Other methods in the button_Convert switch case (which are called after updatePercentage) work properly.

I searched the StackOverflow database and found other folks with setText questions, but none appear to address the issue I'm experiencing. My development environment is Android Studio (2.1.2), with all known relevant updates. I'm experiencing the issues using the AVD device Galaxy Nexus API 23. I'm new to Android/Java and would appreciate your assistance.

Here are the relevant portions of my code.

MainActivity.java:

...
import android.widget.TextView;
...

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
private TextView percentComplete;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    final String METHOD = "Method: onCreate";
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    percentComplete = (TextView) findViewById(R.id.textView_PercentCompleteValue);
    ...

}

...

@Override
public void onClick(View view) {
    final String METHOD = "Method: onClick";

    currentValue = valueToConvert.getText().toString();

    int progressUpdate = 0;
    updatePercentage(progressUpdate);  //This one works...text updated to 0%

    ArrayList<String> goodWords = new ArrayList<>();

    Results_Adapter resultsAdapter = new Results_Adapter(this, goodWords);
    listResults.setAdapter(resultsAdapter);

    switch (view.getId()) {

        ...

        case R.id.button_3:
            valueToConvert.setText(String.format(Locale.US, "%s%s", currentValue, "3"));

            progressUpdate = 3;
            updatePercentage(progressUpdate);  //This one works...text updated to 3%

            break;

        ...

        case R.id.button_Convert:
            progressUpdate = 20;
            updatePercentage(progressUpdate);  //This one doesn't work...text stays 0% or 3%, depending if I've clicked button_3 first

            calcAndDisplayResults();  //If I remove the call to this method, the code for updatePercentage works ????

            break;
    }
}

private void calcAndDisplayResults (){
    final String METHOD = "Method: calcAndDisplayResults";
    ArrayList<String> goodWords = new ArrayList<>();

    currentValue = valueToConvert.getText().toString();
    goodWords = convertNumbersToWords(currentValue);
    if (goodWords.size() == 0) {
        goodWords.add("<No equivalent words!>");
    }
    Results_Adapter resultsAdapter = new Results_Adapter(this, goodWords);
    listResults.setAdapter(resultsAdapter);
}

private void updatePercentage (int newPercentage){
    final String METHOD = "Method: updatePercentage";
    Log.v(LOG_CLASS, String.format(Locale.US, "%s; newPercentage: %d%%", METHOD, newPercentage));
    this.percentComplete.setText(String.format(Locale.US, "%d%%", newPercentage));
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/md_grey_200"
    tools:context="com.psmithe.combowords.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.psmithe.combowords.MainActivity"
    tools:showIn="@layout/activity_main"
    >

    <TextView
        android:id="@+id/textView_CodeToCovert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginStart="50dp"
        android:background="@drawable/textview_border"
        android:textAlignment="center"
        android:textSize="24sp"/>

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView_CodeToCovert"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_gravity="center"
            android:layout_row="0"
            android:backgroundTint="@color/colorNumberKeys"
            android:clickable="true"
            android:gravity="center_vertical|center_horizontal"
            android:maxWidth="120dp"
            android:minHeight="50dp"
            android:minWidth="90dp"
            android:onClick="onClick"
            android:text="1"
            android:textSize="12sp"/>

        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_gravity="center"
            android:layout_row="0"
            android:backgroundTint="@color/colorNumberKeys"
            android:clickable="true"
            android:maxWidth="120dp"
            android:minHeight="50dp"
            android:minWidth="90dp"
            android:onClick="onClick"
            android:text="2\n(A B C)"
            android:textSize="12sp"/>

        <Button
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_gravity="center"
            android:layout_row="0"
            android:backgroundTint="@color/colorNumberKeys"
            android:clickable="true"
            android:maxWidth="120dp"
            android:minHeight="50dp"
            android:minWidth="90dp"
            android:onClick="onClick"
            android:text="3\n(D E F)"
            android:textSize="12sp"/>

        ...

        <Button
            android:id="@+id/button_Convert"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_column="0"
            android:layout_columnSpan="100"
            android:layout_row="4"
            android:backgroundTint="@color/colorConvertKey"
            android:clickable="true"
            android:onClick="onClick"
            android:text="Convert To Words"
            android:textColor="@color/abc_primary_text_material_dark"/>

    </GridLayout>

    <LinearLayout
        android:id="@+id/linearLayout_percentComplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gridLayout"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView_percentCompleteLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Percent Complete:"/>

        <TextView
            android:id="@+id/textView_PercentCompleteValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text=""/>
    </LinearLayout>

    <ListView
        android:id="@+id/listView_EquivalentWords"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout_percentComplete"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="@color/colorListViewBackground"/>

</RelativeLayout>

Solution

  • Your setText() seems to be overriden.

    If you try posting the calcAndDisplayResults method with a delay, do you see the correct percentage value flash?