custom dialog wait for response - android

I created a custom dialog, to create one standard dialog that i could create in just one line later one and it would be standard. With the parameters I change the text. The dialog is very simplistic and has only one button.
Now I am rethinking of this was a good idea. I actually want my app to stop until my dialog is displaying. How could i manage that? Give the dialog a return type? Or is there a better way?
my dialog:
/**
* custom dialog
*
* #param mcontext use activityname.this
* #param title
* #param text
* #param button
*/
public void showDialog(Context mcontext, String title,String text, String button) {
// fonts
Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf");
Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf");
Resources res = mcontext.getResources();
// custom dialog
final Dialog dialog = new Dialog(mcontext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title
dialog.setContentView(R.layout.view_dialog);
TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
tv_dialog_title.setText(title);
tv_dialog_title.setTypeface(tf_hn_bold);
tv_dialog_title.setTextColor(res.getColor(R.color.white));
TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text);
tv_dialog_text.setText(text);
tv_dialog_text.setTypeface(tf_hn);
tv_dialog_text.setTextColor(res.getColor(R.color.white));
Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button);
dialogButton.setTypeface(tf_hn_bold);
dialogButton.setText(button);
dialogButton.setTextColor(res.getColor(R.color.white));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
and then i can use it like this:
dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button));
It worked all fine, until i wanted to show a dialog with "you are logged in" (or so) and then start a intent after the display was clicked away. Anyone an idea?

Create a custom dialog class with a built in listener like so.
public class MyDialog extends Dialog {
String title;
String text;
String button;
DialogListener listener;
interface DialogListener {
void onCompleted();
void onCanceled();
}
public MyDialog(Context context, String title, String text, String button) {
super(context);
this.title = title;
this.text = text;
this.button = button;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf");
Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf");
Resources res = getContext().getResources();
requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title
setContentView(R.layout.view_dialog);
TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
tv_dialog_title.setText(title);
tv_dialog_title.setTypeface(tf_hn_bold);
tv_dialog_title.setTextColor(res.getColor(R.color.white));
TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text);
tv_dialog_text.setText(text);
tv_dialog_text.setTypeface(tf_hn);
tv_dialog_text.setTextColor(res.getColor(R.color.white));
Button dialogButton = (Button) findViewById(R.id.bt_dialog_button);
dialogButton.setTypeface(tf_hn_bold);
dialogButton.setText(button);
dialogButton.setTextColor(res.getColor(R.color.white));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null)
listener.onCompleted();
MyDialog.this.dismiss();
}
});
setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
if(listener != null)
listener.onCanceled();
}
});
} public void setDialogListener(DialogListener listener) {
this.listener = listener;
}
}
And to implement the dialog:
MyDialog dialog = new MyDialog(getContext(), title, text, button);
dialog.setDialogListener(new MyDialog.DialogListener() {
#Override
public void onCompleted() {
// do stuff when dialog is completed
}
#Override
public void onCanceled() {
// do stuff when dialog is cancelled
}
});
dialog.show();

Related

How to stop Activity attached AlertDialog keep reappearing over Activity after Activity paused and recreated?

I am working on project, which simply validates through username and password.
I made some progress with using DialogFragments and AlertDialog. AlertDialog appears after starting the app over the mainactivity asking for username and password.
I must set the Alertdialog's setCanceledOnTouchOutside(false) and DialogFragment's setCancelable(false) because I don't want the users to dismiss it with pressing android's back button.
The problem is, after dismissing it programatically on successful login, if the activity becomes invisible and visible again , the Alertdialog's OnShowListener called, showing this AlertDialog again.
Can I somehow "detach" this AlertDialog from Activity? This popups also happen after unlocking the screen and getting back to activity which makes it very annoying...
Here is the code of interest:
MainActivity
public class MainActivity extends AppCompatActivity implements NoticeDialogFragment.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(GlobalInformations.getInstance().getUsername()==null){
shownoticeDialog();
}
}
public void shownoticeDialog(){
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
}
#Override
public void onDismiss(DialogFragment dialog) {
//set the username on a TextView instance, etc...
}
NoticeDialogFragment extends DialogFragment
public class NoticeDialogFragment extends DialogFragment {
public interface NoticeDialogListener{
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
public void onDismiss(DialogFragment dialog);
}
NoticeDialogListener mListener;
static Activity activity = null;
//static String username;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activity = (Activity) context;
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e){
throw new ClassCastException(activity.toString() + "must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
final AutoCompleteTextView actv_username = (AutoCompleteTextView) view.findViewById(R.id.username);
final EditText password = (EditText) view.findViewById(R.id.password);
getavailableusernames(actv_username);
final AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(getContext(), R.style.AlertDialogCustom))
.setView(view)
.setTitle("Login")
.setPositiveButton("OK", null)
//.setNegativeButton("Cancel", null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
final Button button =((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String passw = password.getText().toString();
String user = actv_username.getText().toString();
try{
if(user.length()<4 || passw.length()<4){
Toast.makeText(getContext(), "Username/password too short", Toast.LENGTH_SHORT).show();
dialog.show();
}
else {
//login to account, if success dismiss.
login(user, passw,dialog);
}
} catch(Exception e){
}
// dialog.dismiss();
}
});
}
});
dialog.setCanceledOnTouchOutside(false);
// set the DialogFragment to make the dialog unable to dismiss with back button
// (because not working if called on the dialog directly)
this.setCancelable(false);
return dialog;
}
public void login(final String username, String password, final AlertDialog dialog){
boolean login_success = false;
//query the credentials
login_success = dosomesqlquery(username, password);
if(login_success){
dialog.dismiss();
}
}
//passing the handling to activity...
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
}
}
Thank you for your help and patience.
Well this is that kind of situation where I end up heading my desk continously.
The source of the problem was I called dialog.dismiss() which dismisses the dialog, BUT not the dialogfragment itself, so will never, ever dismissed, even if the dialog disappeared from screen. Placing this.dismiss() in NoticeDialogFragment's onDismiss or anywhere else after login succeded will let the application act as it should.
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
this.dismiss(); //will dismiss the DialogFragment. Yeeey!
}
Thank you for your time and answers as they helped me point out the real problem. I will modify the code based on your suggestions.
An easier way is to use a static variable in your activity using two steps.
Declare a global static boolean
private static boolean session = false;
Check if the boolean has changed and if not, set the boolean to true when the dialog is shown
public void shownoticeDialog(){
if(session)return;
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
session = true;
}
Set the value when the activity goes background
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("authUser", GlobalInformations.getInstance().getUsername()==null)
}
and read it when it comes back
#Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null && savedInstanceState.containsKey("authUser")) {
boolean authUser = savedInstanceState.getBoolean("authUser", false);
if(authUser) {
//show or don't show dialog
}
}
}

How to create a popup when button is pushed in Android?

How can I create a custom popup class that accepts a simple string message? Im new to Android and help with code will be appreciated.
When a button is pushed in the main layout, the popup must pop up on the screen.
Custom popup class
public class CustomPopup extends PopupWindow {
private String message;
private Double anchorX;
private Double anchorY;
PopupWindow popup;
public CustomPopup(String message) {
super();
this.message = message;
}
public void showPopup(Activity context) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
Main Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText messageTxt = (EditText) findViewById(R.id.messageTxt);
Button generateBtn = (Button) findViewById(R.id.generateBtn);
String message = messageTxt.getText().toString();
final CustomPopup popup = new CustomPopup(message);
generateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popup.showPopup();
}
});
}
}
You can change the following code any way you need. This is just an example of how you make and implement a custom DialogFragment.
He is the code I use. I find it quite flexible because you can create several similar dialogs for slightly different tasks. You will need to create a layout file - this gives you a great deal of flexibility on function and style.
My layout file is fragment_ok_cancel_dialog.
To satisfy your requirements just create your own layout file with all the elements in it you need (like your image).
In the Activity that calls the dialog you will need to implement the Listener.
implements OkCancelDialogFragment.OkCancelDialogListener
Another advantage is with my code you can change the title and the message to fit the needs of any Activity.
private void callMyDialog(){
//Customize the title and message as needed
String title = "This is my dialog title";
String mess = "This is my dialog message";
OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
dialog.show(getFragmentManager(), "OkCancelDialogFragment2");
}
Now you need to implement the dialog callback in the Activity that calls the DialogFragment.
#Override
public void onFinishOkCancelDialog(boolean submit) {
if(submit){
// Do something positive
}
else{
// Do something negative
}
}
Now the code for the DialogFragment:
public class OkCancelDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
Context context = null;
private String title;
private String message;
private boolean submitData = false;
private OkCancelDialogListener mListener;
public OkCancelDialogFragment() {
}
public static OkCancelDialogFragment newInstance(String title, String message) {
OkCancelDialogFragment fragment = new OkCancelDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
message = getArguments().getString(ARG_MESSAGE);
}
}
#Override
public Dialog onCreateDialog(Bundle saveIntsanceState){
context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false);
final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);
titleView.setText(title);
messView.setText(message);
builder.setView(rootView)
// .setTitle(title)
.setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
submitData = true;
if(mListener == null) mListener = (OkCancelDialogListener) context;
mListener.onFinishOkCancelDialog(submitData);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
submitData = false;
if(mListener == null) mListener = (OkCancelDialogListener) context;
mListener.onFinishOkCancelDialog(submitData);
}
});
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if(mListener == null) mListener = (OkCancelDialogListener) context;
}
catch (Exception ex){
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OkCancelDialogListener {
void onFinishOkCancelDialog(boolean submit);
}
}
Please note that .setTitle(title) is valid for API 23 or higher (or maybe API 21 or higher?).
You can create your custom xml layout
and in the OnClickListener of the button you can put this :
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
alertLayout = inflater.inflate(R.layout.YOUR_CUSTOM_POPUP_LAYOUT, null);
final AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setView(alertLayout);
TextView msg= alertLayout.findViewById(R.id.YOUR_TEXTVIEW_ID);
alert.show();
after that you can add another button in your popup and set a listener on it to dismiss the layout after the click.

How to re-size dialog with animation

I have created a custom dialog class (extends Dialog) , inside that dialog xml layout
i have expand/ collapse textview with animation, this is working fine,
but when i expand or collapse the view (of the textview) the size of the dialog also change (with out animation), how can make it also re-size with animation?
public class ErrorDialog extends Dialog {
private String err;
private TextView txt_help_gest;
public ErrorDialogOnClickListener errorDialogListener;
private String shortDesc;
private Animation slide_up_Animation;
private Animation slide_down_Animation;
private Animation rotate_arrow_down;
private Animation rotate_arrow_up;
public ErrorDialog(Context context,String shortDesc, String err, ErrorDialogOnClickListener errorDialogListener) {
super(context);
this.err=err;
this.errorDialogListener = errorDialogListener;
this.shortDesc=shortDesc;
slide_up_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_up);
slide_down_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_down);
rotate_arrow_down= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_down);
rotate_arrow_up= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_up);
getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;
}
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_error);
setTitle("שגיאה");
setCancelable(false);
// set values for custom dialog components - text, image and button
Button btnClose=(Button)findViewById(R.id.btnClose);
TextView text = (TextView) findViewById(R.id.textDialog);
txt_help_gest = (TextView) findViewById(R.id.txt_help_gest);
//help_title_gest = (TextView) findViewById(R.id.help_title_gest);
final ImageView imgArrow = (ImageView) findViewById(R.id.imgArrow);
LinearLayout ll_help_title = (LinearLayout) findViewById(R.id.ll_help_title);
// ll = (View) findViewById(R.id.ll);
text.setText(shortDesc+"\n התוכנית תסגר.");
txt_help_gest.setText(err);
txt_help_gest.setVisibility(View.GONE);
ll_help_title.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
//txt_help_gest.clearAnimation();
if(txt_help_gest.isShown()){
//collapse();
//slide_up_Animation.reset();
txt_help_gest.startAnimation(slide_up_Animation);
txt_help_gest.setVisibility(View.GONE);
imgArrow.startAnimation(rotate_arrow_up);
}
else{
// expand();
// slide_down_Animation.reset();
txt_help_gest.setVisibility(View.VISIBLE);
txt_help_gest.startAnimation(slide_down_Animation);
imgArrow.startAnimation(rotate_arrow_down);
}
}
});
btnClose.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
errorDialogListener.onButtonClick();
dismiss();
}
});
}
public interface ErrorDialogOnClickListener {
void onButtonClick();
}
}

Changing button text from AlertDialog

i am new to developing apps for android and i want to create a simple Conterter app, just for the start. In my view i have a edittext and a Button. If i click the button, it will open a AlertDialog with list of strings. I cant figure out how to manage this: When i click on one item in the AlertView i want to set the text of the button to the selected string and dismiss the AlertDialog. Can somebody please help me ?
public class VypocetDlzkyActivity extends Activity {
EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vypocet_dlzky);
}
public void zmenPrevodZ(View view){
final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
AlertDialog.Builder builder = new AlertDialog.Builder(VypocetDlzkyActivity.this);
builder.setTitle("Vyberte jednotku");
builder.setItems(jednotkyDlzky,null);
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String value = jednotkyDlzky[item].toString();
prevodDlzkaZtlacidlo.setText(value);
dialog.cancel();
}
};
final AlertDialog alert = builder.create();
alert.show();
}
You need to set the values of these 2 member variables in your onCreate() method, like this:
HodnotaDlzka = (EditText)findViewById(R.id.xxxx);
prevodDlzkaZtlacidlo = (Button)findViewById(R.id.yyyy);
xxxx is the ID you gave the EditText in activity_vypocet_dlzky.xml and yyyy is the ID you gave to the Button.
Also, after a button is clicked in the AlertDialog, the dialog is automatically dismissed, so you don't need to call dialog.cancel().
Problem is you didnt add any onClick listnerz.On clicking on button you need to call the required method.
public class MainActivity extends Activity implements OnClickListener {
EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HodnotaDlzka = (EditText) findViewById(R.id.e1);
prevodDlzkaZtlacidlo = (Button) findViewById(R.id.b1);
prevodDlzkaZtlacidlo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Vyberte jednotku");
builder.setItems(jednotkyDlzky,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String value = jednotkyDlzky[item].toString();
prevodDlzkaZtlacidlo.setText(value);
}
});
enter code here
final AlertDialog alert = builder.create();
alert.show();
}
}

