Android Dialog title alignment - android

How to center vertical the "Login" title?
This is the style:
<style name="ABGAlertDialog" parent="#android:style/Theme.DeviceDefault.Dialog">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorHint">#android:color/darker_gray</item>
<item name="android:background">#color/corporativo_red</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:maxLines">1</item>
<item name="android:textSize">20sp</item>
<item name="android:bottomOffset">5sp</item>
</style>
I've tried gravity, textAlignment and some others items with no result.

You can't centre a normal alert dialog's title. You will need to create a custom dialog preferably with a dialog fragment or dialog activity depending on the functionality you need.

Set your text style in the android:textAppearance
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:bottomOffset">5sp</item>
<item name="android:textAppearance">#style/YourTextStyle</item>
</style>
Change both text and background color in title bar (Android)?
Textview: using the dialog title style: #android:style/TextAppearance.DialogWindowTitle

If Alert dialog is normal without any of your custom layout or theme than you ca get object by passing id with android namespace
AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity());
alertDialogBuild.setMessage("message text align center");
alertDialogBuild.setTitle("Title text Align Center");
alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = alertDialogBuild.show();
TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android"));
title.setGravity(Gravity.CENTER);
TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android"));
message.setGravity(Gravity.CENTER);

static void centerTitleHorizontally(final TextView title)
{
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
try {
title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} catch (NoSuchMethodError x) {
title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect bounds = new Rect();
title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds);
title.setPadding((title.getWidth() - bounds.width()) / 2, 0, 0, 0);
}
});
}

Related

How to set title of the dialog in center horizontally in android

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?

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);
}

change popup menu header style

I have a android.View.ContextMenu, which pops up after clicking an item in a recyclerview. It looks like this:
My problem is, that I don't know how to change the color of the header (here: 12345678) and the divider.
I tried to create a PopupMenuStyle, but I'm only able to change the items, not the header. I use Theme.AppCompat.Light.DarkActionBar as a parent for my theming
EDIT 1:
With this I can set the headertextcolor to primary text color, but the divider still stays blue, regardless of which items I add.
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
</style>
EDIT 2:
Context Menu-creation
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
menu.setHeaderTitle("12345678");
}
Styling
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:alertDialogTheme">#style/AppCompatAlertDialogStyle</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- I tried a lot here, but nothing works -->
</style>
Changing Divider Color:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
.setIcon(R.drawable.ic)
.setMessage(R.string.dialog_msg);
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));
Changing Text Title Color:
public static void brandAlertDialog(AlertDialog dialog) {
try {
Resources resources = dialog.getContext().getResources();
int color = resources.getColor(...); // your color here
int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
alertTitle.setTextColor(color); // change title text color
int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
titleDivider.setBackgroundColor(color); // change divider color
} catch (Exception ex) {
ex.printStackTrace();
}
}

Appcompat Alert Dialog Action button background On pressed state

I am trying new AlertDialog from appcompat v7 22.1.1.
It works pretty well (In all android versions) as in image.
Style for AlertDialog is this. (For now I am using hardcoded color values instead of color resources)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#111111</item>
<item name="colorPrimary">#00ddff</item>
<item name="colorAccent">#0044aa</item>
<item name="colorButtonNormal">#00aaaa</item>
<item name="colorControlHighlight">#00ddff</item>
<item name="alertDialogTheme">#style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">#0044aa</item>
<item name="android:background">#ffffff</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:windowTitleStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle">
<item name="android:textColor">#0044aa</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Title</item>
</style>
My question is ,
1) how to change statePressed color which is rounded (Gray) in the image ?
2) No pressed color is there in android >= 21 , what is hack for this ?
3) How can I have different colors of action buttons (Is it possible)?
Any help would be great.
You can use style attributes like
buttonBarButtonStyle
buttonBarNegativeButtonStyle
buttonBarNeutralButtonStyle
buttonBarPositiveButtonStyle
Example:
<style name="dialog_theme" parent="Theme.AppCompat.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/dialog_button.negative</item>
<item name="buttonBarPositiveButtonStyle">#style/dialog_button.positive</item>
</style>
<style name="dialog_button">
<item name="android:textStyle">bold</item>
<item name="android:minWidth">64dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:background">#drawable/dialogButtonSelector</item>
</style>
<style name="dialog_button.negative">
<item name="android:textColor">#f00</item>
</style>
<style name="dialog_button.positive">
<item name="android:layout_marginLeft">8dp</item>
<item name="android:textColor">#00f</item>
</style>
Where dialogButtonSelector is our custom drawable selector.
Unfortunatelly setting background on dialog_button destroy our paddings and margins so I need to set it again.
dialog_button style can inherit through Widget.AppCompat.Button.ButtonBar.AlertDialog but I found that it has missing styles like textStyle bold.
I have the Answer for the 3rd Questions
( How can I have different colors of action buttons (Is it possible)? )
Code:
// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("AlertDialog Title");
...........
.........
//Build your AlertDialog
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();
//For Positive Button:
Button b_pos;
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
b_pos.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
b_neu.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
b_neg.setTextColor(getResources().getColor(R.color.YourColor));
}
Happy Coding :)
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Title"); builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
Button nbutton = alertdialog.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));
Button pbutton = alertdialog.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackground(getResources().getDrawable(R.drawable.btn_press_white_rect));
**btn_press_white_rect.xml**
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/rounded_rect_yellow" ></item>
<item android:state_pressed="false" android:drawable="#drawable/rounded_rect_white" ></item>
</selector>

Title of the dialog is too high

How can I reduce the height of the green title?
Activity:
final Dialog dialog = new Dialog(EditorActivity.this, R.style.dialog);
dialog.setContentView(R.layout.preference_timerange);
dialog.setTitle(getString(R.string.zmien_czas_wyprawy));
Styles.xml
<style name="dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
<item name="android:windowBackground">#color/dialog_background</item>
<item name="android:textColor">#color/dialog_text</item>
</style>
<style name="dialog_title_style" parent="android:TextAppearance.WindowTitle">
<item name="android:textSize">16dp</item>
<item name="android:background">#color/dialog_title_background</item>
<item name="android:textColor">#color/dialog_title_text</item>
</style>
I don't know how to change the height of the Dialog's title, but you can remove the whole title and create a smaller one in your layout preference_timerange.xml:
final Dialog dialog = new Dialog(EditorActivity.this, R.style.dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.preference_timerange);
// Set R.string.zmien_czas_wyprawy as the title in your layout now

Categories

Resources