Android views change on click

17,776

Solution 1

Intent myIntent = new Intent(view.getContext(), YourNewClass.class); /** Class name here */
startActivityForResult(myIntent, 0);

It might crash because in your android manifest file also have to declare activity like that:

<activity android:name="YourNewClass" android:label="YourNewClass"></activity>

Or use the built in grahpical thing. (Android manifest -> Application -> Application Nodes -> Add -> Activity)

Solution 2

In Your XML, create a file for each view (makes it easier to work with), then in your main:

<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/vf">

    <include android:id="@+id/firstView" layout="@layout/first" />
    <include android:id="@+id/secondView" layout="@layout/second" />
    <include android:id="@+id/thirdView" layout="@layout/third" />
    <include android:id="@+id/fourthView" layout="@layout/fourth" />

</ViewFlipper>

in your java code:

ViewFlipper vf;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vf = (ViewFlipper) findViewById(R.id.vf);

when you want to use the next view use vf.showNext();

Share:
17,776
Gal Blank
Author by

Gal Blank

I'm a professional iOs &amp; Android developer with proven record of published apps for mass market as well for Enterprise on both platforms. Mass market iOs apps Enterprise iOs apps Android apps

Updated on June 13, 2022

Comments

  • Gal Blank
    Gal Blank almost 2 years

    I need something simple, an ability to switch views in my app like I do in iPhone, for example I load first screen with button, click on this button and go next screen with an ability to go back ( We all see Back button in the iPhone UI ). I've been trying to do startActivity(this,MySecondScreen.class) but it crashes.

    public class mainClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void login_Click(View v) {
        // Perform action on click
        try{
        Intent i = new Intent(this, MainMenuTabs.class);
        startActivity(i);
        }
        catch(Exception ex)
        {
            Log.e("main",ex.toString());
        }
    }
    

    }

    and my second class is this which is a TabActivity extender public class pissedoff extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenuview);
    }
    
    public void login_Click(View v) {
        // Perform action on click
        try{
        Intent i = new Intent(this, MainMenuTabs.class);
        startActivity(i);
        }
        catch(Exception ex)
        {
            Log.e("main",ex.toString());
        }
    }
    

    }