Alert Dialogs in android apps
Learn how to create alert dialogs in android apps. We can get decisions and information from user through these alert dialogs.
MainActivity.java
package com.singh.jassi.alertdialog;
import android.content.DialogInterface;
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.Toast;
public class MainActivity extends AppCompatActivity {
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
}
public void my_dialog(View view)
{
AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("Alert Dialog");
builder.setMessage("Select your option yes/no");
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int num1)
{
Toast.makeText(MainActivity.this,"You selected yes option",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int num2)
{
Toast.makeText(MainActivity.this,"You selected no option",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog =builder.create();
alertDialog.show();
}
}
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:id="@+id/activity_main"
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="com.singh.jassi.alertdialog.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of an Alert Dialog"
android:layout_marginStart="81dp"
android:layout_marginTop="43dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:text="Click me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="184dp"
android:onClick="my_dialog"
/>
</RelativeLayout>
Create Android virtual device with advance controls - click to create
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.singh.jassi.alertdialog">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Comments
Post a Comment