item icon won't display after choosing the item - java

i am using this library to make the same bottom navigation but when i click on item the icon will disappear, here is my code
activity_main:
<com.github.kwasow.bottomnavigationcircles.BottomNavigationCircles
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:forceDarkAllowed="true"
app:labelVisibilityMode="selected"
app:menu="#menu/navigation" />
navigation items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/destination_ejournal"
android:icon="#drawable/ejournal_selector"
android:title="#string/bottomnav_ejournal" />
<item
android:id="#+id/destination_solah"
android:icon="#drawable/solah_selector"
android:title="#string/bottomnav_solah" />
<item
android:id="#+id/destination_home"
android:icon="#drawable/home_selector"
android:title="#string/bottomnav_home" />
<item
android:id="#+id/destination_dua"
android:icon="#drawable/dua_selector"
android:title="#string/bottomnav_dua" />
<item
android:id="#+id/menu_more"
android:icon="#drawable/more_selector"
android:title="#string/bottomnav_more" />
MainActivity:
val bottomNavigation = findViewById<BottomNavigationCircles>(R.id.navigation)
bottomNavigation.color = Color.WHITE
so what am i doing wrong here?
is it in the activity or XML?
Images of the problem:
If i click Home
If i navigate to another
If i change color from white to red
home selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/home_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_home" android:state_checked="false" />
</selector>
ic_home: is verctor
home_selected: is png
SOLUTION:
i was using a selector to display a different drawable if the "state_checked" and one of them was PNG format so it didn't appear, i used 2 vectors for the selector and it works fine now

I tried your example with that library & the problem is that you're setting the color to white at runtime & that kind of blends into the bottomnav color.
You can try different color e.g red.
val bottomNavigation = findViewById<BottomNavigationCircles>(R.id.navigation)
bottomNavigation.color = Color.RED
It gives me following result:

Related

How to add a background image to an item in a navigation drawer

I want to add a background image to an item in a navigation drawer.
What I currently have:
Desired output:
On each column there would be a background image. I'm going to remove the text and just fill items with images. Is this possible? If yes, do I need different resolutions for background images?
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ITEM1"
android:title="#string/ITEM1" />
<item
android:id="#+id/nav_ITEM2"
android:title="#string/ITEM2" />
<item
android:id="#+id/nav_ITEM3"
android:title="#string/ITEM3" />
</group>
</menu>

How to change iconTint of a specific item in BottomNavigationView

I am trying to change the iconTint of the favorites menuItem in the bottomNavigationView.
To achieve so, I've tried the following:
Creating a selector with colors
Creating a selector with drawables
Programatically setting icon drawable
programatically setting iconItemTint of bottomNavigationView to null
Using colored icon drawables, instead of iconItemTint
Setting the icon of favorites to the drawable selector
Setting the background of favorites to the desired color
Setting iconTint of favorites to desired color
None of the above solutions worked for me. Then I figured that it might be because I'm using com.google.android.material.bottomnavigation.BottomNavigationView instead of android.support.design.widget.BottomNavigationView
My bottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
app:labelVisibilityMode="labeled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:itemTextColor="#232323"
app:menu="#menu/bottom_nav_menu" />
My bottom_nav_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<item
android:title="Favorites"
android:id="#+id/btn_favorites"
android:enabled="true"
android:background="#color/red_heart"
android:icon="#drawable/ic_favorite_red"
app:showAsAction="ifRoom" />
...
Here is a screenshot of my app:
The selected color is blue for all menuItems. I want the favorites to have the color #ff4444 (pale red) when selected.
I wasted a lot of time searching the correct answer to "How to change color of one specific icon of Bottom Navigation View" and I resolved this by myself. Here is the example:
val view = viewBinding.bottomNavigationView.menuView as NavigationBarMenuView
val itemToChange = view.findViewById<NavigationBarItemView>(R.id."item_to_change_id")
itemToChange.setIconTintList(
ColorStateList.valueOf(
ContextCompat.getColor(requireContext,R.color."color you need")
)
)
I didn't try this with android.support.design.widget.BottomNavigationView, but with com.google.android.material.bottomnavigation.BottomNavigationView it works correctly :)
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
app:itemTextColor="#color/bootom_text_color"
app:itemIconTint="#color/bootom_text_color"
android:background="#color/bottom_color"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation" />
create colour.xml in values
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#FFFF00" />
<item android:color="#color/white" /> </selector>

how to create or make or set custom background in floating button android

