Custom Dialog With Call Back in Android
What is Dialog in Android :
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
Create Custom Layout :
If you want a custom layout in a dialog, create a layout and add it to an
Dialod by calling setContentView() on your Dialog object.
.
Dialog Methods :
Let's go to create an example in android studio.
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
/**
* Created by anupam on 15/9/17.
*/
public class CustomDialogWithCallBackButton {
private String strTag, strHeader, strButton1, strButton2;
private Context mContext;
private CallBackButtonClick clickCallBackButton;
private TextView tvHeader, tvButton1, tvButton2;
private View.OnClickListener mOnClick;
Dialog dialog;
public CustomDialogWithCallBackButton(Context mContext, String strTag,
String strHeader, String strButton1, String strButton2,
CallBackButtonClick clickCallBackButton) {
this.strTag = strTag;
this.strHeader = strHeader;
this.strButton1 = strButton1;
this.strButton2 = strButton2;
this.mContext = mContext;
this.clickCallBackButton = clickCallBackButton;
dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_with_callback_button);
initUi();
initUiListener();
attachListener();
dialog.show();
}
public void initUi() {
tvHeader = (TextView) dialog.findViewById(R.id.customDialog_tvHeader);
tvButton1 = (TextView) dialog.findViewById(R.id.customDialog_tvButton1);
tvButton2 = (TextView) dialog.findViewById(R.id.customDialog_tvButton2);
tvHeader.setText(strHeader);
/* TAG is used for hide or display button*/
if (strTag.equals("0")) {
tvButton1.setText(strButton1);
tvButton2.setVisibility(View.GONE);
} else {
tvButton1.setText(strButton1);
tvButton2.setText(strButton2);
tvButton2.setVisibility(View.VISIBLE);
}
}
public void initUiListener() {
mOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.customDialog_tvButton1:
clickCallBackButton.onButtonClick(strButton1);
dialog.dismiss();
break;
case R.id.customDialog_tvButton2:
clickCallBackButton.onButtonClick(strButton2);
dialog.dismiss();
break;
}
}
};
}
public void attachListener() {
tvButton1.setOnClickListener(mOnClick);
tvButton2.setOnClickListener(mOnClick);
}
public interface CallBackButtonClick {
public void onButtonClick(String strButtonText);
}
}
Note :
If you want to call the CustomDialogWithCallBackButton class from fragment use the bellow given code.
// for fragment calling
new CustomDialogWithCallBackButton(context, "1", "Like this page on Facebook(Apna Android) to get more info in details.", "Like", "Dislike",
Output :
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
Create Custom Layout :
If you want a custom layout in a dialog, create a layout and add it to an
AlertDialog
by calling setView()
on your AlertDialog.Builder
object or add it to Dialod by calling setContentView() on your Dialog object.
.
Dialog Methods :
- cancel() : Cancel the dialog.
- dismiss() : Dismiss the dialog. Removing it from screen .
- setContentView(int resId) : Set the screen content from layout resource.
- setCanceledOnTouchOutside(boolean cancel) : Sets whether this dialog is canceled when touched outside the window's bounds.
- setCancelable(boolean value) : Sets whether this dialog is cancelable with the
BACK
key. - requestWindowFeature(int featuredId) : Enable extended window features.
- show() : Start the dialog and display it on screen.
Let's go to create an example in android studio.
Create Project :
a) Open Android Studio
b) Go to File >New> New Project > Project Name > Next > Next > Next > Finish
Layout Design :
open activity_main.xml file and replace the given bellow code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31877B"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Apna Java"
android:textColor="#41f4ca"
android:textSize="30dp" />
<TextView
android:id="@+id/custom_tvButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Single Button"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="@+id/custom_tvButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Double Button"
android:textColor="#FFF"
android:textSize="20dp" />
</LinearLayout>
create custom_dialog_with_callback_button.xml file within layout folder and replace
the given bellow code.
<?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"
android:background="#31877B">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:background="#FFF"
android:layout_margin="2dp">
<TextView
android:id="@+id/customDialog_tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#E80E39"
android:text="Do you want to login"
android:textSize="15dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/customDialog_tvButton1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#FFF"
android:textSize="15dp"
android:background="#31877B"
android:text="Login"/>
<TextView
android:id="@+id/customDialog_tvButton2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#FFF"
android:textSize="15dp"
android:background="#31877B"
android:text="Register"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
open manifest.xml file and replace the given bellow code .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package name">
<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>
Code:
Create CustomDialogWithCallBackButton.java file and replace the given bellow code.
import android.app.Dialog;import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
/**
* Created by anupam on 15/9/17.
*/
public class CustomDialogWithCallBackButton {
private String strTag, strHeader, strButton1, strButton2;
private Context mContext;
private CallBackButtonClick clickCallBackButton;
private TextView tvHeader, tvButton1, tvButton2;
private View.OnClickListener mOnClick;
Dialog dialog;
public CustomDialogWithCallBackButton(Context mContext, String strTag,
String strHeader, String strButton1, String strButton2,
CallBackButtonClick clickCallBackButton) {
this.strTag = strTag;
this.strHeader = strHeader;
this.strButton1 = strButton1;
this.strButton2 = strButton2;
this.mContext = mContext;
this.clickCallBackButton = clickCallBackButton;
dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_with_callback_button);
initUi();
initUiListener();
attachListener();
dialog.show();
}
public void initUi() {
tvHeader = (TextView) dialog.findViewById(R.id.customDialog_tvHeader);
tvButton1 = (TextView) dialog.findViewById(R.id.customDialog_tvButton1);
tvButton2 = (TextView) dialog.findViewById(R.id.customDialog_tvButton2);
tvHeader.setText(strHeader);
/* TAG is used for hide or display button*/
if (strTag.equals("0")) {
tvButton1.setText(strButton1);
tvButton2.setVisibility(View.GONE);
} else {
tvButton1.setText(strButton1);
tvButton2.setText(strButton2);
tvButton2.setVisibility(View.VISIBLE);
}
}
public void initUiListener() {
mOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.customDialog_tvButton1:
clickCallBackButton.onButtonClick(strButton1);
dialog.dismiss();
break;
case R.id.customDialog_tvButton2:
clickCallBackButton.onButtonClick(strButton2);
dialog.dismiss();
break;
}
}
};
}
public void attachListener() {
tvButton1.setOnClickListener(mOnClick);
tvButton2.setOnClickListener(mOnClick);
}
public interface CallBackButtonClick {
public void onButtonClick(String strButtonText);
}
}
Open MainActivity.java file and replace the given bellow code.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements CustomDialogWithCallBackButton.CallBackButtonClick {
Context context;
TextView tvCallDialog,tvCallDialog2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
tvCallDialog=(TextView)findViewById(R.id.custom_tvButton);
tvCallDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CustomDialogWithCallBackButton(context, "0", "This is Apna Java tutorial blog for all Java and Android developers.", "Okay", "", MainActivity.this);
}
});
tvCallDialog2=(TextView)findViewById(R.id.custom_tvButton2);
tvCallDialog2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CustomDialogWithCallBackButton(context, "1", "Like this page on Facebook(Apna Android) to get more info in details.", "Like", "Dislike", MainActivity.this);
});
}
@Override
public void onButtonClick(String strButtonText) {
Toast.makeText(context,strButtonText,Toast.LENGTH_LONG).show();
}
}
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements CustomDialogWithCallBackButton.CallBackButtonClick {
Context context;
TextView tvCallDialog,tvCallDialog2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
tvCallDialog=(TextView)findViewById(R.id.custom_tvButton);
tvCallDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CustomDialogWithCallBackButton(context, "0", "This is Apna Java tutorial blog for all Java and Android developers.", "Okay", "", MainActivity.this);
}
});
tvCallDialog2=(TextView)findViewById(R.id.custom_tvButton2);
tvCallDialog2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CustomDialogWithCallBackButton(context, "1", "Like this page on Facebook(Apna Android) to get more info in details.", "Like", "Dislike", MainActivity.this);
}});
}
@Override
public void onButtonClick(String strButtonText) {
Toast.makeText(context,strButtonText,Toast.LENGTH_LONG).show();
}
}
Note :
If you want to call the CustomDialogWithCallBackButton class from fragment use the bellow given code.
// for fragment calling
new CustomDialogWithCallBackButton(context, "1", "Like this page on Facebook(Apna Android) to get more info in details.", "Like", "Dislike",
(CustomDialogWithCallBackButton.CallBackButtonClick) "put your fragment class name".this
);Output :
Comments
Post a Comment