I want to change e.g. the btn_star_big_off to btn_star_big_on
Everytime someone clicks on the button the state of that should change. Toogle would be perfect, but I don't know how to combine that. And I don't know how to change state of the button. I found out that I could do that with the drawable XML File but there I only have access to the images I imported, but not the the system resource ones?
The Button:
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_star_big_off" />
And here is the trial from my drawable XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#drawable/button"/>
<!-- default -->
</selector>
You need to use ToggleButton
1) In your layout, implement ToggleButton
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<ToggleButton
android:layout_width=0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:background="#drawable/my_button"
android:text=""
android:textOff=""
android:textOn=""/>
</TableRow>
2) Creation xml layout in drawable directory ( here my_button.xml )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/btn_star_big_on" android:state_pressed="true"/><!-- pressed -->
<item android:drawable="#android:drawable/btn_star_big_on" android:state_focused="true"/><!-- focused -->
<item android:drawable="#android:drawable/btn_star_big_on" android:state_checked="true"/>
<item android:drawable="#android:drawable/btn_star_big_off" android:state_checked="false"/>
<item android:drawable="#android:drawable/btn_star_big_off"/> <!-- default -->
</selector>
here i add an another state, if you want to have an focused img
Related
I want to make color like example image. but I found a problem when changing icons and text using the color gradient because the attributes in the bottom navigation only accept color.
Please Help me god bless you.
https://imgur.com/aGKHhwW
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/Widget.BottomNavigationView"
android:background="#drawable/bg_bottom_nav"
app:itemIconTint="#drawable/bottom_nav_icon_color_selector"
app:itemTextColor="#drawable/bottom_nav_icon_color_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:itemIconSize="22dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
After many hours of trying to make the text gradient, I was finally able to achieve this effect. The point is to reach the MaterialTextView inside BottomNavigationItemView. This is the code I use in onCreate() method of the activity (Kotlin):
for (bottomNavigationItem in binding.bottomNavigation[0] as BottomNavigationMenuView) {
(((bottomNavigationItem as BottomNavigationItemView)
.getChildAt(1) as BaselineLayout)
.getChildAt(1) as MaterialTextView)
.setGradientText()
}
setGradientText() method simply decorates text. In your case you need to add third color and setup angle:
fun MaterialTextView.setGradientText() {
paint.shader = LinearGradient(
0f,
0f,
paint.measureText(text.toString()),
textSize,
intArrayOf(
ContextCompat.getColor(context, R.color.main_gradient_start),
ContextCompat.getColor(context, R.color.main_gradient_end)
),
null, Shader.TileMode.CLAMP
)
}
This code will decorate every element in bottom navigation and they only will be gradient when the element is selected. I'm not sure if it is possible to make gradient work on active and inactive elements at the same time.
Also, if you need code of my BottomNavigationView:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
app:itemTextColor="#drawable/bottom_menu_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
In conclusion, this is the best result I've reached now:
Gradient text
About icons: you need to create selectors for your icons and add them to your menu file like this:
Sample selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Icon active gradient -->
<item android:drawable="#drawable/ic_home_color" android:state_pressed="true"/>
<item android:drawable="#drawable/ic_home_color" android:state_checked="true"/>
<!-- Icon default -->
<item android:drawable="#drawable/ic_home"/>
</selector>
Menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:icon="#drawable/home_selector"
android:title="#string/home" />
<item
android:id="#+id/catalogue"
android:icon="#drawable/catalogue_selector"
android:title="#string/catalogue" />
<item
android:id="#+id/collections"
android:icon="#drawable/collections_selector"
android:title="#string/collections" />
<item
android:id="#+id/profile"
android:icon="#drawable/profile_selector"
android:title="#string/profile" />
</menu>
I am also doing this, for the gradient icon, I use 2 images. As for the text gradient, I'm still looking for a way.
Icon Gradient:
drawable/ic_home.xml: icon
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Icon active gradient -->
<item android:drawable="#drawable/icon_home_active" android:state_pressed="true"/>
<item android:drawable="#drawable/icon_home_active" android:state_checked="true"/>
<!-- Icon default -->
<item android:drawable="#drawable/icon_home"/>
</selector>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
....
</menu>
And in Activity, add the following code:
bottomNavView.setItemIconTintList(null);
It there any ways to customize the RatingBar to look like the image below? Default RatingBar is just stars. For now i managed to change the star and color to rectangle but failed to put number inside the RatingBar.
You'll have to customize your RatingBar. In XML
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="#dimen/rating_bar_height"
android:layout_gravity="center"
android:clickable="false"
android:focusable="false"
android:isIndicator="true"
android:numStars="5"
android:progressDrawable="#drawable/custom_star_rating" />
Use images of filled numbers and blank numbers in drawable as:
<?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/notfill_star" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/notfill_star" />
<item android:id="#android:id/progress" android:drawable="#drawable/fill_star" />
You can try this library too
I need to change checkbox from
to
I found that to change color of tick mark you have to put selectors:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checked" />
<item android:state_checked="false" android:drawable="#drawable/unchecked" />
</selector>
In xml:
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/your_selector"/>
create xml file
coustom_check_box.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checked" />
<item android:state_checked="false" android:drawable="#drawable/unchecked" />
</selector>
In checkbox set android:button property to the custom_checkbox_design.xml drawable like
android:button="#drawable/coustom_check_box"
<CheckBox
android:id="#+id/checkBoxCustomized1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Coustom Checkbox"
android:checked="true"
android:button="#drawable/coustom_check_box"
android:textSize="20dp" />
icons
Create a drawable custom_cheackbox:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/checkbox_not_selected"android:state_checked="false"/>
<item android:drawable="#drawable/checkbox_selected" android:state_checked="true"/>
<item android:drawable="#drawable/checkbox_not_selected"/>
</selector>
add your custom background inside custom_checkbox android:button="#drawable/custom_cheackbox"
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="CheckBox"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Black" />
i am new in android development , it very difficult to design customized user interface in
android . i have a image button that have default shap is ractangle i need do it as
rounded
shap please help me .
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#82B210"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SettingActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/makeconfess" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignRight="#+id/imageButton1"
android:layout_below="#+id/imageButton1"
android:layout_marginTop="27dp"
android:text=" Make a\nConfess"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium" />
To create a circle shape of button firstly you have to create an xml in drawable folder
circle_shape_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
use this xml in button background as
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_shape_drawable"
android:text="#string/hello_world" />
You can define a Button or ImageButton with a xml file and save it into the drawable folder. In this way you can provide different states for your button (pressed, focused, normal, ...).
For example:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_action_hover" />
<item android:state_selected="true" android:drawable="#drawable/btn_action_hover" />
<item android:state_focused="true" android:drawable="#drawable/btn_action_hover" />
<item android:drawable="#drawable/btn_action_normal" />
</selector>
To be sure your button will be displayed correctly on any resolution/screen size, I would suggest to use 9 patch drawables as explained in http://developer.android.com/tools/help/draw9patch.html.
in the drawable folder in the res folder
Create a android xml file and named as cbutton.xml
Then enter the bellow code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="#drawable/pressselected">
</item>
<item android:state_focused="true"
android:drawable="#drawable/presshighlight">
</item>
<item android:drawable="#drawable/press"></item>
</selector>
Then set this file as background of your ImageButton like in below.
<LinearLayout xmlns:android = http://schemas.android.com/apk/res/android
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Click to view Custom Button Action"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/press"
android:layout_gravity="center"
android:layout_marginTop="30dp"
/>
</LinearLayout>
xml to create a button in Android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#drawable/one" />
<! -- android:background property is used to apply background to a to button. And #drawable/one means there is a image name as one in your drawable folder we are applying it as background to this button. -->
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Code to define rounded corner button
<? xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> //Defines the shape of button you wants
<solid android:color="#eeffff"/>
<corners android:bottomLeftRadius="8dp" //how much curve you want from bottom left corner
android:bottomRightRadius="8dp"//how much curve you want from bottom right corner
android:topLeftRadius="8dp""//how much curve you want from top left corner
android:topRightRadius="8dp"/> "//how much curve you want from top right corner
</shape>
default shape of button is rectangle so now I will tell you how to create round button
For that you again need to create an xml file in your drawable folder, and name it as button_shape.xml
The codes for button_shape.xml are as follows:
Code to define button shapes
<? xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android=http://schemas.android.com/apk/res/android android:shape="oval" >
//There are many other shapes also available they are rectangle,ring,line and oval
<solid android:color="#ffcccc"/> //applying the colour to the button.
<stroke android:width="2dp"
android:color="#ffffff"/>
</shape>
When I try clicking on a a listview item no selector is shown. Here is my ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/streamRelativeLayout">
<ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="#+id/streamListView" android:cacheColorHint="#00000000" android:fadingEdge="none" android:listSelector="#drawable/swipeview_list_selector"> </ListView>
<TextView android:layout_centerInParent="true" android:layout_height="wrap_content" android:id="#+id/noStreamTextView" android:layout_width="wrap_content" android:text="No Stream Available" android:visibility="invisible"></TextView>
<ProgressBar android:layout_centerInParent="true" android:layout_height="wrap_content" android:id="#+id/streamProgressBar" android:layout_width="wrap_content"></ProgressBar>
</RelativeLayout>
Here is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#drawable/stocks_gradient" />
<item android:state_pressed="true"
android:drawable="#drawable/stocks_gradient" />
<item android:state_focused="true"
android:drawable="#drawable/stocks_gradient" />
</selector>
Here is the gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ECECEC"
android:centerColor="#F6F6F4"
android:endColor="#F8F8F6"
android:angle="90"
android:dither="true"
/>
</shape>
I am also setting the background color to alternate in getView()
int colorPos = position % colors.length;
v.setBackgroundColor(colors[colorPos]);
If you mean that selected item is not displayed the way you want, then that's because you missed the state_selected case:
<item android:state_selected="true" android:drawable="..."/>
The first issue I see is that all of your states use the same drawable, so you wouldn't see a change when you click on it. And you haven't accounted for all of the cases, for example you have no default state.
i need to set the selectorontop property to true.