Apr 5, 2021

Button On click Listener In Activity - Androidpro.in

When I press the button to go into another Activity. I'm using a Activities and there's a button in that Activity code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.

Xml.activity

 <Button
                android:id="@+id/Button1"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Present Indefinite Tense" android:textColor="#ffffff" android:textSize="18sp" android:textStyle="bold" />

Button Activity Class

 Button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

(Button To New Activity) Open Another Activity Click On Button In Activity

Step 1. Create A project

Step 2.  Paste Code In Xml File Where You Want To Show Button Which Code Provide Below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/bbg"
                android:gravity="center"
                android:text="Present Tense"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="#FBF8F8"
                android:textSize="28sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/presentinde"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Present Indefinite Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/presnt_count"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Present Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/present_perf"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Present Perfect Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/present_perf_counti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Present Perfect Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />


            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@drawable/bgforttt"
                android:gravity="center"
                android:text="Past Tense"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="#0A0A0B"
                android:textSize="28sp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/pastinde"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Past Indefinite Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/past_counti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Past Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/past_perf"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Past Perfect Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/past_perf_counti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Past Perfect Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>


            <TextView
                android:background="@drawable/bbg"
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="Future Tense"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="#ffffff"
                android:textSize="28sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/future_indefinite"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Future Indefinite Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/future_counti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Future Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/future_perf"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Future Perfect Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/future_perf_counti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Future Perfect Countinous Tense"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold" />


            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@drawable/bgforttt"
                android:gravity="center"
                android:text="Use Of Verbs"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="#050505"
                android:textSize="28sp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/useisamare"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Is/Am/Are"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usehashave"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Has/Have"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/usehashavehavd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Has/Have/Had"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usewaswere"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Was/Were"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/useshallwill"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Shall/Will"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/useshallbewillbe"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Shall be / Will be"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usewillhave"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Will Have"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/may"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of May"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usemight"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Might"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usemayhavemighthave"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of May Have/ Might Have"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usecan"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Can"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usecould"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Could"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/usewillbeablebe"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Will be / Able To"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/useshouldoughtto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Should And Ought To"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/usewould"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Would "
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/usemust"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Must"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/useneed"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Need"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/usedare"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Dare"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/useusedto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Use Of Used To"
                android:textColor="#ffffff"
                android:textSize="18sp"
                android:textStyle="bold"/>



        </LinearLayout>

    </ScrollView>

</RelativeLayout>

Step 3.  Create New Activity As You Want To Open Click On Button

Step 4.  Paste Java Code In Java File Which Code Provide Below


package in.example.englishtenseshindimebyandroidpro;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {
    Button button, button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12;
    Intent intent, intent13, intent1, intent2, intent3, intent4, intent5, intent6, intent7, intent8, intent9, intent10;

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



        Button button = (Button) findViewById(R.id.presentinde);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present1Activity.class));
            }
        });

        Button Button1 = (Button) findViewById(R.id.presnt_count);

        Button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button2 = (Button) findViewById(R.id.presnt_count);

        Button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button3 = (Button) findViewById(R.id.present_perf);

        Button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button4 = (Button) findViewById(R.id.present_perf_counti);

        Button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button5 = (Button) findViewById(R.id.pastinde);

        Button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });
        Button Button6 = (Button) findViewById(R.id.past_counti);

        Button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button7 = (Button) findViewById(R.id.past_perf);

        Button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button8 = (Button) findViewById(R.id.past_perf_counti);

        Button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });
        Button Button9 = (Button) findViewById(R.id.future_indefinite);

        Button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });
        Button Button10 = (Button) findViewById(R.id.future_counti);

        Button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });
        Button Button11 = (Button) findViewById(R.id.future_perf);

        Button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });

        Button Button12 = (Button) findViewById(R.id.future_perf_counti);

        Button12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this, Present2Activity.class));
            }
        });
    }


}


Whatsapp Button works on Mobile Device only