Android AlertDialog Example

Giving information to the user, either to continue the process or to cancel.We can get AlertDialog through AlertDialog.Builder(Context).

We can set AlertDialog title through AlertDialog.setTitle("AlertDialog Sample") and set message through AlertDialog.setMessage("Do you want to Hide/Show Button");

Full code

package com.etr.alertdialogexample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
Button hide, action;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hide = (Button)findViewById(R.id.button1);
action = (Button)findViewById(R.id.button2);
action.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("AlertDialog Sample");
dialog.setMessage("Do you want to Hide/Show Button");
dialog.setPositiveButton("Ok", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(action.getText().toString().equals("Hide")){
action.setText("Show");
hide.setVisibility(View.GONE);
}else if(action.getText().toString().equals("Show")){
action.setText("Hide");
hide.setVisibility(View.VISIBLE);
}
dialog.cancel();
}
});
dialog.setNegativeButton("Cancel", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
dialog.show();
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}

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

}

Layout design

<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"
    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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Button Hide" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Hide" />

</RelativeLayout>

Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService