Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between screens?

Tags:

android

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?

like image 779
Yanshof Avatar asked Nov 03 '11 07:11

Yanshof


2 Answers

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>

Hope this will help you.

like image 198
Praveenkumar Avatar answered Nov 08 '22 19:11

Praveenkumar


You can do something like this:

import android.view.View;

/** Called when the activity is first created. */
public class YourActivity extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.your_button_id);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        Intent intent = new Intent(your_present_activity.this, target_activity.class);
        startActivity(intent);
    }
}
like image 5
Android Killer Avatar answered Nov 08 '22 19:11

Android Killer