i've a Navigation Drawer and i would like that when i click on an item of the listview this change color. So i've this xml code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/navigation_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="#+id/listview"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:textColor="#drawable/test"
android:divider="#null"
android:dividerHeight="0dp"
android:background="#2F2F2F"/>
</android.support.v4.widget.DrawerLayout>
Test code is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/black"/>
<item android:state_focused="true" android:color="#android:color/black"/>
<item android:state_pressed="true" android:color="#android:color/black"/>
<item android:color="#android:color/black"/>
</selector>
But when i click remain the default color and not change in black. Why? Where is the problem and how can i fix it? Thanks in advance.
It's because you are changing the textColor of a textView not the view inside the ListView. It's not like css.
You must put android:textColor="#drawable/test" inside the TextView (or whatever view it is)
use android:listSelector="color/black" in your listview. Refer here
Related
How can i display icons along with text in Bottom navigation of Android.
Many thanks!
click here to view the image
That looks like a CardView wrapping a BottomNavigationView.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="24dp"
android:layout_gravity="bottom"
android:layout_margin="16dp">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/menu_main"/>
</android.support.v7.widget.CardView>
</FrameLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/lorem"
android:icon="#drawable/ic_archive"
android:title="Lorem"/>
<item
android:id="#+id/ipsum"
android:icon="#drawable/ic_archive"
android:title="Ipsum"/>
<item
android:id="#+id/dolor"
android:icon="#drawable/ic_archive"
android:title="Dolor"/>
</menu>
You can replace icon from menu with your own png Icons heaving text. And will leave title remain empty. Its tested Technique!
Completely new user of Android Studio is here, and I'm trying to make a movie review application as a school project.
So the problem here is that there will be several icons on my homepage's navigation bar, but the first icon for some reason is always blue and larger than the other icons, and I have no idea how to fix that, as you can see in the picture. Picture
Here's the XML of my homepage.xml and the nav_items.xml
HOMEPAGE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".Homepage">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:menu="#menu/nav_items"></android.support.design.widget.BottomNavigationView>
</RelativeLayout>
Nav_items.XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="#drawable/ic_input_black_24dp"
android:title="#string/nav_list" />
<item
android:icon="#drawable/outline_home_black_24"
android:title="#string/nav_home" />
</menu>
If it is blue by default then it means it is selected.
I used
setTheme(R.style.Theme_AppCompat);
to set my app theme darker.
Now I run into that issue that the selected item in my navigationbar is relativly dark, so that users can hardly see it.
Is there anything that I can use in my style to get the selected navigation item in another color?
Here is a picture of it
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context="at.mrminemeet.asciimoji.MainActivity">
.....
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation"/>
</android.support.constraint.ConstraintLayout>
Add the following to your BottomNavigationView:
app:itemIconTint="#drawable/bottom_navigation_selector"
app:itemTextColor="#drawable/bottom_navigation_selector"
In your drawable folder, create a file bottom_navigation_selector.xml and add this to it:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns :android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorAccent" android:state_checked="true"/>
<item android:color="#android:color/darker_gray" android:state_checked="false"/>
</selector>
Finally in every activity where you use this navigation bar add this line to onCreate() and onResume()
bottomNavigationView.setSelectedItemId(R.id.item);
You can specify the BottomNavigationView_itemBackground attribute to your BottomNavigationView, you can set a selector as a background with different colors for the normal and selected states
I have set android:windowLightStatusBar to true to display black icons and text on status bar inside my app as picture below.
However, the Navigation Drawer no longer fit to the screen, see the picture below. This is because to ensure android:windowLightStatusBar works, android:windowTranslucentStatus must be set to false. Any workaround? App like Google Calendar seem to work fine with this feature.
Here's my main activity's XML
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorToolbarBackground"
android:fitsSystemWindows="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/app_bar" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:background="#color/colorWhite"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_view_items" />
</android.support.v4.widget.DrawerLayout>
Remove this line from layout.xml
android:fitsSystemWindows="true"
And add this to styles-v21.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
in styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
in Manifest.xml add this in your Activity Main
android:theme="#style/AppTheme.NoActionBar"
And wrap AppBarLayout tag into this:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- Here AppBarLayout ecc -->
</android.support.design.widget.CoordinatorLayout>
<!-- Here NavigationView ecc -->
AppBarLayout must have
android:theme="#style/AppTheme.AppBarOverlay"
Hello friends i have tried many different ways to highlight my listview but nothing works for me. i want to highlight my customlist view when i click on that and than open a new activity.
here is my xml file list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
here is my gradient_bg_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient BgColor for listrow Selected -->
<gradient
android:startColor="#18d7e5"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>
And gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
and my _activity_setup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
help me
Just remove the list-selector from your listview and instead, set the background for your item view:
Your listview:
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#null" />
Your item view (for example):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/list_selector" >
</LinearLayout>
Hope this helps.