I want the imagebutton to appear transparently on the screen so i have made android:background="#null" in the XML file so it has removed the gray border around the imagebutton.It solved my problem but when i click on the imagebutton it is not showing any background color. But i need some background color to appear on click. So I have added android:padding="3dp". It has removed the border on the sides but not on the top.
<ImageButton
android:id="#+id/btnphoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/photo"
android:layout_x="4px"
android:layout_y="370px"
android:padding="3dp" />
Please help me.
Thanks in advance.
To change View aspect on state changes (click, press, focus, ...) you can use StateListDrawable as android:background. ColorStateList can be usefull too.
This post shows a good example of how to use.
android:background="#android:color/transparent"
or, if you want only some transparency, you can control it with the alpha channel.
Something like
android:background="#7F000000"
would be a partially transparent black.
You can try making your custum button background, using images or shapes in XML...
For example, this shape make one button state where the colors are transparent because they are ARGB type.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#EB363636" />
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<stroke
android:width="2dip"
android:color="#D1000000" />
</shape>
Then you can combine multiple shapes/images to make an button react the diferent states:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<item
android:state_pressed="true"
android:drawable="#layout/shape_btn"/>
<item
android:state_focused="true"
android:drawable="#layout/shape_btn"/>
<item
android:drawable="#android:color/transparent"/>
</selector>
After making your selector you can use it as your button background using android:background="NAME OF SELECTOR FILE"
Related
I'm trying to create an outlined Material Components button, however I need it to have a semi-transparent background in addition to the stroke.
This is my XML code so far:
<android.support.design.button.MaterialButton
android:id="#+id/foo"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:backgroundTint="#CFFF"
app:strokeColor="#color/colorAccent"
app:strokeWidth="2dp" />
And this is what this looks like:
The issue is that some of the background is visible outside the stroke around the button (the larger the stroke width, the more white pixels getting out).
For example, here's a 5dp stroke:
Is there a way to fix this, a better way to set the background color, or anything?
Just use the Widget.MaterialComponents.Button.OutlinedButton style:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeWidth="2dp"
app:strokeColor="#color/colorAccent"
app:backgroundTint="#3ad64f"
.../>
If you create a style of that you have to remove the app: In my example this style is called SecondButton
<style name="SecondButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:theme">#style/SecondButtonTheme</item>
<item name="backgroundTint">#color/second_button_back</item>
<item name="strokeColor">#color/second_button_text</item>
<item name="strokeWidth">1dp</item>
<item name="android:textColor">#color/second_button_text</item>
</style>
Use it like that in you
<com.google.android.material.button.MaterialButton
style="#style/SecondButton"
.../>
You could try to create your button in drawables directly in xlm based on this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="backgroundcolor"/>
<stroke android:color="strokecolor" android:width="2dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
founded here : https://android--code.blogspot.fr/2015/01/android-rounded-corners-button.html
and use it like this in your layout :
android:background="#drawable/nameofbutton.xml"
I have created custom seekbar to make it work according to my need. There are two questions related to it.
Before it let me show you my code for seekbar
In android xml
<com.abc.projectname.customlayout.CustomHorizontalSeekBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:max="7650"
android:paddingLeft="30dp" // Padding given to show thumbnail properly
android:paddingRight="30dp"
android:clickable="false"
android:paddingTop="20dp"
android:tag="drawable/seek_bar_layout_two"
android:thumb="#drawable/dial" />
CustomHorizontalSeekBar Is a class which I have created to get the tag from seekbar and set progress drawable value as whatever tag have supplied to support multiple theme
seek_bar_layout_two xml file
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+android:id/background"
android:drawable="#drawable/deselect">
</item>
<item
android:id="#+android:id/progress"
android:top="20dp"
android:bottom="20dp"
android:drawable="#drawable/lt_grey_seek_bar">
</item>
<!-- android:drawable="#drawable/loadingbar"> -->
</layer-list>
Here deselect is and highlighted image which I want to show as background of seekbar permanently.
lt_grey_seek_bar xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="left" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="#color/lt_grey_border" />
<gradient
android:endColor="#color/lt_grey_end"
android:startColor="#color/lt_grey_start" />
</shape>
</clip>
Questions
1) Can I give same background image for custom seekbar for enable and disable state? If yes than how? It is a special need from my client and it is not necessary to show disable state.
What I have tried:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_enabled="false"/>
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_pressed="true"/>
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_selected="true"/>
<item android:drawable="#drawable/seek_bar_layout_two xml"/>
</selector>
But Still no success.
2) As you can see in my seekbar implementation I have given padding to show proper thumbnail. But when I click outside the drawn state of seekbar where this padding is available seekbar automatically turns into non highlighted form. How should I stop it?
Seekbar and its touch area by which it gets non highlight
Non highlight seekbar look
I have been trying lots of ways to make it work successfully but unable to do so. Can anyone provide me a solution.
This is what I have done in my custom seekbar class
Rect bounds = getProgressDrawable().getBounds();
setProgressDrawable(getResources().getDrawable(idByTag));
getProgressDrawable().setBounds(bounds);
I have a Button in my layout whose background and textcolor are defined as selectors. When unpressed the button has a background color and a textcolor and when pressed another color and another text color - e.g. white background with black text -> black background with white text.
At a certain point in my code I need to replace the background/text color with a third set of colors and then go back to the original selector defined in the xml. However, after going back the text no longer appears and instead I get just a solid colored button.
<Button
android:id="#+id/somebutton"
android:layout_width="80"
android:layout_height="80"
android:textColor="#color/sometext_selector"
android:background="#drawable/somebackground_selector"
android:gravity="center"
android:text="#string/sometext"
android:textSize="15sp"
/>
At this point everything is fine, but when I do this:
someButton.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.backgroundRed, null));
someButton.setTextColor(getResources().getColor(android.R.color.holo_red_light));
and then afterwards this:
someButton.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.originalBackgroundSelectorDefinedInXml, null));
someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
is when the problems begin.
Here are the xmls for my selectors - apologies for pseudocode, just imagine appropriate hex values:
First - the background:
<item android:drawable="#drawable/color_purple_full" android:state_selected="true"/>
<item android:drawable="#drawable/color_purple_full" android:state_pressed="true"/>
<item android:drawable="#drawable/color_purple_full" android:state_focused="true"/>
<item android:drawable="#drawable/color_purple_outline"/>
color_purple_outline:
android:shape="oval">
<stroke android:width="2dp" android:color="#somepurplehexa" />
<size android:width="80dp" android:height="80dp"/> </shape>
color_purple_full:
android:shape="oval">
<solid android:color="#somepurplehexa"/>
<size android:width="80dp" android:height="80dp" /> </shape>
for text:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/black" android:state_selected="true"/>
<item android:color="#android:color/black" android:state_pressed="true"/>
<item android:color="#android:color/black" android:state_focused="true"/>
<item android:color="#color/green"/>
What I'm seeing is the correct background in the unpressed state but in the pressed state I'm just getting a solid color with no text on it at all. I'd be grateful for any ideas as to why this is happening and why in the original xml it works fine but only stops working after changing it programmatically?
SOLVED:
turns out the problem was here:
someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
should have been
someButton.setTextColor(getResources().getColorStateList(R.color.originalTextSelectorDefinedInXml));
I want to create a bar like this initially when progress is zero it will be a fade in color but and as progress goes on it will become bright on that part(This is best I can explain) main thing is i want bar to show all colors at the same time.
Clip your "on" drawable:
over your "off" drawable:
by using res/drawable/custom_progress_drawable.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Background -->
<item
android:id="#android:id/background"
android:drawable="#drawable/custom_progress_bar_off"/>
<!-- Secondary progress - this is optional -->
<item android:id="#android:id/secondaryProgress">
<clip android:drawable="#drawable/custom_progress_bar_secondary" />
</item>
<!-- Progress -->
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/custom_progress_bar_on" />
</item>
</layer-list>
From an Activity, use
Drawable progressDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.custom_progress_drawable, getTheme());
myProgressBar.setProgressDrawable(progressDrawable);
or in xml, use
android:progressDrawable="#drawable/custom_progress_drawable"
And here's the result when using android:max="10" in xml:
It's a little bit off, but you could use setMax() with something more like 10000 and do some offsetting calculations when calling setProgress() to make it cleaner.
Finally! I went on a mission to figure this out for you, so if this suffices, feel free to give me that bounty, haha.
Try using this in your layout:
<View android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight=".20"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="0.62">
<View android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight=".03"/>
<ProgressBar style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.94"
android:progressDrawable="#drawable/progressmask"
android:progress="0"
android:max="10"
android:rotation="180"/>
<View android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight=".03"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight=".18"/>
</LinearLayout>
which references this drawable (progressmask.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="50dip" />
<gradient android:startColor="#00000000" android:endColor="#00000000" android:angle="270" />
<stroke android:width="1dp" android:color="#00000000" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="50dip" />
<gradient android:startColor="#aa000000" android:endColor="#aa000000"
android:angle="270" />
<stroke android:width="1dp" android:color="#00000000" />
</shape>
</clip>
</item>
</layer-list>
and this image (colorprogress.png)
What it does is set the image as the background of a linearlayout, which contains a progressbar. The progressbar adds a semi-transparent black mask to the image to make it appear that the lights are off.
NOTE: In order to get this affect, I had to monkey with the progress bar (i.e. flip it, and set it to only 10 intervals. You will have to do some math to get the progress to line up with the image. i.e. setprogress((100-trueprogress)/10). Sorry I did not do this part for you.
This is what it will look like at progress 50% (the small x's and triangles will disappear on the device)
I hope this answers your question!
Like already suggested i think you should go for an layer-list and set multiple drawables then.
Main problem on this is that i need to be resizeable. An fixed size solution would be quite easy to implement.
You can't actually set the progress bars to different colors. You can however use only the on drawable and get the effect that you want. You could just apply a layer mask. What I mean is add a Relative layout which is initially say dark grey throughout i.e the gradient has only ONE color which is dark gray. Now, use code to set the gradient color on the left programmatically. Obviously the color on the left is going to be transparent. Learn more about Linear Gradients. That's about it. You just need to calculate the position from where the right gradient starts, rather where the left gradient(transparent)ends.
This method is slightly flawed and may not work on ALL devices.
The flawless method would be to create multiple .9png images and set the drawable of the progress dialog programmatically every time.
Does any one knows how can i skin an android button to look like this:
a busy cat http://www.11sheep.com/temp/picForSO.jpg
Thanks in advance,
Lior
The following layout files will product a button with 5px radius, of course u input your own color and change the solid color to gradient to match ur screenshots, then on the button change the text colour to white or something.. I dont have time for examples now.. good luck though.
and lastly you have to apply them as background to your button like this
<Button
android:id="#+id/btnLoveThisOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Love me love u too!"
android:background="#drawable/button_background" <!-- Yes look at me -->
/>
button_background_normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/button_background_normal_color"/> <!-- Change this to your own colour -->
<corners android:radius="5px"/>
<stroke android:width="1dp" android:color="#20ffffff"/>
</shape>
button_background_pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/button_background_pressed_color"/> <!-- Change this to your own colour -->
<corners android:radius="5px"/>
</shape>
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/button_background_normal" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:drawable="#drawable/button_background_normal" />
</selector>
I know two ways to make it:
a Button or TextView with an image having that engraving rectangle, for stretch, use 9.patch :)
a Button or TextView with transparent background and a selector drawing the shape border
However, I don't know exactly how they did as shown in your picture.