I have created custom floating button and use all library but not created custom floating button i have very tried to make to this button if you any idea how to make custom floating button.
Plz help me
My button look like this:
My code
<Button
android:id="#+id/btn_build_now"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0"
android:background="#color/Background_Main"
android:drawableBottom="#drawable/button_bg"
android:paddingBottom="10dp" />
You could simply use Android support library to create a Floating Action Button.
For example here is a floating action button example with custom background color, pressed and focused states.
XML
<android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fb"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/fb_icon_play"
app:backgroundTint="#drawable/fb_play_bg" />
fb_icon_play.xml in Drawable Folder
<?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/floating_button_blue_play_icon_pressed" />
<item android:state_focused="true" android:drawable="#drawable/floating_button_yellow_play_icon_pressed" />
<item android:drawable="#drawable/floating_button_yellow_play_icon_pressed" />
</selector>
fb_play_bg.xml in Drawable Folder
<?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="false" android:color="#3389b3"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#3389b3" />
<item android:state_focused="false" android:state_pressed="true" android:color="#d1930f" />
<item android:color="#3389b3" />
</selector>
Here is another tutorial for creating a custom FloatingActionButton.
You can use the new version of FAB, Extended FAB in XML
The old one
<com.google.android.material.floatingactionbutton.FloatingActionButton/>
The newest:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton/>
then you can define Icon, Text and Shape:
android:text="Hi"
app:icon="#android:drawable/ic_media_play"
app:shapeAppearance="#style/ShapeAppearanceOverlay.Material3.FloatingActionButton"
app:shapeAppearanceOverlay="null"

Color statelist not working on recyclerview textview

I set up a color statelist like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white_25percent_opacity" android:state_selected="true"/>
<item android:color="#color/white_25percent_opacity" android:state_pressed="true"/>
<item android:color="#color/white_25percent_opacity" android:state_focused="true"/>
<item android:color="#android:color/white"/>
</selector>
Then I tried to set it in the xml for a recyclerview item like so:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:textColor="#color/mySelector"
/>
but it doesnt work - the color doesnt change when pressed. So I tried to set it programmatically in onBindViewHolder like this:
viewHolder.myTextView.setTextColor(ContextCompat.getColorStateList(context, R.color.mySelector));
and I also tried like this:
viewHolder.myTextView.setTextColor(ContextCompat.getColor(context, R.color.mySelector));
which also don't work. Where is the mistake here and why doesn't this work for recyclerviews? To clarify - the text is shown in the initial color (white) but doesn't change to the pressed color.
Edit: also tried to solve it by making the selector a drawable - but it didn't work:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_25percent_opacity" android:state_selected="true"/>
<item android:drawable="#color/white_25percent_opacity" android:state_pressed="true"/>
<item android:drawable="#color/white_25percent_opacity" android:state_focused="true"/>
<item android:drawable="#android:color/white"/>
</selector>
If I set an ontouchlistener and switch the colors manually then it works properly - but I want to do this with a statelist.
Hi Implement your XML like the below code , Hope it would be helpful may be the issue is you have given both state_pressed and state_selected = true.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Color when the row is selected -->
<item android:drawable="#android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
<!-- Standard background color -->
<item android:drawable="#android:color/white" android:state_selected="false" />
</selector>
Turns out I needed to set this on the textview to make it work:
android:clickable="true"

Android Button vs TextView - Hyperlink like behavior

On an Android View, I want to have a some clickable text that works similar to a webpage hyperlink.
It looks just like normal text (no button border), but when touched, I want the text color to change and I may also want its background to turn a reverse color.
Is it better to use a Button with a transparent background or a TextView. When should I choose one over the other?
Thanks much
you can use Button and make selector for background and text both.
textselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000ff"/> <!-- pressed -->
<item android:state_focused="true" android:color="#android:color/white"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
buttonselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#android:color/white"/> <!-- pressed -->
<item android:state_focused="true" android:color="#0000ff"/> <!-- focused -->
<item android:color="#0000ff"/> <!-- default -->
</selector>
In your layout xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="some text"
android:textColor="#drawable/textselector" />
hotveryspicy gave a great answer and really pointed me in the right direction. Thank you, but I had a few issues with the response.
What I want is a button that has Black text and a Gray background in its normal state and a Red background with White text in its selected state. Like this:
Using hotveryspicy's suggestions I had the following issues:
I get an error when trying to assign a drawable to textColor. Funny but I see other references to doing this sort of thing. Maybe the XML validation is stricter now?
I also got an error if I didn't define a android:drawable in the buttonselector.xml. It seems you can set the color of a drawable in a selector eiter, you need to actually create a drawable.
Anyhow this is what I did:
I created a Color State Resource that looks like this:
File: res/colors/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffff"/>
<item android:color="#000000"/>
</selector>
Then the following 3 drawables:
My button background in its normal state using a Shape Drawable:
File: res/drawable/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#cccccc"/>
</shape>
My button background in its selected state, also a Shape Drawable:
File: res/drawable/link_button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
Then I created a Drawable Selector that chooses between the 2 shapes.
File: res/drawable/link_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/link_button_selected"
android:state_pressed="true" />
<item
android:drawable="#drawable/link_button" />
</selector>
Then in my layout my button looks like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Button"
android:textColor="#color/link_button"
android:background="#drawable/link_button_selector"
/>
Thanks for the help everyone!
Add String like
<string name="link">'Google'</string>
In your layout
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/link" />
You can linkify your text using the linkify class.
Have a look at this and this with few examples
For Google's sake:
Rather than make a button look like a link why not make a TextView clickable? This was effectively what I wanted and worked for my use. I wanted text to be clickable to spawn an intent.
TextView.setOnClickListener(new View.OnClickListener()

Categories