Different Themes in Android Studio - java

I am currently trying to create a settings menu for my app, where different theme colors (also gradients) can be chosen. When a color gets selected I want all parent layout backgrounds of activities and fragments to have this color. I thought about creating different styles in my theme.xml and based on the chosen color I apply that theme by using setTheme(...). But when I create a style in my theme.xml file by using <item name="android:background">#drawable/gradient</item> all backgrounds of the UI elements also have that background. Can someone tell me how I can create styles that only affect the parent layout background or suggest any other solution?

Related

Animated background for button

How to make animated background for button in Android Studio
Hello guys, I am an java android application developer. In my project, I want to use a button with an animated background. What does it mean? The button class has a method .setBackgroundResource(int res_id), where do we pass R.drawable.some_res. I have video(R.raw.some_video). Can I use this video as a background for a button, and so that when it ends, it starts playing again. And another question is whether this background will consume a lot of phone resources.
To create an animated background for a button in Android Studio, you can use an animation drawable.
Create a new XML file in the res/drawable folder and give it a descriptive name, such as "button_background.xml".
In the XML file, add the following code:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/frame_1" android:duration="50"/>
<item android:drawable="#drawable/frame_2" android:duration="50"/>
<!-- Add additional frames as needed -->
</animation-list>
Replace "frame_1" and "frame_2" with the names of the drawables you want to use for each frame of the animation.
In your button's Java code, set the background of the button using the following code:
button.setBackgroundResource(R.drawable.button_background);
Note that the "android:oneshot" attribute is set to "false", which means that the animation will repeat indefinitely. If you want the animation to play only once, set this attribute to "true".
Keep in mind that using animations as background resources can consume more system resources than using static images, so be mindful of the size and length of the animation.

How to create a simple custom color picker on android studio?

I am trying to replicate the picture above for my app, but I am very confused and don't know how to proceed.(I'm new to android studio :/) I am trying to figure out how to know which color the user selects
So far, I have all the custom color drawable circles on my xml file but how do I know which color the user selected?(adding the check symbol) I'm thinking of putting a hidden check mark on each circle in my xml file, but only let it appear when clicked...but what would happen if the user clicked multiple colors?? I feel like there is a simpler way to approach this
I would really appreciate if someone could put me in the right direction for thinking about this, or if you could provide links that would help me learn how to do this.
First thing first, you would need to use RadioButton for this use case. Since the Radio buttons have inbuild mechanism of selecting only one element at a time. You can read more about RadioButtons at https://developer.android.com/guide/topics/ui/controls/radiobutton .
In case you need select mutiple colors at a time, then use Checkbox. (Read more about checkbox at https://developer.android.com/guide/topics/ui/controls/checkbox)
Now, checkbox/radio buttons can take care of selection part, now we would need to make the UI updates based on whether the color is selected or not. For this use case, we can use StateDrawable, which can change it's UI based on the state (selected/unselected).
Following is an example of StateDrawable for checkbox/radio button:
File location: app/resources/drawables/color_picker_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check_mark_image" android:state_checked="true"/>
<item android:drawable="#drawable/transparent" />
</selector>
The above drawable will show check_mark_image for checked state and transparent for unchecked state.
You can create this type of state drawable file for each color (this will allow to render different color checkbox based on requirement/color).
Or you can set the colors (blue, red, etc.) as background color to your checkbox and create only one state drawable file and reuse it for all based on your requirements.
You can assign this drawable to your checkbox as follow:
<CheckBox
...
android:button="#drawable/color_picker_item"/>
This way you can build your color picker.

What's the most efficient way to change the overall theme of the app?

So I got an idea for an app that would change visual theme based on user's selection, something like how a Sub-Reddit would have the option for users to switch between themes. In this case, I would utilize at least 4 themes, each theme changing the color of certain views, such as the background, buttons, image, and etc. I would like to know the the best approach to this. Do I need to keep a list of views that would be affected or something?
I've tried keeping different button backgrounds with different color since setting background color programmatically would reset the background shape back to the default design, but I'm afraid that it will cause the app to be bloated with numerous files. I've tried using the color filter to change the views.
Color id still keeps the filter applied to it, causing it be unusable if user switch to different color, then back.
Hopefully, this question was directed at native development. If so, then you should take a look at the guidelines. You can create your themes in XML and reference them in your layout XML files.
Personally, I would store the theme as a SharePreference somewhere and then when laying out each Activity/Fragment, utilize the user's saved theme.

Why does my android emulator have different background than main.xml ?

So, I have a holo light theme, I am developing for android 3.0 and in my main.xml graphical layout shows a black screen which is what i want, but when I run the app on my android emulator version 3.0, it has a white background.
I want to have a black background, but main.xml layout shows me black background only the emulator shows different, why ????
Thanks, I have been developing for a week, but it's quite annoying. Thanks !!
The key is to customize Android Graphical XML Editor(Lets call it AGXE) properly to match the exact outcome of your program. For this purpose there are several ways of customizations are available.
In your case you are using Holo.Light the for your application, in your AGXE theme customization(in the light blue highlight) might be Holo/Theme.Holo (or something else) which has a black appearance for blank space.
If you want to know how your app layout will look in Holo.Light theme you have to manually set your theme to Holo.Light/Theme.Holo.Light
There are other controls in the AGXE, which can be used to get the most accurate representation of your layout in real life/emulator you specified (highlighted)(If you want explanations for each ask in comments below).
If you want to set a black background as Theme.Holo, you just have to put a single line code in all your layout android:background="#000000" which will set your entire background as black instead of white (even in Theme.Holo.Light), or you can specify your theme as Theme.Holo.
Read more here

Change app background

I'm currently making an android app in which I'd like the user to be able to change the background image.
I've got 3 images and a screen where I can choose a picture and a button for applying.
The issue:
I can allow the user to see all images in the way I want, but I don't know how to set the selected image as app background.
What I want to do:
I want the user to click a button, which exports the selected image to "bakgrund.png" in "/res/drawable-mdpi" and replaces the current one. This would allow me to easily integrate the background switcher. Renaming of current files also works.
PS: My current background images are located in /res/drawable-mdpi named 1.png 2.png and 3.png.
Easiest way would be to call the setBackgroundResource(image_id) method on the root layout. Like if you have a LinearLayout which has android:id="#+linear" as the root layout in the layout xml, then this code will help:-
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linear);
linear.setBackgroundResource(R.drawable.1);//call this in the OnClickListener's OnClick Method
Firstly, you need different themes which has different backgrounds. So you may use this.setTheme method in your Activity.
Indeed I suggest you, two different layout (with different backgrounds but have same components) and using setContentView during onClick.
I hope it solves your issue.

Categories

Resources