Cara Berpindah Activity menggunakan Button Android Studio

Pada contoh project berikut ini, misalnya kita membuat sebuah aplikasi yang menampilkan list buah – buahan, pada masing-masing item didalam list tersebut hanya menampilkan Namanya saja, saat user mengklik salah satu nama tersebut, maka akan berpindah pada activity lain yang menampilkan detail dari nama yang diklik tersebut.

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"
    tools:context=".MainActivity"
    android:background="@color/colorPrimaryDark"
    >
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="23dp"
        android:layout_marginLeft="23dp"
        android:layout_marginBottom="523dp"
        android:text="Edit Text"
        android:textColor="@color/colorPrimary"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="23dp"
        android:layout_marginLeft="23dp"
        android:layout_marginBottom="466dp"
        android:textColor="@color/colorPrimary"
        android:text="ListView" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="23dp"
        android:layout_marginLeft="23dp"
        android:layout_marginBottom="407dp"
        android:text="Radio BUtton"
        android:textColor="@color/colorPrimary" />


</RelativeLayout>

Main Activity.java

package uts.anu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
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) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openEditText();
            }
        });

        button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openListView();
            }
        });

        button = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openRadioBtn();
            }
        });

    }

    public void openEditText(){
        Intent intent = new Intent(this, EditText.class);
        startActivity(intent);

    }
    public void openListView(){
        Intent intent = new Intent(this, listpiu.class);
        startActivity(intent);

    }

    public void openRadioBtn(){
        Intent intent = new Intent(this, RadioBtn.class);
        startActivity(intent);

    }
}

ListView.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=".listpiu">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/simplelist"
        />
</RelativeLayout>

ListView.java

package uts.anu;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class listpiu extends AppCompatActivity {
            android.widget.ListView listView;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_list_view);

                listView = (android.widget.ListView)findViewById(R.id.simplelist);

                String[] myList = {"Nasi Putih",
                        "Nasi Goreng",
                        "Baso",
                        "Mie Ayam",
                        "Mie Aceh",
                        "Bakmi",
                        "Kwetia",
                        "Capcai"};

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, myList);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        // TODO Auto-generated method stub
                        switch(position){
                            case 0 :
                                Toast.makeText(listpiu.this, "Enak", Toast.LENGTH_LONG).show();
                                break;

                            case 1 :
                                Toast.makeText(listpiu.this, "Enak Poll", Toast.LENGTH_LONG).show();
                                break;

                            case 2 :
                                Toast.makeText(listpiu.this, "Enaknya Nampol", Toast.LENGTH_LONG).show();
                                break;

                            case 3 :
                                Toast.makeText(listpiu.this, "Luar Biasa", Toast.LENGTH_LONG).show();
                                break;

                            case 4 :
                                Toast.makeText(listpiu.this, "Manteb Pokoknya", Toast.LENGTH_LONG).show();
                                break;

                            case 5 :
                                Toast.makeText(listpiu.this, "Apa Lagi Ini", Toast.LENGTH_LONG).show();
                                break;

                            case 6 :
                                Toast.makeText(listpiu.this, "Ga Kenyang Kenyang", Toast.LENGTH_LONG).show();
                                break;

                            case 7 :
                                Toast.makeText(listpiu.this, "Luar Biasa", Toast.LENGTH_LONG).show();
                                break;
                        }

                    }
                });
            }
        }


Radiobtn.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=".RadioBtn">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="41dp"
        android:text="Nasi Uduk" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="87dp"
        android:text="Nasi Padang" />


</RelativeLayout>

radiobtn.java

package uts.anu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class RadioBtn extends AppCompatActivity {

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

editxt.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"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/colorAccent"
        android:text="Tugas UTS"/>
</RelativeLayout>

Diterbitkan oleh josuaabraham

Mahasiswa Aktif Di STMIK ERESHA

Tinggalkan komentar

Rancang situs seperti ini dengan WordPress.com
Mulai