Changing Spinner Highlight AND line color - java

This XML below results in my Spinner having an orange line across the bottom and an orange rectangle on the right-hand side. I have two questions:
How can I make it so that the orange rectangle on the right-hand side is an orange triangle, like the default one associated with Theme.Dialog?
Now that I changed the look of the Spinner, when I click on it it no longer gets highlighted. I want to make it look the way I want AND highlight to a lighter color of orange when I press on it.
Do I have to accomplish both things in the same XML file? I've already got the drawable below set to the background of my spinner, so I can't declare the background twice right?
Any ideas? Thanks in advance!
My Drawable Custom Spinner Layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
<!-- main color -->
<item android:bottom="1.0dp"
android:left="0.0dp"
android:right="10.0dp">
<shape >
<solid android:color="#color/white" />
</shape>
</item>
<item android:bottom="25.0dp">
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
</layer-list>

You could refer to this and get the spinner drawables according to your wish.
Hope that helps

Related

How to make a fade black background in Android Studio

I would like to put a fade black covering my image, just like this one, with a text, a fade background and then my image. I just want to know how to implement this fade. Thank you!
Just create a drawable file, I'll name it gradient_background (you can set any name)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#00000000"
android:endColor="#00000000"/>
</shape>
then inside your target xml, create a View like this one, look that inside it, we're setting the background as your drawable file. In this case is gradient_background
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_background"
app:layout_constraintBottom_toBottomOf="post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="post_image"
app:layout_constraintTop_toTopOf="#+id/post_image" />
So it should look like this:
:)
First, Make bottom_transparent_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270"
android:endColor="#64000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</selector>
Then, add that drawable as your view background in which you want to show the fade background effect.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_transparent_gradient"/>
And that's it! You can see the result you wanted. :)

how to do pop up button animation in android studio

In a calculator app that I am creating, I want buttons to animate in a pop-up way (scaling-up) when the button is clicked. But that is not the issue. I want the button to stay scaled up (bigger size) as long as the user hold the button. It is supposed to go back to its original size (small size) only when the user releases the button. In the same way button colour has to change from white to blue as long as user hold the button(back to white when released). Is there any way to do that? I am not very good at animations in android studio. So please try to explain it in simple words.
I am also attaching a video that I made for prototyping to illustrate it.
Click link below
https://drive.google.com/file/d/13B8LAuNW1ymhmliw52BEIkMbJAJQRbfw/view?usp=sharing
You should create an xml file that have a selector as root element. This element selects the shape in base of the corrent state like pressed, focused or enabled.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/button_enabled" />
</selector>
If you want a different shape with different color and size you must create a new drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
If you want to change the size you can use the attribute size.
Then you must implement the on click listener in the mainActivity onCreate file and set the pressed state of the button to true.
In a nutshell you must create one selector drawable for the button and two drawable one for the state pressed="true" and one for pressed="false".
Here I put an exaustive example: https://stackoverflow.com/a/29848987/13198061

my application ignores any changes in background color of buttons

i'm trying to customize my alert dialog. but i can not change the background color of buttons.
i tried drawable button designing, style, setting direct color in xml and also setting color by java codes in the activity but i could not change it.
the shape of the button changes but the color only can be either the default color or transparent color.
the xml coding of button itself:
<Button
android:id="#+id/btnDenied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="#drawable/button_selected" //// or #drawable/button_costume
android:text="#string/button_no" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed = "true">
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</item>
<item android:state_pressed = "false">
<shape>
<solid android:color="#FFC107"/>
</shape>
</item>
</selector>
and another drawable file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="50dp"/>
<solid
android:color="#color/colorHeader"/>
</shape>
and i also tried this style:
<style name="Button_Style"
parent="#android:style/Widget.Button">
<item name="android:textSize">14sp</item>
<item name="android:background">#color/colorHeader</item>
<item name="android:textColor">#1B1B22</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">2dp</item>
</style>
but non of them could change the color of the button.
The code that I have written is 100 percent correct, but sometimes Android Studio does some wrong tasks, so solve these issues either by running the program again, or doing the following steps:
Clean Project , From the build menu .
Rebuild Project .
And I got this problem with me not to show errors in xml file,
I hope this thing helped you.

Set background color of MaterialSpinner

I am using this (https://github.com/ganfra/MaterialSpinner) for my spinner in my project. The problem is i did't find any way to change the spinner's background. I've try to use android:background="#fff", then try to use android:theme="#style/myspinnertheme" which is changeing the background but still did't work. I don't know another way i can try :D
<fr.ganfra.materialspinner.MaterialSpinner
android:id="#+id/spCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#fff"
app:ms_baseColor="#color/colorAccent"
app:ms_floatingLabelText="Kategori"
app:ms_enableErrorLabel="true"
app:ms_hint="Pilih Kategori : "/>
This is my project screen shoot :
Look at the spinner at bottom, i want to change the background color to white, what shall i do?
It happens because material spinner set background resources in constructor and changes your background. So just reset background resources with your color. Example:
Create background.xml in drawable( colors for default, pressed and selected spinner's state)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray"/>
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/black"/>
</shape>
</item>
And set it
MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spCategory);
spinner.setBackgroundResource(R.drawable.background);
You can use custom:ms_baseColor="#color/green"

Why does the android:src behaves weirdly on padding in drawable?

I recently did some test on android drawable and I found that the android:src behaviors to padding is extremely weird compare the android:background.
Here is my drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
<padding
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
android:left="1dp"
/>
</shape>
</item>
<item android:top = 2>
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
Inside of the mainActivity layout I have another image view which I used to test. I set the image view to 40dp width and height. Then I tested the android:src vs the android:background attribute by setting them to my drawable above. Here is the result:
Android:src
Android:background
The android:background as you can see is the expected behavior because I move the white circle down 2 and it has padding that obeys the pink oval padding. Now the android:src is straight up unexpected because 1st of all the white oval which is the layer that is above the pink oval is not shown. Second of all, the shapes change to a flatten oval.
Any clue to why this is the case??????
src works with android:scaleType where you can give different options ScaleType
whereas background always takes scaleType of FitXY covering full region for that view.
if you want the same behaviour as background with src,
put android:scaleType="FitXY". there are other options you should check also.

Categories