How To Make Basic Component In Android Studio Part 3

By Unknown - Januari 25, 2018





Hallo People to day i want continue the last part "How To Make Basic Component In Android Studio Part 2"  and this "How To Make Basic Component In Android Studio Part 3" Lets Go πŸ‘ŠπŸ‘ŠπŸ‘Š.


In this part i will show you how to make Toast and Alert.
First start a new project.


Second open your activity_main.xml and click that text button below and make The Code like thisπŸ‘‡πŸ‘‡πŸ‘‡.
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="com.blogspot.c.appbasic.MainActivity">
    <LinearLayout        android:layout_width="0dp"        android:layout_height="0dp"        android:orientation="vertical"        android:weightSum="1"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.0">
         <Button            android:id="@+id/btntoast"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@string/btntoast" />
        <Button            android:id="@+id/btnalert"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@string/btnalert"            android:textAlignment="center" />


        <LinearLayout/>


 And the view will be like this πŸ‘‡πŸ‘‡πŸ‘‡.


Ok we go to the java Lets Go πŸ‘ŠπŸ‘ŠπŸ‘Š.

First we go to MainActivity.java.

Second we need to make the Code Like This πŸ‘‡πŸ‘‡πŸ‘‡.

public class MainActivity extends AppCompatActivity {

    private Button btntoast, btnalert,     @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Aplikasi tampil", Toast.LENGTH_LONG).show();
        inisialisasikomponen();        actionButton();
    }

    private void actionButton() {
        btntoast.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "ini toast", Toast.LENGTH_SHORT).show();            }
        });        btnalert.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this).setTitle("Show Toast")
                        .setMessage("Tampilkan Toast?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override                            public void onClick(DialogInterface dialog, int which) {
                                //perintah atau aksi setelah menekan tombol yes                                //menanmpilkan toastm                                Toast.makeText(MainActivity.this, "Tampil Toast", Toast.LENGTH_SHORT).show();                                Toast.makeText(MainActivity.this, "teks", Toast.LENGTH_SHORT).show();                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override                            public void onClick(DialogInterface dialog, int which) {
                                //abaikan / tidak ada aksi
                            }
                        })
                        .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override                            public void onClick(DialogInterface dialog, int which) {
                                //abaikan                            }
                        })
                        .show(); 
  

After that Run the app if there is a error comments that error i will be answer it.
The last word i say.

  • Share:

You Might Also Like

0 komentar