Yoo
Yoo

Reputation: 1441

How to go to next layout(not activity) when pressed button?

I want to create the instruction, so when user press the next button I want to set to the next layout page. I tried setContentView(R.layout.aboutus); but it looks like to set the new layout to particular page.

Layout:

 <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="#dddddd">
        <LinearLayout android:gravity="center" android:background = "@drawable/aboutus"
            android:orientation="vertical" android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
    </LinearLayout>

code :

 public void onItemClick(AdapterView<?> parent, View view, int position,
                long lg) {
            switch (position) {

            case 4:
                setContentView(R.layout.aboutus);
            }

        }

So, I want to know how to go to next layout without new activity and can be back to previous page. Thank you.

Upvotes: 0

Views: 2302

Answers (4)

kaspermoerch
kaspermoerch

Reputation: 16570

You should use a ViewFlipper.

Get a reference to the ViewFlipper:

ViewFlipper myViewFlipper = (ViewFlipper) findViewById(R.id.my_viewFlipper);

You then inflate each page as a view:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.your_layout_id, null);

And then add it to the ViewFlipper:

myViewFlipper.addView( view );

Then when the user hits the button for next page you call this method:

myViewFlipper.showNext();

Or if you want the previous page:

myViewFlipper.showPrevious();

Upvotes: 5

K-ballo
K-ballo

Reputation: 81349

Use a ViewSwitcher or ViewFlipper.

Upvotes: 3

Uday
Uday

Reputation: 6023

May be you could do this:

If you have two different layouts and one wanna each one at one time.

First get the id's of each layout by findviewbyid method.

then use

    firstlayout.setVisibility(View.VISIBLE).
     secondlayout.setVisibility(View.GONE);

for showing first layout.

When you press button, in its click listener make

     firstlayout.setVisibility(View.GONE).
     secondlayout.setVisibility(View.VISIBLE);

it works but the layouts should be seperate.

Hope it helps.

Upvotes: 0

Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

You can try to place the same page in your layout and set all it's component to have Visibility.GONE set, and when you press the button, to set all your visible widgets to GONE and others to Visible. Or you can try a dynamically inflate procedure ( to inflate your views ) since you can use separate layout to inflate views. Check this for more information about inflate http://developer.android.com/reference/android/view/LayoutInflater.html

Upvotes: 0

Related Questions