Reputation: 70
I wan't to set TextView's text with string values I have in string.xml
so I want to do this with switch-case in java class
but it can't set text on my textView and when i run the app, the textview is empty
sorry if it's a simple stupid mistake :)
this is my layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/shape">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textColor="@color/txt_dialog"
android:textSize="25sp"/>
</RelativeLayout>
and this is string.xml
<resources>
<string name="t1"> hi </string>
<string name="t2"> hello </string>
<string name="t3"> ok </string>
</resources>
and finally this is my java codes that should choose one of those strings whith random choice and then set that in textview in dialog
public void hi (){
TextView txt_truth = (TextView) findViewById(R.id.textview);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
Random random = new Random();
int hi_random = random.nextInt(2) + 1;
switch (hi_random){
case 1:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.t1));
}
break;
case 2:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.t2));}
break;
case 3:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.t3));}
break;}
dialog.show();
}
Upvotes: 1
Views: 1715
Reputation: 3195
You have to get the id of the textview from the dialog view inflation in this way
View view = LayoutInflater.from(this).inflate(R.layout.mylayout, null);
dialog.setContentView(view);
TextView txt_truth = (TextView) view.findViewById(R.id.textview);
Upvotes: 3
Reputation: 809
On the top of my head I think the mistake is in your random generator. You have specified the upper value to be 45.
The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
int hi_random = random.nextInt(3) + 1;
Try the above code instead and comment whether it works or not.
You can also check the java doc on nextInt : nextInt javadoc
Upvotes: 0
Reputation: 132972
it can't set text on my textView and when i run the app, the textview is empty
string.xml
has only three strings which want to show in TextView randomly, but you are using 45
for getting Random number which generate number in range [0-45]+1
.
get number between 1 to 3 as:
int hi_random = random.nextInt(2) + 1;
Upvotes: 2
Reputation: 1755
Check the names of your String resources, are they a1 or t1 in series
public void hi (){
TextView txt_truth = (TextView) findViewById(R.id.textview);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
Random random = new Random();
int hi_random = random.nextInt(45) + 1;
switch (hi_random){
case 1:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a1));
}
break;
case 2:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a2));}
break;
case 3:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a3));}
break;}
dialog.show();
}
Upvotes: 0