Android Alert DialogFragment Tutorial
In this tutorial, we are going to create a custom AlertDialog example with custom-made buttons and icons.Previously we learned how to implement DialogFragment.
Enter project name as AlertDialogExample.
Select Android SDK Version 14.
Then create Empty MainActivity.
Next step is to create empty activity with no layout.Name it AlertDialogActivity.java
Paste below code.
AndroidManifest.xml file
Let get Started.
Create a new Project in Android Studio by Click File>New Project.Enter project name as AlertDialogExample.
Select Android SDK Version 14.
Then create Empty MainActivity.
Enter Activity name as MainActivity
MainActivity.java code
package net.techoverload.www.alertdialogexample;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity{
Button alertBt;
FragmentManager fmanager=getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertBt=(Button)findViewById(R.id.alertButton);
alertBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialogActivity alertDialogActivity=new AlertDialogActivity();
alertDialogActivity.show(fmanager,"Alert 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.techoverload.www.alertdialogexample.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/alertButton"
android:layout_centerInParent="true"
android:text="Alert Dialog"
/>
</RelativeLayout>
Next step is to create empty activity with no layout.Name it AlertDialogActivity.java
Paste below code.
package net.techoverload.www.alertdialogexample;
import android.os.Bundle;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.app.Dialog;
import android.app.AlertDialog;
/**
* A simple {@link Fragment} subclass.
*/
public class AlertDialogActivity extends DialogFragment {
public AlertDialogActivity() {
// Required empty public constructor
}
public Dialog onCreateDialog(Bundle savedInstanceState)
{return new AlertDialog.Builder(getActivity())
.setMessage("Alert Dialog Example")
.setTitle("Alert Dialog Example")
.setIcon(R.drawable.ic_launcher_background)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
}
}
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.techoverload.www.alertdialogexample" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
strings.xml
<resources>
<string name="app_name">AlertDialogExample</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>