Home Android-Development

 Hii developer अगर आप  एक activity से दूसरे Activity को ओपन करना चाहते है तो मै  आपको इस  आर्टिकल  में बताऊंगा जैसे आप आसानी से सिख एक Activity  से दूसरे Activity कैसे जाये पूरा Explain किया है।

How to Make a Button To Open a New Activity || Android Development


मैं आपको इस आर्टिकल में दो तरीके को बताऊंगा दोनों तरीको से आप सेकंड Activity में जा सकते है ये दोने तरीके सबसे आसान है उसे करने 

  • Button Click setOnClickListener
  • On Click 

Step 1 

पहले आपको setOnClickListener  के बारे में बताएँगे 
इस कोड में आपको id  जो जरुरत होता है 

android:id="@+id/button1"

आप सबसे पहले Android Studio  को ओपन कर  ले फिर एक नई प्रोजेक्ट ओपन करे उसके  बाद  एक  Button को  Create करे  या  फिर  ये  code को  Copy करके   Jis Activity में  लगाना  होगा वहाँ  paste कर दे 



XML Code     MainActivity 

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="to an activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

Java Code  Main Activity 

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainActivity2.class);
view.getContext().startActivity(intent);
}
});

इस कोड को उसे से भी Activity में जा Sakte hai 

Step 2 

दूसरे कोड में आपको Id की जरुरत नहीं होता है 
इसमें आपको On Click  का जरुरत होता है 

Xml 

<Button
android:onClick="goToAnotherActivity"
android:text="to another activity" android:layout_width="wrap_contant"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Java 

public void goToAnotherActivity(View view) {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}


Example Project 

mainActivity 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="to an activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goToAnotherActivity"
        android:text="to another activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>


Java  code 


package com.webflix.stillexmaple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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 );

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), MainActivity2.class);
                view.getContext().startActivity(intent);}
        });
    }

    public void goToAnotherActivity(View view) {
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
}

Button To Open a New Activity
Button To Open a New Activity
 

जैसे  Screen Shot में दर्शाया है वैसा एक प्रोजेक्ट बनेगा उम्मीद है आपको पोस्ट पसंद आया होगा आपको पोस्ट  आया है तो शेयर जरूर करे 

Relate link:

कोई टिप्पणी नहीं

एक टिप्पणी भेजें

Thank You For Comment,

to Top