AlertDialog change positive button color - android

I want change colour for positive button in Alertdialog, and here is what i'm doing below:
// style.xml
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:buttonBarPositiveButtonStyle">#style/positive</item>
</style>
<style name="positive">
<item name="android:textColor">#color/accent</item>
</style>
I use the style by:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogCustom);
But what i'm doing is not working, i just want to change positive button colour. I don't want change the button's colour in java code.
Thanks in advance :-)
Edit-1
Alertdialog layout xml
<android.support.v7.internal.widget.ButtonBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="bottom"
app:allowStacking="#bool/abc_allow_stacked_button_bar"
style="?attr/buttonBarStyle">
<Button
android:id="#android:id/button3"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.widget.Space
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<Button
android:id="#android:id/button2"
style="?attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#android:id/button1"
style="?attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Attention:The solution is not recommended, because this is actually a hacky solution. It's not following android design and shouldn't be used. And I change positive button color via java. Thanks to #Divers for pointing this.
styles.xml
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarPositiveButtonStyle">#style/positive</item>
</style>
<style name="positive">
<item name="android:textColor">#color/accent</item>
</style>
Usage
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogCustom);
Maybe here is one point you should be attention to, buttonBarPositiveButtonStyle not android: buttonBarPositiveButtonStyle.

builder.setOnShowListener( new OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(COLOR_YOU_WANT);
}
}
Hope it helps! Cheers!

There is no way to do it via styles, because in general it's wrong from UI point of view. If you want to do that anyway, just do via java.

Related

How do I add back the transparent background around my dialog activity with a custom theme

