Thursday 18 September 2014

Opening an activity in a button click in Android

Opening an Activity in a button click in Android


                                             

In this article you are going to learn about how to open an activity using Explicit intent.

1.Open Eclipse IDE
2.Click File -> New -> Project -> Android Application Project
3.Enter the name of the application  as openactivity and click next 
4.Make sure that you have enabled the create project in workspace
5.Design application launcher icon and create a blank activity workspace and click finish 

Now you need to design your xml file ,xml file is the layout where we design our User Interface.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
tools:context="com.mobileapptechnology.openactivity.MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="150dp"
       android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/download"
            android:text="" />

    </RelativeLayout>

</RelativeLayout>
In the eclipse the activity_main.xml is present  under the res/layout/activity_main.xml.

In this layout you have to place a button as shown above, i had placed a image  as the background of the button.

After this now you have create a another xml file for the second activity.
so right click on the layout folder and click new and select android xml file ,they layout folder is present under the res folder.name the new xml as secondactivity.xml
String.xml
Normally in Android application whatever the values you are going to give will be provided in a proper manner.
Hardcore values could not be used in the application
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">OpenActivity</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="hai">Hai Friends</string>

</resources>

In  this application we set the value for the text view through the string.xml only as shown above.
the string.xml is under the values folder which is under the res folder.
Secondactivity.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="#FFFFFF"
            android:textStyle="italic"
            android:text="@string/hai" />

    </RelativeLayout>

</RelativeLayout>
In this layout i had placed a textview just to represent a text.in textview i mentioned the style ,size and color for it.and i give the gravity as center to place the text in the center of the screen.

MainActivity.java
Now its time to code the MainActivity as shown below
package com.mobileapptechnology.openactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button but1=(Button)findViewById(R.id.button1);
  TextView text=(TextView)findViewById(R.id.textView1);
  but1.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i= new Intent(getApplicationContext(),SecondActivity.class);
    startActivity(i);
   }
  });
 }

 
}
In MainActivity.java i have initialized the textview and button as shown above,after that am creating a on click event for the button to set the action for the button.

Here is used the explicit intent to open an activity from an activity when a button is clicked.

Here the getApplicationContext() refers the current class that 

means it refers the main activity.and the SecondActivity.class file 


refers the SecondActivity.java which is mentioned below.Before 


coding the MainActivity  we have to create a SecondActivity it 

means we have to create a new class by right clicking the package 

under the src folder and click the new and select the class give the 

name as SecondActivity.

SecondActivity.java
package com.mobileapptechnology.openactivity;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity{
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.secondactivity);
}
}


In this SecondActivity we are going to link the secondactivity.xml file with the SecondActivity.java here we just mentioning the layout file .It is highlighted in the above code.

After that you have mention your SecondActivity.java in Manifest file ,Because Each and every activiy which we are newly creating has to be mentioned in Manifest file .Without initializing it the application will be filled with errors.

OpenActivtyManifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobileapptechnology.openactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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="com.mobileapptechnology.openactivity.SecondActivity"></activity>
    </application>

</manifest>
The text which is in bold represent  that the SecondActivity is mentioned in the Manifest File.by default the MainActivity is mentioned in Manifest file.
Now its time to run the application,you will get the output in your emulator as shown below .


When you click the button in the UI it will redirect you the SecondActivity as shown below.
                                                                             


         

0 comments:

Post a Comment

Total Pageviews

Contact Form

Name

Email *

Message *

Mobile App Developer