How do i change the text color depending on the theme in Android Studio with setTextColor()?
So that when dark mode is enabled it changes the Text to white and when white mode is enabled it changes to black
this may be not exactly what you want but it might help you.
it doesn't matter what layout you use i'm just going to provide dummy code so you can understand.
Blockquote
Activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_gravity="center"
android:textColor="#color/colorPrimaryDark"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
note : In order to change the color depending upon the current theme light or dark, it has to be dynamic.
now implement Dark theme:
create a new resource file and call attributes.xml, inside this file we will be declaring some attributes that we need for setting colors for widgets and layout.
and think of these attributes as controllers for colors in our layout.
add this snippet to attributes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="metaColor" format="color"/>
<attr name="color" format="color"/>
<attr name="textColor" format="color"/>
</resources>
After that, we need to create two theme, light and dark in styles.xml file
inside styles.xml, here we make use of our attributes, add this code snippet:
<style name="Light" parent="AppTheme">
<item name="textColor">#000000</item>
<item name="metaColor">#606060</item>
<item name="color">#ffffff</item>
</style>
<style name="Dark" parent="AppTheme">
<item name="textColor">#ffffff</item>
<item name="metaColor">#aeaeae</item>
<item name="color">#000000</item>
</style>
then go to activity_main.xml
update it with this code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:textColor="?attributes/textColor"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="?attributes/metaColor"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
now go to MainActivity and add this inside of onCreate function like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Dark);
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Light);
setContentView(R.layout.activity_main);
}
and here is a really cool repository to use to create your custom themes and change them dynamically with ripple animation:
https://github.com/imandolatkia/Android-Animated-Theme-Manager
Related
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>
I am trying to get a button custom attribute to return true/false using custom attributes defined in styles.xml. My example is trivial with only two buttons but I can't get it to work.
My layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:orientation="vertical"
tools:context=".CustomAttr">
<Button
android:id="#+id/button_1"
style="#style/red_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1!" />
<Button
android:id="#+id/button_2"
style="#style/blue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2!" />
</LinearLayout>
<resources>
styles.xml looks like:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.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>
<attr name="is_red" format="boolean"/>
<style name="red_button" >
<item name="android:background">#ffff0000</item>
<item name="is_red">true</item>
</style>
<style name="blue_button" >
<item name="android:background">#ff0000ff</item>
<item name="is_red">false</item>
</style>
</resources>
And the code looks like:
public class CustomAttr
extends AppCompatActivity
implements View.OnClickListener {
private static final String TAG =
CustomAttr.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_attr);
findViewById(R.id.button_1)
.setOnClickListener(this);
findViewById(R.id.button_2)
.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
int [] searchAttr = {R.attr.is_red};
TypedArray attrs
= v.getContext()
.obtainStyledAttributes(null, searchAttr);
boolean isRed = attrs.getBoolean(0, false);
Log.d(TAG, String.format("%s:%b"
, (id == R.id.button_1) ? "button 1" : "button 2"
, isRed )
);
}
}
Everything compiles fine and the button colors are working and I'm not getting any warnings. But the boolean isRed in the onClick method always returns false.
I've been looking through the net and docs all day and this looks like it should work -- but it doesn't. What am I doing wrong?
Thanks
Steve S.
********* EDIT Fri Sep 21 10:17:01 PDT 2018 *********
As noted below, this is a prototype for an app with about 250 different buttons in a gridview. There are about 250 of basicly 4 different types and I can set almost everything in each button using one of 4 different styles. I was already using the tag and text fields and really needed a way to detect the button's type (1 0f 4). I finally created a custom button view with it's own attribute set. The code for the working prototype custom view button with its attribute on github. Thanks!
Steve S.
You haven't used the is_red attribute in your xml. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="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:orientation="vertical"
tools:context=".CustomAttr">
<Button
android:id="#+id/button_1"
custom:is_red="true"
style="#style/red_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1!" />
<Button
android:id="#+id/button_2"
custom:is_red="false"
style="#style/blue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2!" />
</LinearLayout>
Since my popup background is white in color I need to change the color of the spinner divider. I have tried styling the spinner in the following way but it does not work:
styles.xml
<style name="applicationTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
</style>
<style name="SpinnerStyle" parent="android:Widget.ListView.DropDown">
<item name="android:divider">#0193DE</item>
<item name="android:dividerHeight">1dp</item>
</style>
main xml
<Spinner
android:id="#+id/year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="#drawable/apptheme_spinner_default_holo_dark"
android:layout_marginLeft="75dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:spinnerMode="dropdown"
style="#style/SpinnerStyle"
android:popupBackground="#FFFFFF" />
java
ArrayAdapter<Integer> adapter_year = new ArrayAdapter<Integer>(this, R.drawable.custom_spinner_holidays, year);
adapter_year.setDropDownViewResource(R.layout.custom_spinner_popup);
custom_spinner_holidays.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white" />
custom_spinner_popup
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#0193DE" />
Can I just combine all these into one?
You need to put this theme in your manifest file like this:
<activity android:name="com.example.activity.Solution"
android:theme="#style/applicationTheme">
</activity>
Maybe the best idea it's doing your own spinner and you can style it in the way that you want. Take a look in the next thread it was so useful for me.
How to customize a Spinner in Android
How to set the text size, text color and other parameters in one xml and than assign them to the imagebutton in android?
I can set the parameters manually for each button, but if there are more than 3 buttons it becomes a nightmare. What is the best practices for doing this?
You can use styles for that
http://developer.android.com/guide/topics/ui/themes.html
Create a style depending on your requirement's add that to your button like this style="#style/your_style". look at this tutorial Styles and Themes
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="#style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="#style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
I have this nice dialog view I set my UserInputDialog class to:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/nameMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your name Captain?"
>
</TextView>
<EditText
android:id="#+id/nameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK">
</Button>
<Button android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
I want my dialog to show up but for the background to not fade out. Is this possible? Cause the view that calls this dialog has a neato background I would like to be shown as a backdrop to the dialog.
I found this online:
<style name="doNotDim" parent="#android:style/Theme.Dialog">
<item name="android:backgroundDimAmount">0</item>
</style >
but not sure how to apply that to my dialog? I have a class called public class UserInputDialog extends Dialog implements OnClickListener. It sets its content view to the layout described above.
I think I am doing this all right, just not sure how to add that style so I can NOT fade the background.
Secondary question: Can you get new looks on your dialog (by say having an image or icon display with your text there) by using Themes?
You can also remove the dim effect by code with the following:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Create a res/values/styles.xml file and add this to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.DoNotDim" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
And apply the theme to your activity.
<activity android:name=".SampleActivity" android:theme="#style/Theme.DoNotDim">
Create a custom style and place it in your values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomStyle" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
To set a style to an individual dialog you can use
Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
For displaying dialog like TrueCaller do this:
In styles.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="android:Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
In AndroidManifest.xml do this:
<activity android:name=".SampleActivity" android:theme="#style/myBackgroundStyle">