Saturday, 23 July 2016

Click Button And Open New Activity in Android Studio



In this example, I will show how to start a new activity(new screen) when anyone button is clicked in
android application. Click button and open new activity firstly Create new project
Add the following code in 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="www.karantips.com.clickbutton.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open Second Activity"
        android:id="@+id/button"
        android:onClick="sahil"
        />
</RelativeLayout>
Note:- Green line is so necessary to add.


Now Create the layout Click app--------res---------layout------Right click on layout ---------Click new -------Click Layout Resource file-------Write file name second_layout and click ok
second_layout.xml
<?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:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Welcome to second Activity by www.karantips.com"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:textSize="40dp" />
</LinearLayout>



Open MainActivity.java
package www.karantips.com.clickbutton;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sahil(View view) {
        String button_text = ((Button) view).getText().toString();
        if (button_text.equals("Open Second Activity")) {
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
    }
}
Note:- Red color Sahil is your activity_main button android:onClick id. you can change any name so you can changing any name activity_main it is also necessary to change name in MainActivity.java

Click App------java-----MainActivity-------------RightClick on MainActivity------new Java Class-----Name SecondActivity
Now Write in
SeconActivity.java
package www.karantips.com.clickbutton;

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

/**
 * Created by Jai Mata Di on 7/19/2016.
 */
public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }
}


And Last step is Android Manifest.xml open in app------click Manifest------Open Your 

AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="www.karantips.com.clickbutton">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <activity android:name=".SecondActivity"/>

    </application>

</manifest>
Note:-   In Android Manifest.xml only added yellow color line No any other changing in AndroidManifest.xml
You like my post Please share with friends
You also learn my another posts of Android Studio Tutorial directly these links

No comments:

Post a Comment