Yanshof Yanshof - 23 days ago 14
Android Question

How to switch between screens?

I'm new in the android develop world.
I created simple application and created a simple GUI with one button.
If the user presses this button, I want to change the screen to show some other GUI.

How can I do this?

Answer

You Perform use this code:

public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button next = (Button) findViewById(R.id.Button01);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Activity2.class);
            startActivityForResult(myIntent, 0);
        }

    });
}
}

activity 2:

public class Activity2 extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    Button next = (Button) findViewById(R.id.Button02);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }

    });
}

Also Make Sure to Create 2 different xml in Layout folder and add both Activity in Manifest File like

<activity android:name=".Activity2"></activity>

As Shown in this example.

Hope this will help you.

Comments