Android Alert DialogFragment Tutorial

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.

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>


Download Source code


Output:

Android Alert DialogFragment Tutorial


COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Android Alert DialogFragment Tutorial
Android Alert DialogFragment Tutorial
Android Alert DialogFragment Tutorial
https://i.ytimg.com/vi/_24LeXjxMKY/hqdefault.jpg
https://i.ytimg.com/vi/_24LeXjxMKY/default.jpg
Code WHIZZ
https://code-whizz.blogspot.com/2018/02/android-alert-dialogfragment-tutorial.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2018/02/android-alert-dialogfragment-tutorial.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy