how to make center MaterialAlertDialogBuilder alignment title, message & button - java

how to make center alignment title, message & button
MaterialAlertDialogBuilder(requireContext())
.setTitle("Message")
.setMessage("This is info message.")
.setPositiveButton("Ok") { dialog, which ->
}
.show()

For the the title and the message you can use:
<style name="MaterialAlertDialog__Center" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogTitlePanelStyle">#style/Title.Panel.Center</item>
<item name="materialAlertDialogBodyTextStyle">#style/Message.Center</item>
</style>
<style name="Title.Panel.Center" parent="MaterialAlertDialog.MaterialComponents.Title.Panel">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="Message.Center" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:gravity">center_horizontal</item>
</style>
with:
MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_Center)
.setTitle("Message")
..
.show()

You can create your own layout and add it to your dialog
val inflater=LayoutInflater.from(requireContext())
//Getting the custom view
val view=inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT,null)
//Obtaining the components
val title=view.findViewById(R.id.YOUR_CUSTOM_TILE)
title.text="Message"
val button=view.findViewById(R.id.YOUR_CUSTOM_BUTTON)
button.setOnCLickListener{
//onClick
}
val dialog=MaterialAlertDialogBuilder(requireContext()).setView(view).create()
dialog.show()

To center the title and message you only have to change this in your Theme
<item name="materialAlertDialogTheme">#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>

In the simplest way
MaterialAlertDialogBuilder(context, com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)

Change to this
MaterialAlertDialogBuilder(requireContext(), com.google.android.material.R.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered))
.setTitle("Message")
.setMessage("This is info message.")
.setPositiveButton("Ok") { dialog, which ->
}
.show()

Related

Text Color Not Changing in AlertDialogue

I am trying to apply custom color in AlertDialogue. Its changing text color as well background color in it but not changing SingleChoiceItems text color. You can see it black color in it with page number like below image.
I am using style like below code
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:textColor">#color/TextColorLite</item>
<item name="android:background">#color/colorPrimaryDarkLite</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">?attr/MainAccentColor</item>
<item name="android:buttonBarButtonStyle">#style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">?attr/TextColor</item>
<item name="android:textStyle">bold</item>
</style>
Java Code For Dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.AlertDialogTheme);
builder.setTitle("Go to Page:");
builder.setSingleChoiceItems(mPageOptions, mPageIndx - 1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
mOptionDiag.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
mOptionDiag = builder.create();
mOptionDiag.show();
What I should do for change this black text color ?
Thanks
I have got solved adding this items in style.
<!--For List Text-->
<item name="textColorAlertDialogListItem">#color/TextColorLite</item>
<!--For Radio-->
<item name="android:textColorSecondary">#color/TextColorLite</item>
Thanks
In your resources folder open styles.xml and add a custom theme like for example this:
<style name="radiobuttonstyle" parent="Material.Drawable.RadioButton">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/md_green_900</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/md_green_400</item>
<item name="colorControlNormal">#color/md_green_900</item>
<item name="colorControlActivated">#color/md_green_400</item>
</style>
and in your RadioButton in your layout add it as the theme of your RadioButton like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioyes"
android:theme="#style/radiobuttonstyle"
android:text="Yes"
android:textColorLink="#color/md_light_green_900"
android:textColorHighlight="#color/md_green_900"
android:textColor="#color/md_light_green_900" />
PS: that is my own custom radio button so it should work.

How to style a TabLayout

I'm trying to style text inTabLayout. I'd like to managed the styles such as font color, size and weight, change the text therein from being set to ALL CAPS
How can I go about it?
TabLayout tabLayout = new TabLayout(this);
tabLayout.addTab(tabLayout.newTab().setText("Upload"));
tabLayout.addTab(tabLayout.newTab().setText("Pics"));
tabLayout.addTab(tabLayout.newTab().setText("Vids"));
tabLayout.addTab(tabLayout.newTab().setText("Papers"));
I would like to attach the TabLayout to styles set in XML: something like:
Then I call it:
Button button = new Button(mContext);
button.setText("Add New BAG");
button.setTextAppearance(R.style.bttn_style);
button.setTextAppearance(R.style.custom_bttn_style);
res/values/styles.xml
<style name="custom_bttn_style" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#00600f</item>
<item name="android:textSize">18dp</item>
<item name="android:fontFamily">\'Zilla Slab\', serif</item>
</style>
Vielen dank im voraus.
You need to inflate a custom view, where you will add all styles you want
View view = LayoutInflater.from(this).inflate(R.layout.icon_tab, null);
AppCompatTextView tv = (AppCompatTextView) view.findViewById(R.id.tab);
ImageView imgTab1 = (ImageView) view.findViewById(R.id.imgTab);
imgTab1.setImageResource(R.drawable.icon_tab_search);
imgTab1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
tv.setText(titles[0]);
tabLayout.getTabAt(0).setCustomView(view);

Can't change AlertDialog items colors

I created multiple alert dialogs in different activities. Now all of sudden the AlertDialog items appear in red !
Here is my code
AlertDialog.Builder builder = new AlertDialog.Builder(this, MyDialogStyle);
builder.setTitle("Select Method :");
final String[] choices = new String[]{"Weight Based", "Body Surface Area"};
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on choice[which]
switch (choices[which]) {
case "Weight": {
create_item(false);
break;
}
case "Body Surface Area": {
create_item(true);
break;
}
}
}
});
builder.show();
styles.xml (v21)
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:colorPrimary">#000000</item>
<item name="android:colorAccent">#000000</item>
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearanceListItemSecondary">#000000</item>
<item name="android:textAppearance">#000000</item>
<item name="android:color">#000000</item>
styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>\
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearance">#000000</item>
</style>
I tried changing the parent style or deleting it, but both give the same bright red result ! any ideas ?
try use this style ,,, hope it'll work for you
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>

How to automatically apply material primary and accent colors to dialog

I have custom theme with changed primary and accent colors. But AlertDialog is not picking up those colors automatically when I create dialog using:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
This is what I get - dialog with default colors:
And this is what I would like to have - dialog with custom colors:
I can create custom AlertDialog theme and apply it during AlertDialog creation:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme_Dialog);
I would like to skip calling constructor with theme parameter.
Is there a way to force custom colors on all themes without deriving them in styles, including AlertDialog base theme?
Minimup API level I have to support is 15.
My activity code
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public void dialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
My customized style
<resources>
<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>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
My colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Add this in your the App theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">#style/AppTheme.Dialog</item>
And then,
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);

How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme?

I have black background with white letters.
Any ideas?
Thanks.
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:textColor">#android:color/white</item>
</style>
Normal:
Click in menu button, and show :
Modify the style like this:
<style name="PacerTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#android:color/white</item>
</style>
For "myCustomMenuTextApearance" style use for parent
#android:style/TextAppearance.Widget.IconMenu.Item
instead of
#style/Widget.AppCompat.Light.ActionBar.Solid
I found the solution, program change text color, as follows:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
for(int i=0; i<menu.size(); i++)
{
MenuItem mi = menu.getItem(i);
String title = mi.getTitle().toString();
Spannable newTitle = new SpannableString(title);
newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(newTitle);
}
return true;
}
The XML is as follows:
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
</style>

Categories

Resources