How to display soft keyboard after AlertDialog show - android

I have Google this proplem, but none of the answers works for me.
This is the style of my AlertDialog
<style name="dialog_full_screen">
    <item name="android:windowBackground">#android:color/transparent</item>
    <item name="android:windowFrame">#null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">#null</item>
    <item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>
This is the code to show it.
val alertDialog = AlertDialog.Builder(this, R.style.dialog_full_screen).apply {
setCancelable(true)
}.create()
alertDialog.apply {
val wlp = window!!.attributes
wlp.gravity = Gravity.BOTTOM
wlp.flags = wlp.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND.inv()
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
wlp.width = displayMetrics.widthPixels
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
window!!.attributes = wlp
}
alertDialog.show()
The following code is layout
<LinearLayout
android:id="#+id/linear_layout_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
style="#style/CenterInConstraintLayout"
tools:ignore="MissingConstraints">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/index_play_page_icon_comments_null"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/homepage_no_comments"
android:textColor="#color/color_gray_dark2"
android:textSize="#dimen/font_size_14"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center">
<EditText
android:id="#+id/edit_text_comment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:maxLength="150"
android:hint="#string/homepage_hint_comment"
android:textColorHint="#color/color_gray_dark2"
android:background="#null"
android:importantForAutofill="no" />
<ImageView
android:id="#+id/image_view_send"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="30dp"
/>
</LinearLayout>
This AlertDialog can show properly, but i can't show soft keyboard

alertDialog.window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

Give the EditText the type of softkeyboard you want, otherwise default will get applied
// Get the Edit text box to put into the dialog
final EditText input = findViewById(R.id.edit_text_comment)
// set the type of input entered, this would be for entry of a number digit keyboard
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
...
input.requestFocus();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
There is also question posted for similar situation here:
when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop
or else try this:
How to show soft-keyboard when edittext is focused

Related

PopupWindow not shown as expected (preview is ok)

I'm trying to show a full screen popup window from a fragment. I was starting the layout for the screen and it looked as expected in the AS screen preview, but it doesn't when it is executed in a device. Basically, it seems that the paddings are omitted. All of them, the paddings in the root of the layout, in the image view which I put to increase the clickable area, in the text input layout (added by theme).
On the other hand, the animation is not shown in some devices but in emulator is.
Here is the layout file ``:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_gray"
android:fitsSystemWindows="true"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom">
<ImageView
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/grid_size_x2_5"
android:src="#drawable/ic_close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_title"
style="#style/TextAppearance.Touche.H3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Product"
android:text="Product"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/bt_close"
tools:textColorHint="#color/white" />
<Button
android:id="#+id/bt_add_item"
style="#style/TextAppearance.Touche.Button.Primary"
android:layout_width="match_parent"
android:layout_height="#dimen/button_height"
android:text="#string/add_order_add__to_order"
app:layout_constraintBottom_toBottomOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/ti_quantity"
android:layout_width="#dimen/grid_size_x10"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/grid_size_x6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/bt_close">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:lines="1"
android:maxLength="50"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>
<ImageButton
android:id="#+id/bt_less"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:src="#drawable/ic_less_selector"
app:layout_constraintBottom_toBottomOf="#id/ti_quantity"
app:layout_constraintEnd_toStartOf="#id/ti_quantity"
app:layout_constraintTop_toTopOf="#id/ti_quantity" />
<ImageButton
android:id="#+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:src="#drawable/ic_add_selector"
app:layout_constraintBottom_toBottomOf="#id/ti_quantity"
app:layout_constraintStart_toEndOf="#id/ti_quantity"
app:layout_constraintTop_toTopOf="#id/ti_quantity" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here you have the kotlin code inside a fragment:
context?.let {
val customView: View = (it.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.popup_add_item, null)
val popupWindow = PopupWindow(it)
popupWindow.contentView = customView
popupWindow.width = LinearLayout.LayoutParams.MATCH_PARENT
popupWindow.height = LinearLayout.LayoutParams.MATCH_PARENT
popupWindow.animationStyle = R.style.popup_window_animation // TODO Not working in some devices
popupWindow.elevation = 5f
popupWindow.showAtLocation(getBinding().rvDiscounts, Gravity.CENTER, 0, 0)
val btClose = customView.findViewById<ImageView>(R.id.bt_close)
val btAddToCart = customView.findViewById<Button>(R.id.bt_add_item)
val btAdd = customView.findViewById<ImageButton>(R.id.bt_add)
val btLess = customView.findViewById<ImageButton>(R.id.bt_less)
val etQuantity = customView.findViewById<TextInputEditText>(R.id.et_quantity)
btClose.setOnClickListener {
popupWindow.dismiss()
}
btAddToCart.setOnClickListener {
// TODO add to cart
popupWindow.dismiss()
}
btAdd.setOnClickListener {
val currentQuantity = etQuantity.text.toString().toInt()
etQuantity.setText(currentQuantity.inc().toString())
}
btLess.setOnClickListener {
val currentQuantity = etQuantity.text.toString().toInt()
if (currentQuantity > 0) etQuantity.setText(currentQuantity.dec().toString())
}
}
And finally the screenshots, the preview and expected first, the shown on the device second:
To the plus and minus button use a android:margin_start="8dp" and android:margin_end="8dp" respectively, to get some space between the button and the TextInputEditText box.
If you wish to center the zero within TextInputEditText then use android:gravity="center"
The #dimen/padding_start values in these might not be acceptable, so just hardcode the values using android:paddingHorizontal="8dp" and android:paddingVertical="8dp".
<style name="AppTheme.FullScreenDialog" parent="Theme.MaterialComponents.Light.Dialog">
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="actionMenuTextColor">#color/colorAccent</item>
</style>
Use this style while creating your Dialog in the fragment like this :
var fullScreenCameraDialog = Dialog(context, R.style.AppTheme_FullScreenDialog)

