cnfw
cnfw

Reputation: 800

How to set a string from a previously defined string in Android?

I have the following code:

public void onClickV(View view){
 String title = "Go To...";
}

public void showDialog (View arg0) {
dialog.setTitle(this.String.title);
}

But the "this.String.title" isn't working at all. How could I set the title of the dialog depending on what the string is set to by an onClick method?

Thanks

EDIT: How would I do something similar for images?

Upvotes: 0

Views: 69

Answers (2)

user2467078
user2467078

Reputation:

Don't use harcoded String. Use a string ressource.

In strings.xml

<string name="title">Go to...</string>

In Activity.java

public void showDialog(View arg0) {
    dialog.setTitle(getString(R.string.title));
}

Upvotes: 2

Alexis C.
Alexis C.

Reputation: 93842

Declare a variable outside the function.

String title;

public void onClickV(View view){
  title = "Go To...";
}

public void showDialog (View arg0) {
dialog.setTitle(title);
}

Upvotes: 3

Related Questions