I'm targeting API level 8+. I need a dialog with a title, a message, a checkbox with some text and two classic bottom buttons.
I'm trying to use a standard AlertDialog, and method setView to add the checkbox:
checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:checked="true"
android:textSize="14sp" >
</CheckBox>
</FrameLayout>
showDialog method
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Title");
builder.setMessage("Message");
View checkboxView = View.inflate(activity, R.layout.checkbox, null);
CheckBox checkBox = (CheckBox) checkboxView.findViewById(R.id.checkbox);
checkBox.setText("Some checkbox text");
builder.setView(checkboxView);
// First button
builder.setNegativeButton(getString(R.string.not_now),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
});
// Second button
builder.setPositiveButton(getString(R.string.help),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
});
builder.create();
builder.show();
}
My AppBaseTheme has parent "Theme.AppCompat.Light" (/res/values/styles.xml), the same for values-v11 and "Theme.AppCompat.Light.DarkActionBar" for values-v14.
I've problem in 2.3.3, checkbox and text have no margins or padding (I've set them in xml), besides text is black as the background. Everything looks fine in 4.4.
I've read a lot of things about this problem, but I'm still confused and without solution. Can you please help me?
You can force a white background of your dialog by using:
if(Build.VERSION.SDK_INT <= 10) {
builder.setInverseBackgroundForced(true);
}
Related
How to make single choice list dialog right to left? More specifically I use rtl language and want the radio button to be placed and aligned at right side. Should I implement a custom dialog, a custom adapter or there is much simpler solution?
Any help or suggestions is appreciated.
Building dialog code:
final String items[] = {"1", "2"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("title")
.setSingleChoiceItems(new ArrayAdapter<String>(this,
R.layout.rtl_list_item,
R.id.text, items),
0,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
Now create your layout file, Your custom xml in your layout folder will be as
layout/rtl_list_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:textDirection="rtl"
android:textAlignment="gravity"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
/>
I used AlertDialog (android.support.v7.app.AlertDialog) for showing levels of game as below
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogLevelsStyle);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_game_level, null);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.cancel();
}
}
});
dialog = builder.create();
dialog.show();
//styles.xml
<style name="DialogLevelsStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#3caaf0</item>
</style>
But the dialog display the positive button OK at the button-right of the alert dialog, Please help me!
I update the question by added image:
This happens because of Rules for aligning buttons.
Action buttons are typically Cancel and/or OK, with OK indicating the preferred or most likely action. However, if the options consist of specific actions such as Close or Wait rather than a confirmation or cancellation of the action described in the content, then all the buttons should be active verbs. Order actions following these rules:
The dismissive action of a dialog is always on the left. Dismissive actions return to the user to the previous state.
The affirmative actions are on the right. Affirmative actions continue progress toward the user goal that triggered the dialog.
You can't change alignment for your positive/negative buttons. Simply add Button to your view which you inflate for dialog aligned to the center and process it as below:
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (dialog != null) {
dialog.cancel();
}
}
});
Update
This is how it should look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:text="something"
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/id2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
P.S. I cannot check if everything properly, cuz my design window have some problems in Studio, but this is the right way and seems to work fine.
I am trying to implement a CalendarView in an AlertDialog. It works fine except for how the calendar turns completely white which leads to the numbers being barely readable and there are no lines between them. Does anybody have a clue why this is the case?
Here is my code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="#+id/calendarID"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="300dip"
android:tag="my tag" />
</FrameLayout>
and
public void onClickZumKalendar(final View view){
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout ll= (FrameLayout)inflater.inflate(R.layout.calendar_dialog, null, false);
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("something")
.setView(ll)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do nothing...yet
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}
).show();
}
The property you want for the text color is:
android:focusedMonthDateColor
Here is an example edit to your code which results in a green background and large black text:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="#+id/calendarID"
android:background="#color/light_green"
android:dateTextAppearance="#android:style/TextAppearance.Large"
android:focusedMonthDateColor="#color/black"
android:layout_width="match_parent"
android:layout_height="300dip"/>
</FrameLayout>
I have an alert dialog created using the alertdialog builder. I want to remove the space in the left and right of the dialog... basically extend it from side to side. I know I could you an activity instead of a dialog, but I want to keep the button style and implementing that button style in an activity requires making a layout for different SDKs, which is not convenient in the long run.
Why I need it full width?
Because I need to display AdMob ads and if they are not full width the ads will not load.
Any help is appreciated as I have tried all kinds of theme properties...
Thanks,
Adrian
PS: Here is my current code for creating the dialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogTheme));
alertDialogBuilder.setInverseBackgroundForced(true);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.dial_dialog, null);
AdView adView = (AdView) view.findViewById(R.id.adView);
if (!application.getLicense().isValid()) {
adView.loadAd(new AdRequestWrapper(this));
}
alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle(R.string.dial_dialog_title).setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.dial_dialog_message_positive_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNeutralButton(R.string.dial_dialog_message_neutral_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton(R.string.dial_dialog_message_negative_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
PS2: Here is an image with what I need... http://www.tiikoni.com/tis/view/?id=f3aed38
I need to loose all the space with red. I don't have any problems with the space marked in yellow. (it can be kept or it can be removed)
I know a little bit late to come back with an answer, but following the recipes from here I managed to solved almost everything. Basically I have the following Dialog theme:
<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#drawable/screen_background_selector_light</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
Note the reference to screen_background_selector_light which means I also had to add screen_background_selector_light.xml and (its dependency background_holo_light.xml).
Other that that I only had to specify the activity will use this new theme:
<activity android:name=".zom.xyz.app.activity.MyActivity_" android:label="#string/activity_title" android:theme="#style/DialogTheme"/>
And of course create it as any other activity.
As for the other need I had... easy buttons I did it this way. Here is the layout of the activity
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
INSERT_DIALOG_CONTENT_HERE
</RelativeLayout>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cancelButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_negative_text"/>
<Button
android:id="#+id/skipButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_neutral_text"/>
<Button
android:id="#+id/correctButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_positive_text"/>
</LinearLayout>
Using this layout and theme will give you the exact feel of a dialog while giving it full width.
1 )Create your own dialog extended from Dialog and in constructor set style, something like this:
public MyDialog(Context c, Event e) {
super(c, android.R.style.Theme_Light);
}
or
2) when creating instanse, set style:
Dialog dialog = new Dialog(this, R.style.MyDialogTheme);
example here
I'm using a single choice alert dialog in which I'd like to replace the default blue (the title line and the radio buttons) to the orange that I am using in the title bar. I was able to change to the title bar using setCustomTitle(), but I am lost trying to get rid of this damn blue.
For the title bar
<?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:orientation="vertical"
android:background="#color/orange" >
<TextView
android:id="#+id/alertTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="14dp"
android:gravity="center"
android:text="Alert Title"
android:textColor="#color/white"
android:textSize="18sp" />
</LinearLayout>
Building the AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View customTitle = View.inflate(MainActivity.this, R.layout.custom_alert_title, null);
builder.setCustomTitle(customTitle);
builder.setSingleChoiceItems(mAlertOptions, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do stuff
dialog.dismiss();
}
}).create().show();
This is what it looks like
I need to get rid of this blue! Help!
The only way to change the title divider color is to use Resources.getIdentifier in combination with Window.findViewById. The checkmark can be easily changed by calling AlertDialog.Builder.setSingleChoiceItems(ListAdapter, int, OnClickListener).
Here's an example:
Single choice item layout
: I generated your_radio_button using Android Holo Colors.
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#drawable/your_radio_button"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="16dip"
android:paddingStart="16dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorAlertDialogListItem" />
Implementation
final ListAdapter adapter = new ArrayAdapter<String>(this,
R.layout.select_dialog_singlechoice, android.R.id.text1, new String[] {
"Option 1", "Option 2"
});
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_alert_title, null));
builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do something
}
});
// Show the AlertDialog
final AlertDialog dialog = builder.show();
// Change the title divider
final Resources res = getResources();
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = dialog.findViewById(titleDividerId);
titleDivider.setBackgroundColor(res.getColor(android.R.color.holo_orange_dark));
Results