1

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();
}
4
  • 1
    Obvious: txt_truth.setText(getResources().getString(R.string.t1)); but your strings are named a1, a2, ..., not t1, t2, ... Commented Jul 13, 2016 at 16:09
  • sorry about that mistake, that occurred when i was writing that, i edited now Commented Jul 13, 2016 at 16:12
  • Also, if you are basing on this: int hi_random = random.nextInt(45) + 1; chances are that you are getting a number which is out of your switch range. Commented Jul 13, 2016 at 16:13
  • so sorry :) i copied codes from large project and i tried to make it more simple, but i missed some of them Commented Jul 13, 2016 at 16:16

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@MohsenMohseni : ctx means context, you can pass this, depends where you are using
2

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;

Comments

0

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();
}

Comments

0

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

2 Comments

random.nextInt(3) + 1 this will give int as 1,2,3,4 but 4 is not in switch case
Its lower bound inclusive and upper bound exclusive. I have mentioned this in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.