how to make imagebutton in android stay highlighted? - java

if you click on a button in android, then it become orange, how would you make it stay like that in java? I tried setBackgroundcolor, but it change the shape of my button to the default shape when i use it and i dont want that.

It not Possible for button as it does not have permanent focus but you can take one radio button and use android:button="#drawable/yourselector". In Radio button you will get checked event and Radio Button checked is permanent state
Make a layout in res/draw able
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:state_pressed="true"
android:drawable="#drawable/btn_attending_h" /> <!--clicked-->
<item android:drawable="#drawable/btn_attending"/> <!--not clicked-->
</selector>

Related

Bottom navigation view dot on the top of icon

I want my bottom nav bar like this image. I tried using a "." in label field but it did not work any suggestion how can we achieve this
The dot only appears on the selected item; so I suggest to add the dot on the icon, in that way you will have to icons for each item (selected and not selected). Then, you should use a drawable for the background of the item. Each item should be like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_with_dot"android:state_selected="true" />
<item android:drawable="#drawable/icon_without_dot" android:state_pressed="true" />

How to Change Color of Button when FireTV remote navigates over it

I am making my first Android App. This is for a firestick.
All done except for the color of the buttons.
The default light gray color is good enough (though I may change this when I find out how to).
When navigating over buttons with the remote control, the color of the button only changes to a slightly darker color. It makes it near impossible to tell which button is being navigated over.
I have gone all through the code and can find nowhere to change this to a darker color.
Changing just the background color stops the color changing at all when it is navigated over.
Where would I change this color?
As luck would have it I managed to find the answer just after I posted.
Create xml file in drawable (I used button_events.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="#color/primary_dark"/>
<item
android:state_pressed="true"
android:drawable="#color/primary_dark"/>
<item android:drawable="#color/primary" />
</selector>
Then I set my buttons background to use this drawable:
<Button
android:background="#drawable/button_events"/>

Contextual Action Bar Back Arrow Color Change

I made a Contextual Action Bar which inflates directly while long clicking on some item of the RecyclerView.
I did not create a toolbar or an appbarlayout in the xml file so the app starts without any toolbars or actionbars but when I long click on some item it inflates the contextual action bar.
My problem is, I tried all the possible methods to change the black color of the back arrow in the contextual action bar, but all in vain.
I'm not sure that you mean home up button, because it isn't part of contextual menu, it's part of activity menu.
If yes, just put this into your style
<item name="homeAsUpIndicator">#drawable/your_drawable</item>
your_drawable may look like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
...
</shape>
</item>
<item>
<bitmap
android:src="#android:drawable/your_image"
android:tint="#color/your_color"
/>
</item>
If no, without an image of this arrow I can't tell more.

How to create a custom shape of a button but keep it clickable and focusable

I want to make a button to have a custom shape, but when I tried the shape changed, but it wasn't clickable/focusable, how do I do it?
I hope this is what you are looking for, but I have done this a couple times where I liked a rounded button a little bit more, or if it's a widget, I wanted a checkbox. Here's a couple examples I just did by just changing android:background:
<Button
android:id="#+id/widget_yes_no_checkbox_off"
android:layout_width="#dimen/checkbox_size"
android:layout_height="#dimen/checkbox_size"
android:layout_marginEnd="#dimen/right_margin"
android:layout_gravity="center"
android:background="#android:drawable/checkbox_off_background"
/>
This gave me a button that looks like a checkbox (necessary for making a checkbox in a widget).
Or, here's a drawable xml to help me round a button
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#color/primaryDarkColor"/>
<corners android:radius="20dp"/>
</shape>
with it's corresponding style theme:
<style name="myAppStyle.ButtonStyle">
<item name="android:background">#drawable/rounded_button</item>
</style>
So, in summary, I think the easiest way to do this is to still do a button layout, but use the background attribute to change the way your button looks.
You can do it by two ways,
Set any layout/button with shape drawable and add android:foreground="?attr/selectableItemBackground" in XML
Set the Style attribute in button.
style="#style/buttonTheme" in its XML and define
<style name="buttonTheme" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/#000000</item> in /style folder.

How to show that ImageButton is pressed?

How to show that ImageButton is pressed if i add image to it like this:
Drawable testPic = getResources().getDrawable(R.drawable.test_pic);
button.setImageDrawable( testPic );
And make transparent background so that there would be just image:
button.setBackgroundColor(Color.TRANSPARENT);
So when i press image button i don't see that it is pressed. I want that image would be highlighted or something when it is pressed.
Also every my button is created dynamically by code and i don't know what image would be and how many button there are.
So any ideas ?
Thanks.
you should use a selector for choosing all the different appearances of a UI element, including an ImageButton.
first comes the state_Pressed=true which means what does it look like when pressed. after that comes the regular which is the normal state of the button.
I think you should use State List for that...
You have to change Image at run time. You can achieve these with the help of xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/category_listing_bg_img" />
<item android:state_pressed="true" android:drawable="#drawable/category_listing_bg_img_pressed" />
</selector>
Copy this xml to your drawable folder

Categories