2015/05/11

Android Button and AlertDialog Sample

This post is about Android Button and AlertDialog Sample.

MainActivity.java

package com.example.wei_hsiunghuang.helloworld;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

    Button showMsgBtn;

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

        // ** Added for the Android Button and AlertDialog sample **

        showMsgBtn = (Button) findViewById(R.id.btnShowMsg);
        showMsgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               // the following codes are from Stack Overflow
               // http://stackoverflow.com/questions/13268302/alternative-setbutton

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Alert Title")
                .setMessage("Alert Message String")
                        .setCancelable(false)
                        .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // here you can add functions
                                dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });

        // ** End of the added codes **

    }

    // The following codes are provided by Android Studio upon project creation

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Message"
        android:id="@+id/btnShowMsg"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp" />
</RelativeLayout>


- Development Environment: Android Studio 1.2.
- Other than the above 2 files, all other files of this project remain unchanged.

No comments:

Post a Comment