Displaying a User-Defined String in an XML Layout

Put your problem here if it does not fit any of the other categories.

Displaying a User-Defined String in an XML Layout

Postby Lil_Ozzy » Sat Dec 26, 2009 7:04 am

Hey, guys,

So, I don't quite know if you can do this, but I wanted to ask, because it would make my life so much easier.

Say I have an app that has two screens, defined by two XML layouts. The first screen is called using setContentView(), then a button listener is employed. When the button is pressed, text that was entered into a text box is placed in a string, then setContentView() switches screens. The second screen would then display the text entered, using the string that was defined by the text entered in the previous screen.

Is this possible? Can an XML layout use a string that's defined on the Java side, or would I have to hardcode the layout in Java for the second screen (definitely not preferred).

Thanks,

Oz
Lil_Ozzy
Freshman
Freshman
 
Posts: 2
Joined: Sat Dec 26, 2009 6:55 am

Top

Postby hellshady00 » Sat Dec 26, 2009 7:32 am

All you have to do is create a textview in your layout, then in your code do something like

Code: Select all
TextView userString = (TextView)findViewById(R.id.userString); // Create a textview

userString.setText("Test"); // set the text to whatever the user entered


Where R.id.userString is the ID of the TextView in your XML layout

you'll need 2 textviews, you need to get text from one text view and place it into another, you can use something like
Code: Select all
userStringOne.setText( userStringTwo.getText() );

if you wanted

That might seem overly complicated, but really all you need to be doing is taking the text from one text view and placing it into another, using TextView.getText() and TextView.setText(CharSequence)
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Postby Lil_Ozzy » Sat Dec 26, 2009 8:14 am

Hah, thanks. That was a lot easier than I thought, and I feel a bit lowly for not having thought of it. But thanks, again.
Lil_Ozzy
Freshman
Freshman
 
Posts: 2
Joined: Sat Dec 26, 2009 6:55 am

Postby hellshady00 » Sat Dec 26, 2009 8:29 am

No problem, everybody has to start somewhere :)
merry christmas :)
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests