Screenshots :
1) Alert Dialog
2) List Dialog
3) Progress Dialog
4) Custom Dialog
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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="com.chantisandroid.exampleapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alert dialog"
android:id="@+id/button"
android:layout_marginTop="44dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="list dialog"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress dialog"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom dialog"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Chantisandroid"
android:id="@+id/textView"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />
</RelativeLayout>
custom.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Delete"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="are you sure"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="no"
android:id="@+id/no"
android:layout_below="@+id/textView3"
android:layout_toLeftOf="@+id/textView2"
android:layout_toStartOf="@+id/textView2"
android:layout_marginTop="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yes"
android:id="@+id/yes"
android:layout_below="@+id/textView3"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_marginTop="5dp" />
</RelativeLayout>
MainActivity.java :
package com.chantisandroid.exampleapp;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button,button2,button3,button4;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete");
builder.setMessage("Are you sure?");
AlertDialog.Builder yes1 = builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("no", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
break;
case R.id.button2:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
CharSequence[] items = {"Red", "Green", "Blue"};
builder1.setTitle("Chose Color..");
builder1.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
textView.setTextColor(Color.RED);
break;
case 1:
textView.setTextColor(Color.GREEN);
break;
case 2:
textView.setTextColor(Color.BLUE);
break;
}
}
});
AlertDialog alert = builder1.create();
alert.show();
break;
case R.id.button3:
final ProgressDialog ringProgressDialog = ProgressDialog.show(this, "Please wait ...", "Downloading Image ...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
break;
case R.id.button4:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
Button yes = (Button) dialog.findViewById(R.id.yes);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button no = (Button) dialog.findViewById(R.id.no);
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
break;
}
}
}
No comments:
Post a Comment