to show active image if I clicked button - java

I want to show active image when I clicked button.But ,if I am not clicked,to show normal image style.How can I do that ?Please help me kindly.
icon_home_config.xml
<?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/home_button_hover" />
<item android:state_focused="true" android:drawable="#drawable/home_button_hover" />
<item android:drawable="#drawable/home_button"/>
</selector>

ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/btn_background" />
btn_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_default_disabled_focused" android:state_focused="true" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_default_focused" android:state_focused="true" android:state_enabled="true"/>
<item android:drawable="#drawable/btn_default_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_default_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_default_normal" />
</selector>

use this background.xml selector & put it in drawable folder & set image background to this selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/normal" android:state_focused="true"/>
<item android:drawable="#drawable/normal"/>
</selector>
OR
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#drawable/normal" />
<stroke
android:width="0dp"
android:color="#drawable/pressed" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
set image background
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/background" />

Related

Android studio custom icon state_focused not working

custom_email_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/ic_email_focused"/>
<item android:state_focused="false"
android:drawable="#drawable/ic_email"/>
</selector>
activity_main.xml
<EditText
android:id="#+id/emailText"
android:layout_width="301dp"
android:layout_height="48dp"
android:layout_marginTop="100dp"
android:ems="10"
android:background="#drawable/custom_input"
android:drawableStart="#drawable/custom_email_icon"
android:drawablePadding="12dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:hint="Email Address"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Icon color change not working when I clicked to text. Same problem with the password line.
Instead of a focused state, you can add a selector for a pressed and selected state like following
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#drawable/ic_email" />
<item android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#drawable/ic_email" />
<item android:state_pressed="true"
android:drawable="#drawable/ic_email_focused" />
<item android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/ic_email_focused" />
<item android:state_enabled="true"
android:drawable="#drawable/ic_email" />
<item android:state_focused="true"
android:drawable="#drawable/ic_email_focused" />
<item android:drawable="#drawable/ic_email" />
</selector>
All problem is this code line, I just removed it and fixed; idk why automatically its cames when I add vector.
android:tint="?attr/colorControlNormal"

Is is possible to have rounded background of selected Bottom Navigation item?

So this is what I am trying to achieve (corners are rounded):
And here is where I'm stuck at (corners are not rounded):
Main activity xml:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
style="#style/CustomBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_navigation_menu"
app:itemBackground="#color/bottom_navigation_bar_background_color"
app:itemTextColor="#color/bottom_navigation_bar_content_color"
app:itemIconTint="#color/bottom_navigation_bar_content_color"/>
color/bottom_navigation_background_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/background_selected" />
<item android:state_checked="false" android:color="#color/background" />
</selector>
color/bottom_navigation_content_color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/text_active" android:state_checked="true" />
<item android:color="#color/text_inactive" />
</selector>
I think the problem is that you're trying to use color instead of drawable.
In the drawable folder create an xml called background_menu_item.xml, with content:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/nav_bar_colors_bg" />
<corners android:radius="120dp" />
</shape>
In the color folder create another XML called nav_bar_colors_bg.xml, with content like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/navBarActiveIconColor" android:state_checked="true" />
<item android:color="#color/navBarInactiveIconColor" android:state_checked="false"/>
</selector>
Then in the xml where you have the bottom navigation view add:
app:itemBackground="#drawable/background_menu_item"
Also if you'd like to change the icon color as well then add:
app:itemIconTint="#drawable/nav_bar_colors"
and create a nav_bar_colors.xml in the drawable folder with content like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/navBarInactiveIconColor" android:state_checked="true" />
<item android:color="#color/navBarActiveIconColor" android:state_checked="false" />
</selector>
Also make sure that the android:background is your inactive color to align with material design:
android:background="#color/navBarActiveIconColor"
Note: namings could be better I know + SO formed my reply incorrectly and due to lack of time I rather deleted it, but now after formatting I cannot undelete it so I'm posting it again.

How to remove the delay from an android button?

I created a specially effect, if a button pressed. So the the xml file is:
<?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="true"
android:drawable="#drawable/clicked"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item android:drawable="#drawable/shape"
/>
</selector>
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#2B4881" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
In activity.xml I set the file as background for the button:
<Button android:background="#drawable/background"/>
But if i press the button, it tooks a while, until the button changes it's color. Can i remove this delay?
Use below code inside selectors
<item android:drawable="#drawable/clicked" android:state_pressed="true"></item>
<item android:drawable="#drawable/shape"></item>"

Selector: "<item> tag requires a 'drawable' attribute"

I've got the following button:
<Button
android:id="#+id/searchCompaniesButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/company_icon"
android:drawablePadding="10dp"
android:drawableRight="#drawable/next_icon_big"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:backgroud="#drawable/transparent_button_selector"
android:paddingRight="10dp"
android:text="#string/searchCompanies" />
transparent_button_selector is transparent_button_selector.xml in /drawable-ldpi folder:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/lightGrey"
android:state_pressed="true" />
<item android:drawable="#color/veryLightGrey"
android:state_focused="true" />
<item android:drawable="#null" />
</selector>
Also I've got a colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="lightGrey">#3d3d3d</color>
<color name="veryLightGrey">#4d4d4d</color>
</resources>
But when I run my app, log will say:
'item' tag requires a 'drawable' attribute"
What the hell? I know there are few similar questions, but answers are not helpful. I tried:
Restarting Eclipse
Cleaning projects
Running app at real device(crush)
Replace this:
<item android:drawable="#null" />
with:
<item android:drawable="#color/transparent" />
and add transparent color in your color file with code: #00000000
The drawable attribute always takes #drawable.i.e
you have to replace
<item android:drawable="#color/..."
android:state_pressed="true" />
with
<item android:drawable="#drawable/..."
android:state_pressed="true" />
Use this,
<item
android:state_pressed="true"
android:drawable="#drawable/selected_state" />
Then make a shape in drawable folder with name "selected_state" like this.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/lightGrey" />
</shape>
Your android:background attribute is misspelled:
android:backgroud="#drawable/transparent_button_selector"
The letter 'n' is missing in 'background'

Android radius button

I have a button like this
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:text="ButtonText"
style="#style/ButtonText"/>
and this style
<style name="ButtonText">
<item name="android:layout_margin">3dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
Now I want to add radius to button, I have tried <item name="android:radius">6dp</item> but it is not working. What is a solution for this?
In your drawable folder, create a round_button.xml with
<?xml version="1.0" encoding="utf-8"?>
<!-- http://www.dibbus.com/2011/02/gradient-buttons-for-android/ -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and then in the xml containing the button, set the background as:
android:background="#drawable/round_button"
I believe to add radius to a button you have to create a custom drawable (see this link, specifically the shape drawable section)
then, with the drawable defined, set the background xml tag of the button equal to your drawable file:
android:background="#drawable/your_file"

Categories

Resources