Button to new activity in android studio - Androidpro.in

Java 

 button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
            }
        });

Xml

 <Button
        android:layout_marginTop="250dp"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button One"

Main Activity. Java

package in.app.androidpro.buttontonewactivity;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private Button button1;
    private TextView textView1;

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

        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
            }
        });

        textView1 = findViewById(R.id.button2);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity3.class);
            }
        });
    }
}


activity main Xml.

<?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=".MainActivity"
    android:layout_gravity="center">

    <Button
        android:layout_marginTop="250dp"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button One"
        android:textAppearance="@style/ShapeAppearance.MaterialComponents.LargeComponent"
        android:textSize="25dp"
        android:textStyle="bold" />

   <TextView
       android:id="@+id/button2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
      android:text="Button Two"
      android:textStyle="bold"
      android:textSize="25dp"
      android:textAppearance="@style/ShapeAppearance.MaterialComponents.LargeComponent"
      android:layout_below="@id/button1"
      android:layout_marginTop="25dp"
      android:textAlignment="center"
      android:background="#FFEB3B"/>

</RelativeLayout>

Post a Comment

0 Comments