0

I have 8 buttons and each button should take you to the same activity but the title of the activity will change depending on which button you press.

The idea behind this is that I have 8 Movie screens and each button corresponds to a screen (1 through 8). so inside the onclick method i will use the proper query for that screen and set the title to that specific screen number. There are better ways to do this im sure like with a drop down list but i just want to know for what i have already.

Is there a way to set the title of the page with a @string reference or do i need to hard code the title in the button's onclick method?

3 Answers 3

2

If I understand correctly, you want to access string resources from java code instead of xml. You can do that by using the generated R class.

E.g. R.string.my_title. This will give you a resource id for that particular entry. If you want to get the string value of it, you call Context.getString(R.string.my_title).

More information on that can be found in http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode

Sign up to request clarification or add additional context in comments.

3 Comments

Cool that seems pretty easy and straight forward.
Yes but how will you know which title was passed?
Ah, I did not completely understand the question at first. wyatt, see codeMagic's answer.
2

Just pass the text with the click

public void onClick(View v)
{
    String title = ((Button).getText();
    Intent i = new Intent(MainActivity.this, NextActivitiy.class);
    i.putExtra("title", title);
    startActivity(i);
}

This is assuming that the String resource you are referring to is the text of the Button. You will have to send the title one way or another so this should work.

Then in your next Activity use something like

Intent intent = getIntent();
String title = intent.getStringExtra("title");

9 Comments

can i just use the title String to set the preferences of the Label that is my title? String titleString = getString(R.string.theater_number1); but i don't know how to set the preferences with methods from inside the class.
Now I think I am confused. You will have different titles depending on which Button is pressed, correct? And the title for the next Activity is the text of the Button pressed? That is how I'm seeing your question
yes. maybe we mean different things when i say title. The activity im calling has a textview at the top of the screen and i'd like to set that textview to "theater 1" through 8. i think what you have given me is good i just don't know how to access that specific textview and set the text.
What is the text on your Buttons? Is that what you want for your title on the next Activity? Or how do you decide what the title will be?
i answered my own question. What do you think about the solution?
|
0

figured it out the way i wanted to do it. I'm willing to change it if anyone has any other ideas. im new to android programming so i may not understand it. but this was my solution.

    public void theaterOneButtonOnClick(View v) {

    String title = ("" + R.string.theater_number1);
    Intent i = new Intent(MainActivity.this, TheaterActivity.class);
    i.putExtra("title", title);

    startActivity(i);

}

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.theater_activity);

    Intent intent = getIntent();
    String sTitle = intent.getStringExtra("title");
    TextView msgTextView = (TextView) findViewById(R.id.textView1);
    int title = Integer.parseInt(sTitle);
    msgTextView.setText(title);


}

}

4 Comments

It looks like you're setting the same title no matter which button is clicked
@codeMagic oh i see what you are saying. you are assumming that i am using the same listener for all 8 buttons. i didn't think of that. in that case i would need to get the button text like you were suggesting. im a beginner-ish.
Yes, since they all do the same thing but change a variable this would make sense
See this answer to use on listener. It will make your code more efficient and readable. I guess I didn't understand exactly how you knew which string you wanted. I assumed it was the Button text. But that answer should help you consolidate your code. Hope it helps

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.