Chad Chad -4 years ago 90
Android Question

Android newbie, button text not changing

I'm trying to teach myself android and I'm completely stuck.
I've got an array of 9 buttons, whose default text is "button", and a TextView whose default text is "Hello World!".

I can get the TextView's text to change to Lorem Ipsum,
but I can't get the button array text to change to "Btn something"

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
rv = new RemoteViews(getPackageName(), R.layout.activity_main);
int i = 0;
bArr[i++] = (Button) findViewById(R.id.button1);
bArr[i++] = (Button) findViewById(R.id.button2);
bArr[i++] = (Button) findViewById(R.id.button3);
bArr[i++] = (Button) findViewById(R.id.button4);
bArr[i++] = (Button) findViewById(R.id.button5);
bArr[i++] = (Button) findViewById(R.id.button6);
bArr[i++] = (Button) findViewById(R.id.button7);
bArr[i++] = (Button) findViewById(R.id.button8);
bArr[i] = (Button) findViewById(R.id.button9);

for(i = 0; i < 0; i++) {
bArr[i].setText("Btn " + i); //Doesn't Work
rv.setTextViewText(bArr[i].getId(), "Btn " + i); // Doesn't work
bArr[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Btn pressed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

((TextView)findViewById(R.id.text1)).setText("Lorem Ipsum"); // Works
}


And the XML file:

<android.support.constraint.ConstraintLayout
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:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="edu.csub.cs.cenri3390.MainActivity"
tools:showIn="@layout/activity_main"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/button4"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="@+id/button4"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/button4" />
<!-- 8 more buttons.../>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="91dp"
app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>


The buttons are out of order but that shouldn't matter, I hope. They're placed in the right spots, anyhow.

Answer Source

Your for loop is instantiated improperly here, it will never execute. You've set i=0 and tell the loop to execute so long as i<0, which of course is never true.

for(i = 0; i < 0; i++) {
    bArr[i].setText("Btn " + i); //Doesn't Work
    rv.setTextViewText(bArr[i].getId(), "Btn " + i); // Doesn't work
    bArr[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Btn pressed", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

You can just do for(i=0;i<9;i++) if you know you always have 9 buttons.

Also, just a tip you can iterate the buttons using layout.getChildCount() and use layout.getChildAt(i) to access them individually. So instead of doing

rv = new RemoteViews(getPackageName(), R.layout.activity_main);
int i = 0;
bArr[i++] = (Button) findViewById(R.id.button1);
...
bArr[i] = (Button) findViewById(R.id.button9);

You're able to do

for(i=0;i<layout.getChildCount();i++) {
    // Do something
    layout.getChildAt(i).setText('something');
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download