Is it possible to create a custom rating bar like this?
If yes, can you give me an example on how to do it?
Thanks in advance!
Great tutorial here.
I will copy important parts in this answer in case the link will be invalid.
First you should create a style which extends the original RatingBar
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/food_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
Then you need to provide 3 drawables, why? Because you should fill the 3 cases: empty, 50% and full.
This file will be food_ratingbar_full.xml
> Here’s an example of a filled rating (cookie):
<!-- This is the rating bar drawable that is used to
show a filled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:drawable="#drawable/cookie" />
</selector>
I just use one image for all states (and it actually looks decent),
but as you can see from the selector, there are four different states
possible (#drawable/cookie is finally an actuall cookie png image).
And the cool thing here is that RatingBar component will automatically
fill in part of the cookie when needed based only on “full” and
“empty” images (if you support half ratings, as in my example image).
Then to use your style you should just add style attribute in RatingBar XML.
style="#style/foodRatingBar
The point is: you should create a custom style if you want.
Then you could use setRotation to rotate it.
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
EDIT: This is not an appropriate answer, as OP wanted to customize RatingBar. But it is still a solution.
It is possible. You just have to put each one of the five image resources in your project and then, make a layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#android:color/black"
android:gravity="left"
android:orientation="vertical"
android:weightSum="5" >
<ImageView
android:id="#+id/oneStar"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/twoStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/threeStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/fourStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/fiveStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
Put your resources as source of your ImageViews. That should be a start for you :D
Related
I know that Picasso sets the "src" therefore leaving the "image.background" in peace, but I cannot seem to make the ImageView respond to a selector (as in when it's touched to show the standard holo selector or any custom one).
Does anybody have a working sample? I must be missing something. I've tried different selectors but I don't see the touched state.
I've tried doing a setselected(true) in onClick() but I know that this is something that the View handles automatically if there's a drawable with states like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/abc_list_focused_holo" />
</selector>
The Java code to load the image is:
Picasso.with(mContext).load(url).error(R.drawable.error).fit().into(holder.someIV);
And the XML for that someIV looks like:
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:contentDescription="#string/mixes"
android:scaleType="centerCrop"
android:background="#drawable/mix_list_art_selector"
android:cropToPadding="false" />
The background is set to the above selector.
Any ideas? Thanks.
Here's what I did:
what I wanted to highlight was an ImageView that was part of the layout of a list_item (technically speaking, a row).
But I didn't want to highlight the whole row, just an image inside it.
I had to wrap the ImageView in a FrameLayout.
<FrameLayout
android:id="#+id/list_item_image_wrapper"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:clickable="true"
android:foreground="#drawable/mix_list_art_selector">
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:contentDescription="#string/mixes"
android:scaleType="centerCrop"
android:clickable="false"
android:focusable="false"
android:cropToPadding="false" />
</FrameLayout>
… and also move the Click Listeners to the FrameLayout.
Thanks to #jason_t for the pointer to the solution.
Please note that for other widgets like TextView, the above is likely not needed.
I would like to style the buttons in my Android application like this:
Whats the easiest way to create the buttons like above?
Currently I simply have a rectangle, here is the xml:
<Button
android:id="#+id/Main_DownloadCenter"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="90dp"
android:background="#drawable/main_button_about_state"
android:text="txt" >
</Button>
What I need to do now is add the extra small rectangles that are on both sides of the main big rectangle. Meaning add a small_rect_grey and after that small_rect_aqua .. how can I achieve that?
Check out the Styling Your Button section:
http://developer.android.com/guide/topics/ui/controls/button.html#Style
Mainly borderless and custom background
button_custom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:onClick="action"
android:background="#drawable/button_custom" />
I want to have a ImageButtons changing the image based on its state. (same as "like" button in nine gag and facebook apps).
I have defined a style for each of them that references a selector as a drawable but when I run the program, images doesn't appear. Can you tell me what's wrong with this approach? (sorry for my bad English)
comment_actions_style in values folder:
<style name="LikeStyle">
<item name="android:src">#drawable/like</item>
</style>
<style name="DislikeStyle">
<item name="android:src">#drawable/dislike_normal</item>
</style>
<style name="ReplyStyle">
<item name="android:src">#drawable/reply_normal</item>
</style>
<style name="ShareStyle">
<item name="android:src">#drawable/share</item>
</style>
</resources>
and like drawable in drawable folder: (other buttons are the same)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/like_clicked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#drawable/like_focused" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="#drawable/like_normal"/>
<!-- default -->
</selector>
EDIT:
I forgot to mention, I have a list view that its items are user comments and comments have buttons described above.
here is my items layout (part of it)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" />
</LinearLayout>
and comment_actions layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comment_action"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip">
<ImageButton
android:id="#+id/comment_action_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
/>
<TextView
android:id="#+id/comment_action_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/comment_action_img"
android:gravity="center"
android:layout_marginLeft="5dip"/>
</RelativeLayout>
These buttons also inherit from a layout if that's matter.
Here, your are setting style for comment_actions layout, and it is a Relative Layout, which won't be abled to response to your "android:src" attribute.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" /> <!--here,you are setting a style for the RelativeLayout -->
</LinearLayout>
I'm developing an Android application and I use this to customize RadioButtons:
<!-- Radio button style -->
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/checkbox_unchecked</item>
</style>
But the image #drawable/checkbox_unchecked is very big.
Do you know how can I make it smaller?
I know you can achieve what you want by reducing the size of your image. But I would recommend using the following code. This will help you set images for both the unselected and selected modes.
This is how you can achieve it, in a few steps:
-Create Image Drawables for their two states (checked/unchecked).
-Create selector for this two drawables. The sample content should be something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/unchecked" />
<item android:state_checked="false" android:drawable="#drawable/checked" />
</selector>
-Add this selector as a android:button attribute to RadioButton
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="Custom RadioButton" />
</LinearLayout>
I have around 6 images which i am using as tabs in my application.Its just images and they aren't the tabs that android provides.
So my question is that when i click on a image i want replace the image with a another image so that it can let user know that which tab is selected.
What i am did was creating different xml for this it in my layout.But its tedious to do so since I can't be creating different xml's for every image.
What i am trying to do now is using for this but not getting the result.
How is it possible to use for this ?
Its quite simple.You dont have to make different xml for each image/tab.you just have to make one xml and include this xml in every xml which needs these images..in the xml set only one image as selected and the rest unselected.Suppose for the image1 keep image1 selected and the rest unselected.by clicking image2 will redirect to activity2.In activity2 set image2 selected and the rest unselected and the same for the remaining activities.This way will be the easiest way.Trust me i have implemented this and will help you too.any sort of queries you can ask me.
i have done with 4 images but in your case there will be 6.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
style="#style/toolButton"
android:background="#drawable/image1"
android:gravity="center"
/>
<Button
android:id="#+id/btn2"
style="#style/toolButton"
android:background="#drawable/image2"
android:gravity="center"
/>
<Button
android:id="#+id/btn3"
style="#style/toolButton"
android:background="#drawable/image3"
android:gravity="center"/>
<Button
android:id="#+id/btn4"
style="#style/toolButton"
android:background="#drawable/image4"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
And here is style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="toolButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">45dip</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_weight">5</item>
<item name="android:layout_marginBottom">0dip</item>
<item name="android:layout_marginTop">0dip</item>
</style>
</resources>