How to start or open new activity or window from one window on button click

Requirement: You want to make an "app", which displays a login screen, and users can log in. If the login is correct,  open a new window, where users can do other staff. Then how can you do this?


Ans:

Step 1. Create a layout xml for the new window. In my case I have created one
named newwindow.xml . The entry are as follows --

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9999FF" >
   </Gallery>

Step 2. Create a class with extending Activity .

Step 3. On that new class add --

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newwindow);
    }

Here newwindow is the layout xml.

Start Up window is given bellow --




















Step 4. Add the new class on AndroidManifest.xml


Note that, if you don't add the class on AndroidManifest.xml app will crash ("the app stopped unexpectedly").


Step 5. Now add the following where you want to initiate this new activity or window ---


Intent intent = new Intent(getApplicationContext(), NewWindow.class);
                    startActivity(intent);
 That's all!


Now If you add it on button click then the output will be --

You can see that the new windows background colour is "#9999FF" .


Post a Comment