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();
}
txt_truth.setText(getResources().getString(R.string.t1));but your strings are nameda1, a2, ..., nott1, t2, ...int hi_random = random.nextInt(45) + 1;chances are that you are getting a number which is out of your switch range.