Ryan Murphy :
****I am very new to Java and android studio and am trying to display an individual element of my array into my textView wordTextView with every click of a button. When I click the button on the emulator nothing is being displayed. I have also tried to use a for loop yet I cannot seem to get that to work either. Any help would be greatly appreciated****
public class MainActivity extends AppCompatActivity {
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void nextWord(View view) { //nextWord is the onclick of the button
i = i++;
}
public void getNextWord(String string) {
String [] array = {"Noun", "Adjective", "Proper Noun", "Name"};
TextView wordTextView = findViewById(R.id.wordTextView);
wordTextView.setText(array[i]);
}
}
Here is the .xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:onClick="nextWord"
tools:context=".MainActivity">
<TextView
android:id="@+id/wordTextView"
android:layout_width="155dp"
android:layout_height="153dp"
android:layout_marginTop="84dp"
android:gravity="center"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/enterEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="280dp"
android:ems="10"
android:hint="Something"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/wordTextView" />
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:onClick="nextWord"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/enterEditText"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Waqar UlHaq :
You can do something like this:
public class MainActivity extends AppCompatActivity {
int i = 0;
TextView wordTextView; // declare your textView here
// no need to create array again in "getNextWord" method
String[] array = {"Noun", "Adjective", "Proper Noun", "Name"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialise text view here
wordTextView = findViewById(R.id.wordTextView);
}
public void nextWord(View view) { //nextWord is the onclick of the button
i = i++;
getNextWord() // call this method here, without passing string
}
public void getNextWord() {
// you need to check the value of "i", it should not be greater than "array" size.
// you can simply do
// i = i% array.length
// or you can check index with if-condition
// otherwise you will get "ava.lang.ArrayIndexOutOfBoundsException" if i >= array.length
wordTextView.setText(array[i]);
}
}