How to change seekbar color in Android? - android

Is there a way I can change yellow color in the SeekBar android widget?
Any soln?

Step 1: Create Your Image Drawables (9-Patch)
Before creating any XML drawables, make sure you create the image drawables (including one 9-patch drawable) needed for the seekbar background, handle, and progress sections. The 9-patch drawables will be put to use by the XML drawables in the steps below.
Create the following drawables and place them in your /res/drawable/ folder:
Step 2: SeekBar Progress Drawable
Now create an XML drawable for the Android seekbar progress (the blue-striped section in the example), call it seekbar_progress_bg.xml, and place it in your /res/drawable/ folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<clip>
<shape>
<gradient
android:startColor="#FF5e8ea3"
android:centerColor="#FF32a0d2"
android:centerY="0.1"
android:endColor="#FF13729e"
android:angle="270"
/>
</shape>
</clip>
</item>
<item>
<clip>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/stripe_bg"
android:tileMode="repeat"
android:antialias="true"
android:dither="false"
android:filter="false"
android:gravity="left"
/>
</clip>
</item>
</layer-list>
The above XML first draws a semi-transparent, blue gradient, then layers the semi-transparent stripe image on top of the gradient. The highlighted line of code (line 20) refers to the stripe (semi-transparent) image inside your drawable folder, created in Step 1.
For more information on creating custom shapes via XML, check out the Android drawable resources docs, specifically the bitmap and shape sections.
Step 3: SeekBar Background Drawable
Next create the main seekbar progress drawable; it’ll assign a drawable to the seekbar progress and secondaryProgress actions inside your seekbar. Name your drawable something like seekbar_progress.xml, place it inside your /res/drawable/ folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/seekbar_background"
android:dither="true"
/>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:startColor="#80028ac8"
android:centerColor="#80127fb1"
android:centerY="0.75"
android:endColor="#a004638f"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="#android:id/progress"
android:drawable="#drawable/seekbar_progress_bg"
/>
</layer-list>
The first bit of highlighted code above (line 8) is referring to the seekbar background image (9-patch drawable) created in Step 1 and (line 29) is referring to the drawable you created above in Step 2.
Step 4: Bringing it all together…
At this point, all you need to do is call your seekbar_progress drawable when declaring your seekbar:
<SeekBar
android:id="#+id/frequency_slider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="0"
android:secondaryProgress="0"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/seek_thumb"
/>
The two lines of highlighted code are setting the progress and thumb drawables for the SeekBar item. The #drawable/seekbar_progress refers to the XML drawable created in the previous step.

Related

Custom loader like Instagram in android

I try to create like this custom rounded corners imageview.I searched out it and I found some examples,but Also I would to create progress bar,I mean progress in this blue border, like instagram.
Can anyone give me a some suggestions, how I can create like this loader?
Thanks
You can create a custom layout for that. Set the background of the layout (whether constraint, relative or linear) to the drawable you've created and place a progress bar in middle of it.
*Edit: Drawable file *
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid
android:color="#color/white"/> <!--Foreground color-->
<stroke
android:color="#color/blue"
android:width="1dp"/> <!--stroke color-->
<corners
android:radius="50dp"/>
</shape>
</item>
</selector>

How to make glassy quare button with drawable on top?

I want to make a glassy button with application icon drawable on top.
I tried Button, set android:background to the glassy drawable, and attached an application icon using drawableTop, however, the application icon doesn't center in the button
So I tried to use ImageButton but ImageButton doesn't have android:background and that means I can't set the glassy drawable and app icon together.
How can I make glassy drawable using XML?
How can I apply both backgrounds and center the app icon?
The button is 75x75 dp
Here is the view for your requirement
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/android"
android:text="Android"
android:padding="20dp"
android:gravity="center"
android:layout_gravity="center"
android:drawablePadding="15dp"/>
I have tried this with 75 * 75 dp drawable. It is aligning to the center. You can also set background to the button for glassy look. To do it so use this
tool to generate background drawable for your need and customize accordingly
Here is the glassy button background that i have created for you. Set this in your android:background=#drawable/glassybutton
glassybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#ff0000"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<gradient android:angle="270"
android:startColor="#80FFFFFF"
android:endColor="#20FFFFFF"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Try using ImageView, use android:background for your drawable and android:src for your icon and you can change the gravity of the icon within the view by android:gravity

Android change color without changing shape

I have created layout similar to this:
<RelativeLayout
android:id="#+id/layout_one"
style="#style/layout_one" >
<RelativeLayout
android:id="#+id/layout_two"
style="#style/layout_two" >
<RelativeLayout
android:id="#+id/layout_three"
style="#style/layout_three" >
</RelativeLayout>
</RelativeLayout>
For layout one I have created a custom drawable with rectangle shape so that the corners would be rounded and blue background color.
But for layout three I need to set white background color but if I do android:background="#FFFFFF" than it changes also the shape and the bottom corners are no longer rounded.
My first thought was to create custom drawable for the layout_three with rounded bottom corners but it wasnt working. Either all the corners were rounded or none.
Need to create something like this in the picture with rounded corners. Any suggestions?
I see that for layout three you use #style/layout_three which is different from #style/layout_one. So why don't you go to your style folder and put item with background white in layout_three style
It should look something like this:
<style name="layout_three">
<item name="android:background">#FFFFFF</item>
<!-- ... other stuff here... -->
</style>
EDIT: Sorry I did not understand your question well, reading that comment now.
There is only one way that comes to my mind right now to help you fix that.
Create an .xml file in your drawable folder and name it layout_three.xml or whatever.
And use this code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<padding
android:bottom="10dp" <!-- you can set padding here
or on your layout layout_three -->
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
<corners android:radius="2dp" /> <!-- change the radius too -->
</item>
</layer-list>
And then you just use #drawable/layout_three instead of #style/layout_three
EDIT2: You can also use this code for the corners if you want only the bottom one to be rounded
<corners
android:topRightRadius="0dp"
android:topLeftRadius="0dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/>
You can create a xml drawable with rounded bottom corners for your requirement. Try the below code:
<item>
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="7dp"
android:topRightRadius="0dp"
android:topLeftRadius="0dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"/>
</shape>
</item>
I just read that the problem is that the Android layout preview doesn't show the different corner radius so you have to test in on device or emulator to see the difference.