Change style for AlertDialog lower grey panel

What style is causing the grey bar in the bottom of an AlertDialog?
Either I need to change the rest of the dialog to match the color or vice versa. I've tried modifying #android:buttonStyle and #android:buttonBarStyle. That helps but there's still some grey peeking out from the edges of the region.
Here are my current styles:
<style name="MyAlertDialog" parent="#android:style/Theme.Dialog">
<item name="#android:background">#FF000000</item>
<item name="#android:buttonBarStyle">#style/MyButtonBar</item>
</style>
<style name="MyButtonBar" parent="#android:style/ButtonBar">
<item name="#android:background">#FF000000</item>
</style>
And it looks like this:
Here is a working solution, based on Rod_Algonquin's idea of using a custom layout.
private void showCustomAlert (String message)
{
// build dialog
LayoutInflater inflater = getLayoutInflater();
View alertView = inflater.inflate (R.layout.custom_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder (this, R.style.CustomAlertDialog);
builder.setView (alertView);
final AlertDialog alert = builder.create();
// message
((TextView)alertView.findViewById (R.id.message)).setText (message);
// ok button
alertView.findViewById (R.id.cancel).setOnClickListener (new View.OnClickListener()
{
#Override public void onClick(View v) { alert.dismiss(); }
});
// show
alert.show();
}
Here is a layout that works with this code:
<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="#FF000000">
<TextView android:id="#+id/alertTitle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:gravity="center"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="#string/custom_alert_title" />
<TextView android:id="#+id/message"
style="#android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textAppearance="#android:style/TextAppearance.Medium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:background="#android:color/black"
android:measureWithLargestChild="true">
<Button android:id="#+id/cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minHeight="50dp"
android:minWidth="100dp"
android:textSize="14sp"
android:text="#android:string/ok" />
</LinearLayout>
</LinearLayout>
Finally, here's the styles, from styles.xml, I referenced; you may not need them depending on your dialog coloring.
<style name="CustomAlertDialog" parent="#android:style/Theme.Dialog">
<item name="#android:background">#FF000000</item>
<item name="#android:buttonBarStyle">#style/CustomButtonBar</item>
</style>
<style name="CustomButtonBar" parent="#android:style/ButtonBar">
<item name="#android:background">#FF000000</item>
</style>

style spinner drop down list

I have several issues with styling a spinner
I need to change the drawable of the spinner(selected view)
when the user opens the dropdown list(I tried several selectors
with different states but nothing works properly)
I can`t change the text color on the items in dropdown list (I can change
color but I need different color for the selected value) you can see in more
detail in links below.
I can`t remove the dividers from the dropdown.
I don`t want to use custom layout with button and a list to simulate the functionality of spinner(but if there is no solution I will do that).
The style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="android:colorBackground">#android:color/white</item>
<item name="android:dropDownSpinnerStyle">#style/SpinnerTheme</item>
</style>
<style name="SpinnerAppTheme" parent="android:Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/spinner_background_holo_light</item>
<item name="android:dropDownSelector">#drawable/list_selector_holo_light</item>
<item name="android:dropDownListViewStyle">#style/mySpinnerStyle</item>
</style>
<style name="mySpinnerStyle" parent="android:Widget.ListView.DropDown">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0px</item>
</style>
<style name="Theme_Dialog_Translucent" parent="android:Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:background">#android:color/white</item>
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="Theme_Dialog_Measurment_Data_Dialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:background">#color/general_background_color</item>
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:textColor">#android:color/black</item>
</style>
<Spinner
android:id="#+id/res_spinner"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="60"
android:background="#drawable/ref_spinner_selector"
android:dropDownVerticalOffset="1dp"
android:dropDownWidth="60dp"
android:gravity="center"
android:popupBackground="#null"
android:spinnerMode="dropdown"/>
how the spinner needs to be look like
how the spinner looks like now
Thanks
Update:
if anyone will have problems to customize spinners
I manage to fix all the issues using ListPopupWindow
private void initPopup()
{
_lp = new ListPopupWindow(getActivity());
_lp.setAnchorView(_resTitle);
ColorDrawable cd = new ColorDrawable(getResources().getColor(android.R.color.transparent));
_lp.setBackgroundDrawable(cd);
_lp.setOnDismissListener(new OnDismissListener()
{
#Override
public void onDismiss()
{
_resTitle.setSelected(false);
}
});
_listPopupAdapter = new DataSetListAdapter(getActivity(), _resData);
_lp.setAdapter(_listPopupAdapter);
}
onclick
case R.id.res_title:
if (!_lp.isShowing())
{
_resTitle.setSelected(true);
_lp.setOnItemClickListener(this);
_lp.show();
_lp.getListView().setDivider(null);
_lp.getListView().setDividerHeight(0);
}
else
{
_resTitle.setSelected(false);
_lp.dismiss();
}
break;
Android does not permit make customization of spinner UI.So
If you want to modify your spinner UI,Then use android popup view where you can put your style UI with effect like drop-down menu
example
spinner=(EditText)findViewById(R.id.txt_Spinner);
spinner.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
p = new Point();
p.x = location[0]+(v.getHeight());
p.y = location[1]+v.getHeight();
if (p != null)
showPopup(statusActivity.this, p);
System.out.println("show popup");
}
});
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 300;
int popupHeight = 500;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 00;
int OFFSET_Y = 00;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
((TextView)layout.findViewById(R.id.textView9)).setClickable(true);
}
and popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/popup_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_orange_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Sleeping"
android:text="Sleeping" />
<TextView
android:id="#+id/textView3"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Available"
android:text="Available" />
<TextView
android:id="#+id/textView4"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Busy"
android:text="Busy" />
<TextView
android:id="#+id/textView5"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At work"
android:text="At work" />
<TextView
android:id="#+id/textView6"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Battery charge low"
android:text="Battery charge low" />
<TextView
android:id="#+id/textView7"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="In meeting"
android:text="In meeting" />
<TextView
android:id="#+id/textView8"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="TMS me later"
android:text="TMS me later" />
<TextView
android:id="#+id/textView9"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At the toilet"
android:text="At the toilet" />
<EditText
android:id="#+id/textCustomize"
style="#style/text_blue_contains"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:tag="Customize"
android:text="Customize" />

Text not fiting in Button

i am trying to display the text in the center of button, but text is not completely fit in the button like that
here is i am trying to display V and W and space is also there but it just not fit in. Help Please
text of style is like this
<style name="textstyle">
<item name="android:textSize">15sp</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#000000</item>
</style>
size of button is decided on run time
box = new Button(context);
box.setTextAppearance(context, R.style.input_box_textstyle);
Drawable image = context.getResources().getDrawable(R.drawable.inputbox);
box.setBackgroundDrawable(image);
.
.
.
if(size <= n)
{
Log.v("textsize..", width+"");
p = new LinearLayout.LayoutParams(width, height);
}
else
{
p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
p.weight = 1;
}
box.setLayoutParams(p);
isSet = false;
Issue solve by setting the
box.setIncludeFontPadding(false);
box.setPadding(0, 0, 0, 0);
I hope this must be your xml
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_below="#id/imagetitle"
android:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="V" android:id="#+id/previous" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="W" android:id="#+id/next" />
</LinearLayout>

AlertDialog Theme: How to change item text color?

When I try to apply a standard theme to AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle("Change");
String[] info= this.getResources().getStringArray(R.array.info);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.addAll(info);
builder.setSingleChoiceItems(arrayAdapter, ....
Result:
The note is that I have no problem with builder.setItems(...) since its text color is Black while the theme applied with builder.setSingleChoiceItems(...) has a White text color.
Any fast fix? Or any way to create a customized theme based on AlertDialog.THEME_DEVICE_DEFAULT_LIGHT ?
My customized style doesn't work as expected:
<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
<item name="android:textColor">#7ABDFF</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->
<item name="android:layout_centerHorizontal">true</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:textColorAlertDialogListItem">#A844BD</item>
<item name="android:itemBackground">#7ABDFF</item>
</style>
Update
#lopez answer is a complete solution, but I find a single line fix for my problem, a custom theme to be applied to the activity in manifest:
<style name="MyTheme">
<item name="android:textColorAlertDialogListItem">#android:color/black</item>
</style>
If anyone happens to read this, and is using the Suport library Alert Dialogs and wants to change the text color of list items then drop the android: part, like so:
<item name="textColorAlertDialogListItem">#color/your_color</item>
Personally, I use the android Dialog but I use a custom layout for that match the design of my application.
Here is an example:
new AlertDialog.Builder(context)
.setView(inflater.inflate(R.layout.dialog_delete_contact, null))
.setPositiveButton(context.getResources().getString(android.R.string.ok).toUpperCase(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// YOUR TREATMENT
}
})
.setNegativeButton(context.getResources().getString(android.R.string.cancel).toUpperCase(), null)
.show();
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/GrayLight"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/Black"
android:gravity="center"
android:orientation="horizontal"
tools:ignore="DisableBaselineAlignment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:fitsSystemWindows="true"
android:padding="10dip"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/remove_contact"
android:textColor="#color/White"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:ignore="DisableBaselineAlignment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:text="#string/ask_remove_contact"
android:textSize="15sp"
tools:ignore="HardcodedText" />
</LinearLayout>
Result in picture:
To avoid rewriting the code every time here is a utility class :
public class MyDialog {
public static Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
View view = layoutInflater.inflate(R.layout.generic_dialog, null);
((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
return new AlertDialog.Builder(context).setView(view);
}
}
And an example of using :
AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), "Quitter ECOLEMS", "Voulez-vous vraiment quitter l'application?");
myDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// YOUR TREATMENT
}
})
.setNegativeButton("Non", null)
.show();
I hope you have helped!
What works for me
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#color/text</item>
<item name="android:background">#color/background</item>
<item name="android:textColorPrimary">#color/text</item>
<item name="textColorAlertDialogListItem">#color/text</item>
</style>
This is what worked for me for every color on my dialog:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#00ffff</item>
<!-- Used for the title and text -->
<item name="android:textColor">#00ff00</item>
<item name="android:textColorAlertDialogListItem">#ffff00</item>
<item name="android:textColorSecondary">#ff00ff</item>
<!-- Used for the background -->
<item name="android:background">#3a3a3c</item>
</style>
AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle("Change");
String[] info= this.getResources().getStringArray(R.array.info);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.addAll(info);
builder.setSingleChoiceItems(arrayAdapter, ....**change this line to**
builder.setSingleChoiceItems(info,0,null);

Categories

Resources