I have an activity and I've set it's theme in the manifest to a custom dialog theme.
But now the transparency around the dialog box is gone. Everything is black.
Here's what it looks like with my custom theme:
I want it to look like this:
But with a custom background and text color, like in the first picture.
Here's the part of the manifest with the activity's theme set to my custom theme, as in the first picture:
<activity
android:name=".activity.LockScreenActivity"
android:exported="true"
android:label="#string/aplicacao_bloqueada_title"
android:theme="#style/dialog_theme"/>
Here's the part of the manifest with the activity's theme set to android's dialog theme, as in the second picture:
<activity
android:name=".activity.LockScreenActivity"
android:exported="true"
android:label="#string/aplicacao_bloqueada_title"
android:theme="#style/Theme.AppCompat.DayNight.Dialog"/>
Here's my xml with my custom theme:
<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:textColor">#color/white</item>
<item name="android:windowBackground">#color/dark_gray</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">50</item>
</style>
Here's my activity class:
public class LockScreenActivity extends AppCompatActivity {
// Notification activity that is called after too many failed login attempts
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
finishAndRemoveTask();
System.exit(0);
}
}, 5000);
}
}
Layout for my activity:
<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="wrap_content"
tools:context=".activity.LockScreenActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="214dp"
android:layout_height="136dp"
android:gravity="center_vertical"
android:padding="20dp"
android:text="#string/aplicacao_bloqueada_text"
android:textAlignment="viewStart"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
What's the best way to do this?
Edit: As nima sugested, I tried adding the following line to the onCreat method of my dialog themed class:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
But it turned everything transparent, including the pop up window.
See picture:
How do I solve this?
Edit Your Code Like Bellow:
1-In style
<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
2-In Manifest
<activity
android:name=".activity.LockScreenActivity"
android:exported="true"
android:theme="#style/dialog_theme"
android:label=""
/>
3-activity_lock_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/cardview_dark_background"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="170dp">
<TextView
android:id="#+id/textView2"
android:layout_width="214dp"
android:layout_height="0dp"
android:layout_weight=".2"
android:gravity="center"
android:padding="20dp"
android:text="Application Blocked !"
android:textColor="#color/white"
android:textSize="16sp"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="214dp"
android:layout_height="0dp"
android:textColor="#color/white"
android:layout_weight=".4"
android:gravity="center_horizontal"
android:padding="20dp"
android:text="#string/aplicacao_bloqueada_text"
android:textSize="16sp" />
</LinearLayout>
As #nima pointed out you can use getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); to make the background transparent.
This will introduce a couple of issues:
Transparency of everything
Fix this by setting a white background to the ConstraintLayout root layout.
Transparent activity title
Fix this by removing the title from the manifest and set it to an empty one in activity with setTitle(""); and add another TextView to represent the title in the layout:
Remove the title (android:label) from the activity in the manifest:
Also you should android:exported="true" as it seems that this activity should be exported to other apps:
<activity
android:name=".activity.LockScreenActivity"
android:theme="#style/Theme.AppCompat.DayNight.Dialog"/>
Set the title & background:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
// ...........
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setTitle("");
}
Layout:
<?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="wrap_content"
android:background="#color/white"
android:paddingHorizontal="16dp"
android:paddingTop="16dp"
tools:context=".LockScreenActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Application Blocked"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="#id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="214dp"
android:layout_height="136dp"
android:gravity="center_vertical"
android:padding="20dp"
android:text="#string/aplicacao_bloqueada_text"
android:textAlignment="viewStart"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>
UPDATE:
When I use my custom theme, as opposed to Theme.AppCompat.DayNight.Dialog, I still get the black screen
This is because the android:backgroundDimAmount is set to 50 in your custom theme; the valid range for this attribute is 0 through 1; any value greater than that will be considered 1...
0 means there is no dimming (completely transparent, and 1 meaning 100% opaque/black.
So to fix this issue, I guess you mean by 50 as the half way, so you'd set it to 0.5 instead:
<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:textColor">#color/white</item>
<item name="android:windowBackground">#color/dark_gray</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
</style>
Your backgroundDimAmount is too high try some smaller number like 0.5
So your dialog_theme would look something like this.
<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:textColor">#color/white</item>
<item name="android:windowBackground">#color/dark_gray</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
</style>

How to use default android themes?

Hy
I created an android project, and I saw thet, tehe are two themes.xml file. In emulator, when I switch day and night mode, my application color schema is changes too.
I thought it is greate, because with thease themes it will be easy to create a day/night app, but I don't know how it works.
How can I define a new color item inside themes, and use this schema for layout background?
For example: There is an item in both themes
<item name="colorPrimary">#303F9F</item>
And I would like to create a new item for both themes.xml, and use it to set up fragment background color, but the intellisense doesn't find this element directly.
<item name="backgroundColor">#303F9F</item>
You can change color easily from where you define your theme in 'styles.xml'.
<resources>
<!-- Base application theme. -->
<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="AppThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorSecondary</item>
<item name="colorPrimaryDark">#color/colorSecondaryDark</item>
<item name="colorAccent">#color/colorSecondaryAccent</item>
</style>
I have two themes in my app so if i change the color or anything in my theme i will define that color in my selected theme for example:
i want to change the background color of 'AppThemeDark' i just define the color like this
<item name="background">#color/"#fff67"</item>
copy this line in your first screen
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Ok In constraintlayout background tag is not worked so I will fix this problem with the use of View.
<android.support.constraint.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="#AAA">
<View
android:id="#+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FFF"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toTopOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toBottomOf="#+id/textView1"
tools:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toBottomOf="#+id/textView2"
tools:text="TextView" />
</android.support.constraint.ConstraintLayout>

Setting spinner popup background color

I can set the background of the popup menu of a spinner using:
<Spinner
android:popupBackground="#color/darkThemeBackgroundColor"/>
But how do I set the background text color of it?
This solution is more simpler, you only need add this code to styles.xml
<style name="spinnerTheme">
<item name="android:background">#color/colorDark</item>
<item name="android:textColor">#android:color/white</item>
<!-- this is for api 19 text color -->
<item name="android:color">#android:color/white</item>
</style>
And this is how use in your layout file
<Spinner
android:id="#+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:entries="#array/aves"
android:theme="#style/spinnerTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
That is the result:
I hope this solution helps you, greetings.

Wrap button around icon?

how can i remove that gap after the icon? how can i Wrap button around icon normally?
<Button
android:layout_marginStart="4dp"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
app:icon="#drawable/googleg_standard_color_18"
app:iconTint="#null"/>
I dont see any direct way of doit it but did some tweak to help you out
Create Button without wrap content give some width as per your design look and feel.
<Button
android:id="#+id/button"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_google_hangouts"
app:backgroundTint="#android:color/white" />
In code do the following changes and play with adjusting padding per your requirement.
val button = findViewById<Button>(R.id.button)
button.setPadding(24,0,0,0)
the image dimension which I used for testing is 24dpX24dp
my final result:
You can use a ImageButton as suggested in the comments
You can use the MaterialButton with this style:
<style name="Widget.MaterialComponents.Button.IconOnly">
<item name="iconPadding">0dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingRight">12dp</item>
<item name="android:minWidth">48dp</item>
<item name="android:minHeight">48dp</item>
<item name="iconGravity">textStart</item>
</style>
Something like:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.Button.IconOnly"
app:icon="#drawable/...."
app:iconTint="#null"/>

Transparent background on custom Dialog [duplicate]

I have a custom AlertDialog style that makes the AlertDialog box transparent. It works fine except that when I inflate my desired transparent layout into the alert dialog window, it shows up with a black background. My goal is to have a completely transparent AlertDialog to where it seems as if there are only 4 buttons floating rather than a frame.
Image one is what the custom dialog is giving me, image two is my desired or what I am aiming for.
Here is the code to the custom Dialog
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#android:color/transparent</item>
</style>
Here is what I am calling in my onCreate()
AlertDialog.Builder imageDialog = new AlertDialog.Builder(TimeLine.this, R.style.CustomDialog);
inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.tabs, null);
AlertDialog a = imageDialog.create();
a.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
a.setView(view, 0, 0, 0, 0);
a.show();
Edit*
The code for the tabs layout xml is here
`
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#000000ff">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_marginTop="206dp"
android:layout_below="#+id/button4"
android:layout_alignStart="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="100dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
`
From testing to see what the source of the problem is, I have found out that it is true that the layout is transparent because when I change the background color of the layout, so does the alert dialog change. However when the layout is set to transparent, it seems that what is behind the inflated layout is black. And because of that I am unsure of what to do or whether it is the AlertDialog settings or my layout code.
The problem is that AlertDialog builder is actually not good for designing transparent dialog and will and always have this black background which is actually a Theme for it, instead use the Dialog to create a transparent theme instead.
sample:
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
Using Dialog does not require any theme manipulation for transparent background so it is basically easy.
Have you tried using something like this:
<style name="CustomDialog" parent="#android:style/Theme.Translucent">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
...
</style>
EDIT
Try this in java code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Those who are having still problem creating custom transparent dialog or alert dialog, can use this combination. I also wanted to show custom background this is what worked for me.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
example:-
/**
* alert dialog
*/
public class ToolTipView extends AlertDialog {
public ToolTipView(#NonNull Context context) {
super(context);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_tool_tip_view);
}
}
In your R.layout.tabs,
Replace
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#000000ff">
with
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#android:color/transparent">

Categories

Resources