square edged indeterminate progress bar in android

I'm trying to customize an indeterminate progress bar in xml but I can't find a way to square the edges. I've found the default xml drawables from the SDK resources but they just reference PNGs with no mention of shape.
This is the default xml from the SDK resources:
<?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/progressbar_indeterminate1" android:duration="200" />
<item android:drawable="#drawable/progressbar_indeterminate2" android:duration="200" />
<item android:drawable="#drawable/progressbar_indeterminate3" android:duration="200" />
</animation-list>
progressbar_indeterminate1, 2 and 3 are just square PNGs but it always displays the progress bar with rounded edges.
I've tried creating a shape and using the PNGs as a background with this:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="#drawable/progressbar_indeterminate1"
android:duration="200">
<shape>
<corners android:radius="0dp" />
</shape>
</item>
</animation-list>
But it doesn't change the shape. I'd post images but I don't have enough reputation yet.
What am I missing? Thanks for any help.
I just had the same problem and it seems that indeterminate drawable that is set using XML has rounded corners. In case you need square (or probably any other) edges then you need to set indeterminate drawable with the code:
// line below is needed to have sharp corners in the indeterminate drawable,
// ProgressBar when parsing indeterminate drawable from XML sets rounded corners.
progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.progressbar_indeterminate));
And then example of progressbar_indeterminate that I used:
<?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/progress_2b" android:duration="100" />
<item android:drawable="#drawable/progress_1b" android:duration="100" />
</animation-list>
I needed to have images repeated so I created progress_1b (and 2b) as follows:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/progress_1"
android:tileMode="repeat" >
</bitmap>`
Where drawable/progress_1 are real images that I want to be repeated as background of indeterminate progressbar.
This solution worked for me.
This worked for me. No PNGs. Just colors.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="0dip" />
<stroke android:width="2px" android:color="#80c4c4c4"/>
<solid android:color="#08000000"/>
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="0dip" />
<solid android:color="#11416a"/>
</shape>
</clip>
</item>
</layer-list>
I was looking to create a horizontal indeterminate ProgressBar without rounded corners that simply swept from left to right over and over again. To that end I created the following drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="#color/app_orange"/>
</shape>
</clip>
</item>
</layer-list>
This works just fine for a normal ProgressBar but when I set it as the indeterminateDrawable in the ProgressBar's layout file I got the correct animated behavior but the size of the ProgressBar became tiny and refused to stretch across the entire page as I had set in the layout.
After despairing of making this work I decided to create a custom view and animate it myself. To that end I used ObjectAnimator which was introduced in API 11. To use it in API 10 (and lower) I included the jar from the NineYearOldAndroid project. Worked like a charm.
The final step was to create my custom view:
public class ProgressLoad extends ProgressBar
{
public ProgressLoad(Context context, AttributeSet attrs)
{
super(context, attrs);
ObjectAnimator animator = ObjectAnimator.ofInt(this, "progress", 0, 101); // Need the 101 for the progress bar to show to the end.
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.RESTART);
animator.start();
}
}
Then I simply included this custom view in my layout:
<com.example.views.ProgressLoad
android:id="#+id/pb_load"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_gravity="top"
android:progressDrawable="#drawable/content_progress_bar_determinate"/>
And voila, I had a horizontal rectangular progress bar (without rounded corners) whose progress bar swept across repeatedly. When I was done with it I simply removed the custom view from its parent.
The advantage of this technique is that one can set the speed at which the animation sweeps across by simply changing the value passed to animator.setDuration(int value).

"How to pass a parameter into a drawable" or "Create button with gradient and changeable image"

I have a custom drawable in my android project that should be the background of a button. But each button should also support an icon AND a text.
What I tried so far is the following:
I have a drawable called "btn_background.xml"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#ff0000"
android:endColor="#ee1111"
android:type="linear"
android:angle="90"/>
<stroke android:color="#ff0000"
android:width="1dip"/>
<size android:width="80dip"
android:height="40dip" />
</shape>
</item>
<item android:drawable="#drawable/btn_search"/>
</layer-list>
</item>
And in my layout file i use that like this:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#drawable/btn_background"
android:textColor="#ffffff"/>
This is getting me somewhere close... but there are two problems:
* The image is stretched across the whole button
* I can't just change the image in the layout file... so i would have to make a seperate btn_whatever.xml file for each icon?!?
The other solution that I tried was to use an ImageButton instead of an Button and set the background to my btn_background.xml .... this would make me able to select the image in the layout file... but then i can't give it a text....
I would be happy to hear about your solutions for my problem :)
Thanks a lot!
Try to use android:drawableLeft (or similar Top, Right, Bottom) attribute.

Categories

Resources