Thursday, September 15, 2011

How to Create a new Activity.

When the first button is clicked it should take us to the second screen. Lets see how this can be done.

We need to Create the Second screen now .
Create a new java file as well as the layout .



Lets Add  a image on the second screen.

Redirect to res/drawable-hdpi and paste the image and the Xml file of the second screen should look this way.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:background="@drawable/android"
  android:layout_height="match_parent">
    
</LinearLayout>

Now we need to start a new Activity on the Button Click.

 YellAdworksActivity.java file.
onClick() needs to be changed as :
@Override
public void onClick(View v) {
if(button1==v){
Intent intent= new Intent(YellAdworksActivity.this,SecondScreen.class);
startActivity(intent);
//Toast.makeText(YellAdworksActivity.this, "You have clicked the First Button", Toast.LENGTH_SHORT).show();
}


Run as Android Application:

Wow , A Critical Bug app force closed,But very simple to fix this Bug.




Just get into the AndroidManifest.xml file and Add the activity which your trying to start.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sai.sample"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".YellAdworksActivity"
                  android:label="@string/app_name">
                  
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".SecondScreen"
                  android:label="Image"/>
</application>


</manifest>


Click on the First Button redirects to the Second Screen.


No comments:

Post a Comment