-->



5/recent-posts/slider2

404

We Are Sorry, Page Not Found

Apologies, but the page you requested could not be found.

Home Page

Tutorial Android Radio Button

RadioButton memiliki dua pilihan: baik chechked atau unchecked. ini memungkinkan pengguna untuk memilih salah satu pilihan dari set.

Berikut langkah-langkah sederhana, bagaimana untuk membuat aplikasi Android sendiri menggunakan Linear Layout dan RadioButton



Membuat Project Android

1. Pada Android Studio, buatlah project baru.
     => Jika Anda tidak memiliki sebuah project, di layar Welcome klik New Project.
     => Jika Anda telah membuka project, dari menu File, pilih New Project.

    2. Pada Configure your new project, isi kolom berikut seperti di bawah ini.
     => Application name ⇒ MyRadioButton
     => Company domain ⇒ example.com (Misalnya: com.megadistudio.MyRadioButton)
     => Project Location ⇒ memilih lokasi untuk project Anda.

    3. Pada Select the form factors your app will run oncentang untuk Phone and Tablet.
     => Untuk Minimum SDK, pilih API 15: Android 4.0.3 (IceCreamSandwich). klik Next.

    4. Pada Add an activity to Mobile, pilih Blank Activity dan klik Next.
    Next ->

    5. Klik tombol Finish untuk membuat project.
    6. Pilih View Anda sebagai Project, Buka file layout untuk kegiatan utama (activity_main.xml) terletak di bawah App  res ⇒ layout.

    Hapus TextView (Hello World) dan copy kode di bawah ini untuk membuat layout.

    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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="myradiobutton.megadistudio.com.myradiobutton.MainActivity">
    
        <TextView
            android:text="@string/tk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50sp"
            android:textStyle="bold"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/textView" />
    
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="@drawable/tk"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="19dp"
            android:id="@+id/imageView" />
    
        <TextView
            android:text=""
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:layout_weight="1"
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="17dp" />
    
        <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rg1"
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true">
    
        <RadioButton
            android:text="@string/rt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="24dp"
            android:id="@+id/radioButton1"
            android:layout_weight="1" />
    
        <RadioButton
            android:text="@string/dn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton2"
            android:layout_below="@+id/radioButton3"
            android:layout_alignLeft="@+id/radioButton3"
            android:layout_alignStart="@+id/radioButton3"
            android:layout_weight="1" />
    
            <RadioButton
            android:text="@string/lm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton3"
            android:layout_below="@+id/radioButton1"
            android:layout_alignLeft="@+id/radioButton1"
            android:layout_alignStart="@+id/radioButton1"
            android:layout_weight="1" />
    
    </RadioGroup>
    
        <Button
            android:text="Klik"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:layout_marginTop="29dp"
            android:layout_below="@+id/rg1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    
    </RelativeLayout>
    

    7.  Buka string.xml terletak di app ⇒ res ⇒ value dan copy kode di bawah ini.

    string.xml
    <resources>
        <string name="app_name">MyRadioButton</string>
        <string name="tk">Tahu Koding</string>
        <string name="rt">Roti</string>
        <string name="dn">Donat</string>
        <string name="lm">Lemper</string>
    </resources>
    

    8. Sekarang Buka MainActivity.java terletak di app ⇒ src ⇒ Java dan copy kode di bawah ini.

    MainActivity.java 
    package myradiobutton.megadistudio.com.myradiobutton;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private RadioGroup rg1;
        private RadioButton rt, dn, lm;
        private Button b1;
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            rg1 = (RadioGroup) findViewById(R.id.rg1);
    
            rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
    
                    if(checkedId == R.id.radioButton1) {
                        Toast.makeText(getApplicationContext(), "Kue : Roti", Toast.LENGTH_LONG).show();
                    }else if (checkedId == R.id.radioButton2){
                        Toast.makeText(getApplicationContext(), "Kue : Donat", Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(getApplicationContext(), "Kue : lemper", Toast.LENGTH_LONG).show();
                    }
                }
            });
    
            rt = (RadioButton) findViewById(R.id.radioButton1);
            dn = (RadioButton) findViewById(R.id.radioButton2);
            lm = (RadioButton) findViewById(R.id.radioButton3);
            tv = (TextView) findViewById(R.id.textView2);
            b1 = (Button) findViewById(R.id.button);
    
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    int selectedId = rg1.getCheckedRadioButtonId();
    
                    if(selectedId == rt.getId()){
                        tv.setText("Anda Memilih kue Roti");
                    }else if(selectedId == dn.getId()){
                        tv.setText("Anda Memilih Kue Donat");
                    }else{
                        tv.setText("Anda Memilih Kue Lemper");
                    }
    
                }
            });
        }
    }
    

    Untuk menjalankan aplikasi dari Android Studio , klik Run ikon dari toolbar.



    [full-width]
    Google+ Linked In Pin It
    No comments:

    All Rights Reserved by Tahu Koding © 2015 - 2016