How to Make a Button Open a New Activity – Android Studio Tutorial



In this video we will learn, how to open a new Activity from a Button click. For this we will create a new Intent and pass it to the startActivity method.

Example code for this video:

____________________
❗ Subscribe to the channel:

⏯ Watch more tutorials:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:

Facebook:

Instagram:

Twitter:

Blog:

Nguồn: https://indiancinema-analysis.com/

Xem thêm bài viết: https://indiancinema-analysis.com/category/cong-nghe

Related Post

28 Replies to “How to Make a Button Open a New Activity – Android Studio Tutorial”

  1. This code works, but in new versions of Android Studio some modification are needed, I will post the working code for recent version that works for me https://github.com/ambiorixg12/android

  2. I noticed that when you dragged the text at 0:57 the code to center the text was automatically added. That doesn't seem to happen for me. The text is automatically moved back to where it was after I release the left mouse button. What am I doing wrong?

  3. Thank you Coding in Flow,I am trying with a third activity but when I click to open it, the app shut down; How can i collect it in order to work with multiple activities?

  4. Why can't you create the button in the designer for the activity main, then switch to mainactivity and add the code? I'm getting an error even though my button id is "button".

  5. It's not working for me 🙁 i followed the tutorial and changed the names of activities to suit my needs etc but when i try to run i get an error
    error: incompatible types: int cannot be converted to Button

    This is my code, Please help me if you can

    MainActivity Jave
    package com.example.bfcchildrensguidenew;

    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 {

    private Button button;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    button = (Button) (R.id.faq);

    button.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    openfaq();

    }

    });

    }

    public void openfaq() {

    Intent intent = new Intent(this, faq.class);

    startActivity(intent);

    }

    }

    MainActivity 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"

    android:background="@drawable/bgapp"

    tools:context=".MainActivity">

    <ImageView

    android:layout_width="310dp"

    android:layout_height="86dp"

    android:layout_marginLeft="45dp"

    android:src="@drawable/bfcfulllogotransparent" />

    <ImageView

    android:layout_width="79dp"

    android:layout_height="76dp"

    android:layout_marginLeft="160dp"

    android:layout_marginTop="530dp"

    android:src="@drawable/bfchandtransparent"/>

    <Button

    android:id="@+id/faq"

    android:layout_width="273dp"

    android:layout_height="66dp"

    android:layout_marginLeft="65dp"

    android:layout_marginTop="95dp"

    android:background="@drawable/custom_button"

    android:text="FAQ Frequently Asked Questions"

    android:textAllCaps="false"

    android:textSize="18dp"

    android:textColor="#ffff"

    android:textStyle="bold"

    android:onClick="goTofagActivity"/>

    <Button

    android:id="@+id/complaint"

    android:layout_width="273dp"

    android:layout_height="66dp"

    android:layout_marginLeft="65dp"

    android:layout_marginTop="180dp"

    android:background="@drawable/custom_button"

    android:text="Making a complaint"

    android:textSize="18dp"

    android:textAllCaps="false"

    android:textColor="#ffff"

    android:textStyle="bold" />

    <Button

    android:id="@+id/advocacy"

    android:layout_width="273dp"

    android:layout_height="66dp"

    android:layout_marginLeft="65dp"

    android:layout_marginTop="270dp"

    android:background="@drawable/custom_button"

    android:text="Advocacy"

    android:textSize="18dp"

    android:textAllCaps="false"

    android:textColor="#ffff"

    android:textStyle="bold" />

    <Button

    android:id="@+id/socialworker"

    android:layout_width="273dp"

    android:layout_height="66dp"

    android:layout_marginLeft="65dp"

    android:layout_marginTop="360dp"

    android:background="@drawable/custom_button"

    android:text="Social Workers"

    android:textSize="18dp"

    android:textAllCaps="false"

    android:textColor="#ffff"

    android:textStyle="bold" />

    <Button

    android:id="@+id/ccfw"

    android:layout_width="273dp"

    android:layout_height="66dp"

    android:layout_marginLeft="65dp"

    android:layout_marginTop="450dp"

    android:background="@drawable/custom_button"

    android:text="Children's Commissioner for Wales"

    android:textSize="18dp"

    android:textAllCaps="false"

    android:textColor="#ffff"

    android:textStyle="bold" />

    </RelativeLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *