I want to create a dialog box when a button is clicked inside the dialog box I need 2 radio buttons which are linked with mobile default music and other is external memory of the mobile. Anyone can help me out this here is my code as follows?
buttonSound = (Button) findViewById(R.id.btn_sound);
buttonSound.setOnClickListener (new OnClickListener(){
public void onClick(View v){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
Log.d("TAG","button inside mobile");
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Sound",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlarmActivity.this.finish();
}
});
// builder.setPositiveButton("Sound", );
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("SdCard",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Log.d("TAG","button inside sdCard");
AlarmActivity.this.finish();
}
});
alertDialogBuilder.show();
And it is showing error as follows given below
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Use AlarmActivity.this or this intead of getApplicationContext().
Create in layout in res/layout
radio_dialog_layout.xml
<RadioButton
android:id="#+id/rd_!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="#+id/rd_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rd_!"
android:text="B" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rd_2"
android:layout_centerInParent="true"
android:text="OK" />
</RelativeLayout>
create dialog in activity by using this code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.radio_dialog_layout);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
// there are a lot of settings, for dialog, check them all out!
// set up radiobutton
RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);
// now that the dialog is set up, it's time to show it
dialog.show();
You can also refer this tutorials: How to add lists, Checkboxes and Radio buttons to an AlertDialog – Android
Try this..
Change this..
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
to
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
EDIT
radio_btn.xml
<?xml version="1.0" encoding="UTF-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/status_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<RadioButton
android:id="#+id/default_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default music" />
<RadioButton
android:id="#+id/external_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External card music" />
</RadioGroup>
JAVa
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
Log.d("TAG","button inside mobile");
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setView(dialoglayout);
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlarmActivity.this.finish();
}
});
alertDialogBuilder.show();
Create one xml file,
<RadioButton
android:id="#+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi" />
<RadioButton
android:id="#+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radiobutton1"
android:text="Bye" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/radiobutton2"
android:layout_centerInParent="true"
android:text="Click" />
and create Dialog in activity,
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("My Dialog Box");
dialog.setCancelable(true);
// set up radiobutton inside dialog box
RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.radiobutton1);
RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.radiobutton2);
// Show the dialog
dialog.show();
Related
I created a custom alert dialog to show in my app. It has an imageView, two textViews and a button. I am trying to show this custom alert dialog once my main activity launches.I am calling this custom alert dialog.show() in a separate method according to my requirement.The custom alert dialog is showing up but on click of a button it is not getting dismissed, also the custom alert dialog is showing a extra white background.
showCustomAlertDialog() Method
public void showCustomAlertDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View promptView =layoutInflater.inflate(R.layout.reminder_alert_dialog, null);
final AlertDialog builder = new AlertDialog.Builder(this).create();
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
builder.setView(promptView);
builder.show();
}
reminder_alert_dialog Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#drawable/ic_alert" />
<TextView
android:id="#+id/greetings_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOOD MORNING, KEVIN"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/medicines_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Have you taken your medicines today?"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="normal"
android:layout_below="#id/greetings_text"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/record_button"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3BC4B8"
android:text="Record"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_below="#id/medicines_text"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Coming to the extra white background, you have to apply the Window Background colour to transparent for the AlertDialog with this code
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
And the issue where dialog isn't showing, is the System.out.println() called? I would also recommend you setting the view for the AlertDialog.Builder before setting the listeners like,
final AlertDialog builder = new AlertDialog.Builder(this).create();
builder.setView(promptView);
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
Try like this...
//class variables
AlertDialog dialog;
Button dismiss;
//Add your other views if required
//in onCreate() activity method
makeDialog();
//makeDialog() method code
void makeDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
View root = getLayoutInflater().inflate(R.layout.your_layout,null);
dismiss = root.findViewById(R.id.your_button_id);
//another views initialization goes here...
dismiss.setOnClickListener(new View.OnClickListener(){
dialog.dismiss();
//your logic or job...
});
builder.setView(root);
dialog = builder.create();
}
finally show the dialog whenever you want
dialog.show();
I have a custom AlertDialog with radio buttons which will open on click of a textview. Everything is working as it is supposed to except that the radio buttons are never shown as "checked". Whenever I open the AlertDialog all of the buttons are unchecked. After selecting a button and opening the AlertDialog again the buttons are unchecked again.
My XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<RadioGroup
android:id="#+id/rg_gender"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<RadioButton
android:id="#+id/buttonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_male"
/>
<RadioButton
android:id="#+id/buttonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_female"
/>
<RadioButton
android:id="#+id/buttonEither"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_either"
/>
</RadioGroup>
</LinearLayout>
My AlertDialog:
private void therapistDialog() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.gender_dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = dialogBuilder.show();
final RadioGroup rg = (RadioGroup) dialogView.findViewById(R.id.rg_gender);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.buttonMale){
tvGender.setText(getResources().getString(R.string.therapist_male));
therapistSex = 0;
mMassage.setTherapistSex(therapistSex);
male.setChecked(true);
alertDialog.dismiss();
} else if (checkedId == R.id.buttonFemale){
tvGender.setText(getResources().getString(R.string.therapist_female));
therapistSex = 1;
mMassage.setTherapistSex(therapistSex);
alertDialog.dismiss();
} else if (checkedId == R.id.buttonEither){
tvGender.setText(getResources().getString(R.string.therapist_either));
therapistSex = 2;
mMassage.setTherapistSex(therapistSex);
alertDialog.dismiss();
}
}
});
You are not restoring the state of dialog on opening and assign a new instance of AlertDialog every time You invoke therapistDialog method.
Try to check the button on AlertDialog view init.
Add
int buttonToCheck = mMassage.getTherapistSex();
RadioGroup radioGroup = dialogView.findViewById(R.id.rg_gender);
int buttonId = radioGroup.getChildAt(buttonToCheck).getId();
radioGroup.check(radioGroup.getChildAt(buttonToCheck).getId());
Before invoking dialogBuilder.setView(dialogView);
Every time you call the method therapistDialog() is creating a new instance of the alertDialogBuilder. You must create instance of the alertDialogBuilder only once globally,
and just call
final AlertDialog alertDialog = dialogBuilder.show(); inside your method.
note: this is not tested but this should work and will maintain your check box states also
Hello senior developer.
recently I create alertdialog.
I know title size, message size controll
but I don't know controll button text size
How controll button text size ?
I must this format please
thanks.
View view = getLayoutInflater().inflate(R.layout.connecting_error_dialog, null);
TextView txtTitle = (TextView) view.findViewById(R.id.title);
txtTitle.setTextSize(40);
txtTitle.setTextColor(Color.RED);
txtTitle.setText("fail");
TextView message = (TextView) view.findViewById(R.id.message);
message.setTextSize(30);
message.setText("dddddd");
AlertDialog.Builder builder = new AlertDialog.Builder(Connectingreceiver.this);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="HelloAlert!"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"/>
</LinearLayout>
The alert dialog needs to be created first and then it's elements become accessible to be changed.
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTextSize("desired font size");
Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
btnNegative.setTextSize("desired font size");
}
});
return alert;
Hell i am trying to make alert dialogue to show highscore point.but i cant add textview on my dialogue , highscore stores in textview. here is my code i need to show textview when i click NO button.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Time is up!")
.setCancelable(false)
.setPositiveButton("Restart!",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
App2Activity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}.start();
create xml file dialog_layout_pro.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dedede"
>
<TextView
android:id="#+id/tv_dialog"
android:layout_margin="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#444444"
android:text="Please wait..."
android:textSize="30sp" />
</RelativeLayout>
and in java class
{
Dialog cusDialog;
cusDialog = new Dialog(MyTestClass.this, R.style.CustomDialog);
cusDialog.setContentView(R.layout.dialog_layout_pro);
TextView text = (TextView) cusDialog.findViewById(R.id.tv_dialog);
text.setText("here your counter value");
cusDialog.setCancelable(false);
cusDialog.show();
}
i have to align text by middle in android alertdialog.
but i cannot find way...
anyone knows how to this?
try this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage("your message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
I know this thread is old but might help some people :D
TextView title = new TextView(this);
title.setText("Client details not saved!");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
// title.setTextColor(getResources().getColor(R.color.greenBG));
title.setTextSize(23);
TextView msg = new TextView(this);
msg.setText("You're going to lose all the information if you continue!");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
finish();
}
}
};
Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setView(msg);
builder.setCancelable(true);
builder.setPositiveButton("Yes", onClick);
builder.setNegativeButton("No", onClick);
AlertDialog dialog = builder.create();
dialog.show();
Best way is to design Custom Dialog box.
view_dialog_box.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A9E2F3">
<TextView
android:id="#+id/txtDiaTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connection Alart"
android:textColor="#color/Black"
android:textStyle="bold"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#2E9AFE"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="#+id/txtDiaMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="No Internet Connection"
android:textColor="#color/Black" />
<Button
android:id="#+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OK"
android:textColor="#color/Black"
android:textStyle="bold"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#color/White"/>
Then it use in java file
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.view_dialog_box);
// set the custom dialog components - text and button
TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle);
TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg);
Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Just use that method and your dialog title and message will appear at center:
public static void openDialog(Context context, String message) {
TextView title = new TextView(context);
// You Can Customise your Title here
title.setText("Information Message");
title.setBackgroundColor(Color.BLACK);
title.setPadding(10, 15, 15, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(22);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCustomTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
// You Can Customise your Message here
TextView messageView = (TextView) alertDialog
.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
You can use your custom layout for alert dialog layout. To align default alert dialog layout message center you can do
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("hello world");
alertDialog = builder.show();
TextView messageText = (TextView) alertDialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
Be careful, if you set messageText with findViewById before you call builder.show() you'll get a null pointer exception.
Have your TextView fill the parent and give it a center gravity.
<TextView ... android:layout_width="fill_parent" android:gravity="center" />
You would have to use one of the constructors provided for AlertDialog in Android, while creating one.
AlertDialog(Context context, int theme)
Construct an AlertDialog that uses an explicit theme.
This link will help you. Since you want the text to be centered, you would want to give the gravity attribute, the value 'center'.
Try this - it will do the trick.
AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this);
TextView resultMessage = new TextView(Main.this);
resultMessage.setTextSize(22);
resultMessage.setText("Upload completed!");
resultMessage.setGravity(Gravity.CENTER);
completeDialog.setView(resultMessage);
completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#SuppressLint("DefaultLocale")
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
completeDialog.show();