Ignigknot
Ignigknot

Reputation: 25

How to switch views via buttons in Android

I am a novice to android programming and I am trying to create a simple button that switches the view from activity_main_menu.xml to info_menu.xml on the click of a button. Here is the source and XML, respectively.

MainMenu.java

package us.wi.k12.cadottschools;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainMenu extends Activity {

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

        final Button infoButton = (Button) findViewById(R.id.InfoButton);

        infoButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v)
                    {
                        setContentView(R.layout.info_menu);
                    };
                });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
}

avtivity_main_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical"
    android:gravity="center" >
<Button android:id="@+id/CalendarButton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/calendar_button" />
<Button android:id="@+id/InfoButton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/info_button" />
<Button android:id="@+id/ContactButton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/contact_button" />
</LinearLayout>

Both run without any errors thrown, but when I click on the button InfoButton, nothing happens.

Upvotes: 0

Views: 924

Answers (6)

codeMagic
codeMagic

Reputation: 44571

Converting comment to answer

You have an extra ; after your } for the onClick(). So it should look like

        infoButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                setContentView(R.layout.info_menu);
            }  // sholdn't be one here since its closing a method
        });

Its generally not good practice to call setContentView() multiple times in one Activity. This will lead to problems because any Views in your original layout will be null. It is better to start a new Activity (or use Fragments) inside your onClick(). Then in your new Activity set the layout here (info_menu.xml) in setContentView() of your new Activity.

So then your onClick() would look something like

 infoButton.setOnClickListener(new View.OnClickListener() 
 {   
     @Override
     public void onClick(View v)
     {
           Intent i = new Intent(v.getContext(), NextActivityName.class);
           startActivity(i);
     }  
});

Intent Docs

Activity Docs

Upvotes: 2

gonjay
gonjay

Reputation: 727

You can only setContent for once within an Activity.If you want to switch views,you can try fragment: public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
    }

    private PlaceholderFragment extends Fragment() {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.info_menu, container, false);
            return v;
        }
    }
}

Upvotes: 0

brain
brain

Reputation: 17

Recommended way to switch to new view is by starting a new activity. But if you want to change existing view by clicking on button. You can use LayoutInflater.

Refer http://www.mysamplecode.com/2011/10/android-dynamic-layout-using-xml-add.html https://justanapplication.wordpress.com/tag/android-view-layoutinflater/

Upvotes: 0

Zixia
Zixia

Reputation: 21

Though setcontentView() can be called multiple times, it is not recommended.

Start a new activity or use ViewFlipper or ViewSwitcher are better solutions.

BTW, if setcontentview() is called again, all objects should be find again by using view.findviewbyid().

Upvotes: 1

afpro
afpro

Reputation: 1103

Fragment and ViewPager may be what you want. and you could use an FrameLayout and multi *Layout in it, then change visibility (not really change the view, but no different to users)

Upvotes: 0

Daiwik Daarun
Daiwik Daarun

Reputation: 3974

This link should be helpful if you are okay with starting a new activity to swap the views: http://developer.android.com/training/basics/firstapp/starting-activity.html

If you are trying to swap the views without starting a new activity, combine both your layout xml files into a single file. Then set the visibility of the view you want set before button press to View.VISIBLE and the view you don't want to see before button press to View.GONE. Then once you press the button, set the view you want to see to View.VISIBLE and the one you don't want to see to View.GONE.

If you want a code sample let me know!

Upvotes: 0

Related Questions