Custom dialog in custom class returns boolean value

I have searched for the answer and dont find it, so please help me :)
I have a custom class:
public class CustomClass {
private final Context ctx;
public CustomClass(Context ctx) {
this.ctx = ctx;
}
public boolean setDialog(int head, int text) {
final boolean value;
final Dialog d = new Dialog(ctx);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.custom2_dialog);
TextView txtHead = (TextView) d.findViewById(R.id.custom2_txtHead);
txtHead.setText(ctx.getResources().getString(head));
TextView txtText = (TextView) d.findViewById(R.id.custom2_txtText);
txtText.setText(ctx.getResources().getString(text));
Button btnOK = (Button) d.findViewById(R.id.custom2_btnOK);
btnOK.setText("OK");
btnOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
value = true;
d.dismiss();
}
});
Button btnNO = (Button) d.findViewById(R.id.custom2_btnNO);
btnNO.setText("NO");
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
value = false;
d.dismiss();
}
});
d.show();
return value;
}
}
You can see that i have a custom dialog created in my custom class because i dont want to create in every activity a dialog. Now when i use it in an Activity:
CustomClass cC = new CustomClass(this);
if(cC.setDialog(R.string.head, R.string.text)) {
// user checked OK
} else {
// user checked NO
}
How to know if user checked OK or NO, because the return true, false value dont work in custom class, the dialog wont wait before the user clicks, it automatic return a value.
Here is the solution to your question.
First of all, if you are creating the Custom dialog then you have to to extend the Dialog class to read the response and implement the OnClickListener.
class CustomizeDialog extends Dialog implements OnClickListener
{
// some code
public CustomizeDialog(Activity activity, String title, String msg, int i, Typeface typeface)
{
super(activity);
this.activity = activity;
intFlag = i;
setContentView(R.layout.relative);
dialogButtonYes = (Button) findViewById(R.id.buttonCustomDialogYes);
dialogButtonNo = (Button) findViewById(R.id.buttonCustomDialogNo);
textView = (TextView) findViewById(R.id.textViewCustomTitle);
this.msg = msg;
dialogButtonNo.setOnClickListener(this);
dialogButtonYes.setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallForOne.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}
else if(intFlag == 2)
{
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallMe.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}
And the relative.xml file
<TextView
android:id="#+id/textViewCustomTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewCustomTitle"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/buttonCustomDialogYes"
style="#style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="#string/yes" />
<Button
android:id="#+id/buttonCustomDialogNo"
style="#style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="#string/no" />
</LinearLayout>
And the main class
class Main implements OnClickListener
{
onCreate()
{
//your code
}
public void on click()
{
customizeDialog = new CustomizeDialog(CustomDialogExample.this,getString(R.string.title_for_one),getString(R.string.msg_for_one),1,"String");
customizeDialog.show();
}
}
I was facing the same problem here, then I found one solution, to open an Activity from button click in a custom dialog. But you can use this logic for another actions too.
My custom method:
public class Extension extends Activity{
public void showDialogConfirma(Activity localActivity, Class destActivity, String titulo, String mensagem){
final Dialog dialog = new Dialog(localActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_um_btn);
TextView textTitulo = dialog.findViewById(R.id.txtTitle);
textTitulo.setText(titulo);
Button btnOk = dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
// here I can open any activity from any class
localActivity.startActivity(new Intent(localActivity, destActivity));
}
});
dialog.show();
}
}
You call this method like this:
Extension extension = new Extension();
// here you gonna pass your parameters to your Dialog on create it
// on my case the activity that I want open
extension.showDialogConfirma(ThisActivity.this, DestActivity.class, titulo, mensagem);
First of all the comment from #user370305 is the way to do it right. There can be other ways no doubts about that.
The problem you are facing is because when you are calling a method of custom class
if(cC.setDialog(R.string.head, R.string.text));
you are getting the value even though nothing has been clicked
return value;
as false which is the default value set when you are declaring
final boolean value;
Instead you should listen for the Cancel and Ok clicks

Categories

Resources