I am using android.support.v7.app.AlertDialog.However, I'm not able to remove the divider.Can anybody tell me how to remove it? thanks.
this is my style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
</style>
this is my code:
AlertDialog.Builder builder = new AlertDialog.Builder(mSettingsView.getBaseActivity());
builder.setTitle("Ringtone");
builder.setSingleChoiceItems(list, -1, listener1);
builder.setPositiveButton("OK", listener2);
builder.setNegativeButton("Cancel", listener3);
builder.show();
AlertDialog divider is different in pre-lollipop and lollipop devices. I found, divider colour is Gray in pre-lollipop(pre-material design) devices. So it is visible. But for material design(lollipop) devices, divider colour is transparent, so it seems not visible/present.
To make divider visible across all devices, explicitly make the colour Gray or any other colour.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setDivider(new ColorDrawable(Color.GRAY));
listView.setDividerHeight(1);
alertDialog.show();
Are you using the AlertDialog inside a android.support.v4.app.DialogFragment? I always use it this way and I never get the divider in your screen:
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance(){
return new MyDialogFragment;
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate my custom layout
View layout = inflater.inflate(R.layout.my_custom_layout, null);
// Initialize my layout components
...
// Build dialog
builder.setTitle("TITLE")
.setView(layout)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", listener);
return builder.create();
}
}
Related
In my app I have this theme in the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/neon_green</item>
<item name="android:windowBackground">#color/black_17</item>
</style>
but the attribute windowBackground does not cause the dialog background to change. The dialog background color is still white. The neon_green highlight color does appear for the dialog text though.
Only the background color that does not change.
AlertDialog.Builder builder =
new AlertDialog.Builder(new ContextThemeWrapper(act, R.style.AppTheme));
builder.setMessage("text")
.setPositiveButton("pos ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("neg", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();
I just added this to my styles
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">#color/black_17</item>
</style>
then I passed the styles to the builder
AlertDialog.Builder builder =
new AlertDialog.Builder(act,
R.style.AppCompatAlertDialogStyle);
I have used this line to add style to dialog box
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
I am able to style the Title name however the text color in the list remains black color only.
public class NotificationModeDialog extends DialogFragment {
public ArrayList<String> list = new ArrayList<>(); //initialized the array of list
public int i;
final String notificationListItems[]={
"CallAlert","address","name","Message","id","contact","ymail","Gmail"};//the data to be displayed in dialog box
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final boolean[] notificationSelected={false,false,false,false,false,false,false,false};//initializing the starting state
//initializing the alertDialog here AlertDialogStyle is added for styling
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
.setTitle("Notification Mode")
.setMultiChoiceItems(notificationListItems, notificationSelected ,new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int j, boolean isChecked) {
if(isChecked)
{
list.add(notificationListItems[j]); //add items
}
else if(list.contains(notificationListItems[j]))
{
list.remove(notificationListItems[j]);//remove items
}
}
})
//when person click on "ok" this statement is called
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int j) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //cancel the changes and turn of the dialog box
}
}).create();//this will create the dialog box
dialog.show(); //show the dialog box
return dialog; //return the dialog box to layout
}
}
This is styles.xml code.
<!-- Styling the alert BOX -->
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">#e4e4e4</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#64f24c49</item>
</style>
Create a alert dialog style in styles.xml:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColor">#color/white</item>
</style>
Then add the style to you current app theme style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Alert Dialog Style -->
<item name="android:alertDialogStyle">#style/MyAlertDialogStyle</item>
</style>
This is my code to show a custom dialog in android.
private void showCouponCodeDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialoge_apply_coupon);
dialog.setTitle(R.string.apply_coupon);
final ProgressBar progressBar =(ProgressBar) dialog.findViewById(R.id.progressBar);
Button btnApplyCoupon = (Button) dialog.findViewById(R.id.btnApplyCoupon);
btnApplyCoupon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
}
});
dialog.show();
}
How to set title of the dialog in center horizontally?
Set a custom style , set gravity as you want to the specific one
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
Assign the style to dialog
Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
do other stuff as normal
or set custom title manually
// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewname, null);
builder.setView(view);
TextView title = new TextView(this);
// You Can Customise your Title here
title.setText("Custom Centered Title");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
builder.setCustomTitle(title);
How to make an Alert Dialog in full screen in Android?
Try below code
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
you must create a custom style for your dialog
in your style.xml
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
while inflating your dialog set this theme
dialog = new Dialog(this, R.style.DialogTheme);
【Method 1 | Use Custom Style】
In styles file:
<style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar"> //no actionbar, but status bar exists
<item name="android:windowFullscreen">true</item> //remove status bar
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item> //smooth animation
<item name="colorAccent">#color/colorAccent</item> //change button text color
</style>
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.myFullscreenAlertDialogStyle); //second argument
【Method 2 | Use Built-In Style】
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
This method will make the button text become green color, you can use dialog.getButton().setTextColor() to change it.
if you're using DialogFragment to build AlertDialog you may set MATCH_PARENT on LayoutParams in onResume():
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
Can you help my figure out why is my alert dialog BLACK?!
Recently i changed my app theme to support material design, but my alert dialog got black!
Here is my create dialog Code:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivity.this);
alertDialog.setCancelable(true);
alertDialog.setTitle("sample");
alertDialog.setItems(new String[] { "a", "b" }, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
and its my main style and dialog style
<style name="MaterialTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:alertDialogStyle">#style/AppDialog</item>
<item name="android:alertDialogTheme">#style/AppDialog</item>
<item name="android:textColor">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:dialogTheme">#style/AppDialog</item>
<item name="colorPrimaryDark">#color/myPrimaryDarkColor</item>
<item name="android:textColorHint">#color/gray_1</item>
<item name="colorAccent">#color/myAccentColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/black</item>
</style>
<style name="AppDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
You don't set theme for AlertDialog, for using theme, change code to:
ContextThemeWrapper ctw = new ContextThemeWrapper(TestActivity.this, R.style.AppDialog);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctw);
you must use a light theme with your alertdialog builder such as the THEME_HOLO_LIGHT.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT);