HelloWorld With Exit Button AndroidStudio

12,923

1

1

About: To continue learning to gain more knowledge in life and use the best of my knowledge to give contribution to the people.

This tutorial will teach you on how to build Android App that display Hello World text and Exit button to exit from the activity.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create New Project

Open Android Studio and create new project. Name your new project as HelloWorld and add Empty Activity.

Step 2: Edit the Activity_main.xml

Add a new text view and button (as shown below) inside the res>layout>activity_main.xml.

<TextView        
	android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:foregroundGravity="center"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView" />
<Button
android:id="@+id/btn_logout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:text="Exit" android:textColor="@color/black"/>

Since we using colors for the text, add a new resource in the color.xml. Go to res>values>colors.xml and add the following code

<resources>

<color name="black">#0d0c0c>

<resources/>

Step 3: Now, Edit the MainActivity.java

Add the following code to the OnCreate() method in the MainActivity.java

We add OnClickListener function to the button so whenever user clicked on the button a dialog will popup with a warning "Do you want to exit?". Two options will be provided to the user, "Yes ..." to exit and "Not ..." to close the dialog and go back to main.

Button btnlogout = (Button) findViewById(R.id.btn_logout);

btnlogout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Exit");
        builder.setMessage("Do you want to exit ??");
        builder.setPositiveButton("Yes. Exit now!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 

                finish();

            }
        });
	    builder.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 

               dialogInterface.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }
});

Step 4: Finish!

Now, you may run the app.

Good Luck!

The full code is here:https://github.com/mLynnhope/HelloWorld

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions

    0
    None

    It is amazing just how many different ways that I have seen Hello World be implemented.