Mohammad Fajar
Mohammad Fajar

Reputation: 1007

Load second XML layout when click a button

How do I view a second xml layout beside the main layout with click button listener. And to get some variable in that layout? Sory this beginner question, but I cannot find the similar question in stackoverflow.

Upvotes: 0

Views: 639

Answers (3)

Jorgesys
Jorgesys

Reputation: 126455

Inflate the "second" layout (second_layout.xml):

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Get the View:

View view = inflater.inflate(R.layout.second_layout, null);

Get an element inside the second_layout.xml, for example if you have a TextView with android:id="@+id/txtview_description":

TextView myTextView = (TextView)view.findViewById(R.id.txtview_description);

Upvotes: 1

Felipe Vasconcelos
Felipe Vasconcelos

Reputation: 2605

If you want to put another layout from other XML inside the one you already have, you could use LayoutInflater too.

// Get the layout inflater
LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Get the view
View view = inflater.inflate(R.layout.other_layout, null);

Link of documentation.

[]'s

Upvotes: 3

ansorod
ansorod

Reputation: 367

If you want to show another layout beside the current one, you should use Fragments. See the links below to some explanation.

http://developer.android.com/guide/components/fragments.html

http://developer.android.com/training/basics/fragments/creating.html

http://www.vogella.com/tutorials/AndroidFragments/article.html

Upvotes: 1

Related Questions