This is how the button looks right now:
android:background= "#color/white" does not change anything.
Code:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Button"
android:background="#color/white"/>
Use app:backgroundTint="#FFFFFF"
If you want to use a selector, you will need to have android:background="#drawable/your_button_selector_id" as the button background for the selector to work.
Selector:
<selector xmlns:android="schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_selected="true" />
<item android:drawable="#color/black" android:state_selected="false" /> </selector>
Since you are using a color just use the app:backgroundTint attribute:
<Button
app:backgroundTint="#color/white"/>
If you just need rounded corners with a stroke, you don't need a drawable.
For it use:
<com.google.android.material.button.MaterialButton
app:cornerRadius="16dp"
app:backgroundTint="#color/white"
app:strokeColor="#color/black"
android:textColor="#color/black"
app:strokeWidth="2dp" />
Did you add value for white in your colors.xml?
if not open colors.xml and add
<color name="white">#FFFFFF</color>
Full Explanation here
https://stackoverflow.com/a/2749027/7174681
Related
I have created a xml file called round_button. It's a simple circular button with a gradient. Here is the simple code
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3000dip" />
<gradient android:type="linear" android:startColor="#8dbab3" android:endColor="#0DCAAC" />
</shape>
And that's the output:
Here is the code of the xml file of my main activity.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/city"
android:text="Search"
android:background="#drawable/round_button"
/>
but the output is this one:
why it doen's have the gradient while the shape is correct?
Everything in your code is okay just change Button to androidx.appcompat.widget.AppCompatButton like below and you will be able to achieve your desire result.
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/city"
android:text="Search"
android:background="#drawable/round_button"
/>
Go to themes.xml and change parent.
parent=Theme.MaterialComponents.NoActionBar.Bridge
<style name="Theme.YourApp" parent="Theme.MaterialComponents.NoActionBar.Bridge">
//yourcode
</style>
I have two Text views in my xml file.
<TextView
android:layout_width="#dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Trainings"
android:gravity="center"
android:background="#drawable/rounded_corner"
android:onClick="changeColor"
/>
<TextView
android:id="#+id/learning_programs"
android:layout_width="#dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Learning Programs"
android:background="#drawable/rounded_corner"
android:gravity="center"
android:onClick="changeColor"/>
By default the background color of both of these is grey as set by the drawable rounded_corner.xml file, and when any of these is clicked I want it to change to white.
In my Java file I have
public void changeColor(){
this.setBackground(getResources().getColor((R.color.dark_grey_color)));
}
I know that I can use setOnclickListener and run a switch-case between the ids of the buttons based on View.
But the requirement is to it this way.
So, how can I implement it this way?
Thanks.
Just change your function. Replace Color.BLUE with whatever color or drawable you want. Because when you set onClick from xml it passes its view to the activity which you can get using view,getId to identify which click performed. In your case you are passing textview so cast that to textview.
public void changeColor(View view) {
//((TextView) view).setBackgroundColor(Color.BLUE);
GradientDrawable bgShape = (GradientDrawable)((TextView) view).getBackground();
bgShape.setColor(Color.BLUE);
}
If you want the view to maintain its background once it is clicked then you can keep the TextView and simply change the background color on the changeColor method. If you want it to toggle the background color according to click then it's a checkbox behaviour. If you want this last behaviour then implement it as follows:
Checkbox:
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Trainings"
android:button="#null"
android:gravity="center"
android:background="#drawable/view_state"/>
<CheckBox
android:id="#+id/learning_programs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Learning Programs"
android:button="#null"
android:background="#drawable/view_state"
android:gravity="center"/>
Then add a drawable to define those states (drawable/view_state):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#000" /> <!-- clicked color -->
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#FFF" /> <!-- default color -->
</shape>
</item>
</selector>
Just add the below code snipet onclick of your textview
your_textview.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
In our application we need an indeterminate progress bar, like so:
We can achieve this by setting a negative margin on the ProgressBar, like this:
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:marginTop="-7dp"
android:visibility="#{loading ? View.VISIBLE : View.GONE}" />
BUT because ConstraintLayout does not support negative margins, it will look like this:
OK, the negative margin was a hack. Let's replace it with a different hack, shall we? Let's introduce our custom view CustomProgressBar, which extends ProgressBar and overrides its onDraw method, like this:
#Override
protected void onDraw(Canvas canvas) {
int marginTop = dpToPx(7);
canvas.translate(0, -marginTop);
super.onDraw(canvas);
}
But all of this smells like bad code. There has to be a better solution!
What would you recommend?
Solution that feels less like a hack: Wrap a huge ProgressBar in a smaller FrameLayout. That way the FrameLayout constrains its height, but the ProgressBar still shows in full.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="4dp">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_gravity="center" />
</FrameLayout>
Another way to do this is to use a Guideline and center ProgressBar between the parent top and the guideline.
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:paddingTop="-4dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_begin="4dp" />
I encountered the same problem as well. And like you said, I come across numerous solutions which all seem like a hack that might break something else down the line.
With that said, I came across one solution which is to use this library instead for progress bar.
One thing to note is, it tells you to integrate it by adding:
compile 'me.zhanghai.android.materialprogressbar:library:1.3.0'
However, when I used this, it gave me an error from an Android support library for Floating Action Bar. So I'll recommend you to use this instead:
compile 'me.zhanghai.android.materialprogressbar:library:1.1.7'
A sample code snippet on how I used it:
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="#+id/reviewProgressBar"
style="#style/Widget.MaterialProgressBar.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_below="#+id/my_toolbar"
android:indeterminate="false"
app:mpb_progressStyle="horizontal"
app:mpb_useIntrinsicPadding="false"/>
Hope this helps!
A dirty workaround I did was set the height of the ProgressBar closely to the to stroke width like so:
Progress bar:
<ProgressBar
android:id="#+id/pb_loading_progress"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="2.1dp"
android:layout_below="#id/tb_browser"
android:max="100"
android:progressDrawable="#drawable/style_browser_progress_drawable"
android:visibility="invisible" />
Progress drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape android:shape="line">
<stroke
android:width="2dp"
android:color="#color/colorPrimary" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left">
<shape android:shape="line">
<stroke
android:width="2dp"
android:color="#color/white" />
</shape>
</clip>
</item>
</layer-list>
Which looked like this:
You declared your view without any explicit height so it's height is being picked from the pre-defined style.
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:marginTop="-7dp"
android:visibility="#{loading ? View.VISIBLE : View.GONE}" />
"?android:attr/progressBarStyleHorizontal" will be searched in current theme and is stored in attrs.xml file. Since this defined by the platform it will obtain it's the value from there. As of writing this answer, I'm on android platform 29 and searching "?android:attr/progressBarStyleHorizontal" gives the following results
➜ values
pwd
/Users/vihaanverma/Library/Android/sdk/platforms/android-29/data/res/values
➜ values
grep -rin "progressBarStyleHorizontal" .
./themes_device_defaults.xml:126: <item name="progressBarStyleHorizontal">#style/Widget.DeviceDefault.ProgressBar.Horizontal</item>
./themes_device_defaults.xml:857: <item name="progressBarStyleHorizontal">#style/Widget.DeviceDefault.Light.ProgressBar.Horizontal</item>
./themes.xml:272: <item name="progressBarStyleHorizontal">#style/Widget.ProgressBar.Horizontal</item>
./themes_holo.xml:263: <item name="progressBarStyleHorizontal">#style/Widget.Holo.ProgressBar.Horizontal</item>
./themes_holo.xml:626: <item name="progressBarStyleHorizontal">#style/Widget.Holo.Light.ProgressBar.Horizontal</item>
./public.xml:148: <public type="attr" name="progressBarStyleHorizontal" id="0x01010078" />
./themes_material.xml:258: <item name="progressBarStyleHorizontal">#style/Widget.Material.ProgressBar.Horizontal</item>
./themes_material.xml:635: <item name="progressBarStyleHorizontal">#style/Widget.Material.Light.ProgressBar.Horizontal</item>
./styles_material.xml:1010: <item name="progressBarStyle">?attr/progressBarStyleHorizontal</item>
./attrs.xml:684: <attr name="progressBarStyleHorizontal" format="reference" />
➜ values
Opening one of the files which contain "progressBarStyleHorizontal" you will see minHeight defined in the style and is equal to 16dp.
<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal">
<item name="progressDrawable">#drawable/progress_horizontal_material</item>
<item name="indeterminateDrawable">#drawable/progress_indeterminate_horizontal_material</item>
<item name="minHeight">16dip</item>
<item name="maxHeight">16dip</item>
</style>
This is the cause of the extra padding you are getting. You can fix it by giving your view height and a scaleY value like below
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:indeterminate="true"
android:scaleY="5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Use inside Linearlayout and you will success, the simple way
<LinearLayout
android:layout_width="match_parent"
android:background="#efefef"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
android:layout_marginTop="-7dp"
android:layout_marginBottom="-7dp"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
/>
</LinearLayout>
In my app, I want to get survey answers from the user. So, I've decided put smiley images to act as a radio buttons. Is it possible on android?
For example, I will show smiley images when user touches the image and it will be activated like a radio button. At a time, they will be allowed to only choose one. If anyone could guide me, I would appreciate it.
Example :
Thanks in advance!
This question has been answered before Below is from #Benito-Bertoli
RadioButton - how to use a custom drawable?
Give your radiobutton a custom style:
<style name="MyRadioButtonStyle" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/custom_btn_radio</item>
</style>
custom_btn_radio.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/btn_radio_off_selected" />
<item android:state_checked="false" android:drawable="#drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_radio_on" />
</selector>
Replace the drawables with your own.
Try like this
<RadioGroup
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton
android:button="#null"
android:background="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:button="#null"
android:background="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:button="#null"
android:background="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
output:(margin and pading by yourself)
I might be a bit late. Use android:button"#btmImage link"
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="#drawable/ic_launcher"
android:text="male"
android:textColor="#90999d" />
I think these 3rd party libraries will help you achieve this functionality.
SimpleRatingBar
SmileBar
They are pretty easy to use also.
Actually you just need to handle these two states, and it will work already.
<RadioButton
android:id="#+id/paragraphRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/btn_paragraph"
android:padding="14dp" />
btn_paragraph.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_paragraph_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_paragraph" android:state_checked="false" />
</selector>
I am trying to add an indicator to my button.
similar to this: Image
How can I do so? I am able to make a solid background but I don't know how to add the small blue block part. Any help is appreciated.
Thanks
Add this radiobutton in your xml file
<RadioButton
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home_selector"
android:button="#android:color/transparent"
android:checked="true" />
<RadioButton
android:id="#+id/favourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/fav_selector"
android:button="#android:color/transparent"
android:checked="true" />
....
and now you can create a selector for every different radio button as you wish
home_selector.xml
<item
android:drawable="#drawable/home_selected"
android:state_checked="true" />
<item
android:drawable="#drawable/home_normal" />
fav_selector.xml
<item
android:drawable="#drawable/fav_selected"
android:state_checked="true" />
<item
android:drawable="#drawable/fav_normal" />
Like this you can create selectors for which ever radio button you may want
You can do it like this
By adding a view with your color above the button
<LinearLayout
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:visibility="visible"
android:background="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
Set its visibility on its click