Onclick highlight my list row doesn't working - java

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.

Related

How to create custom shadow for view?

I wanna to create custom shadow for view which i customize as like as my plan.
Custom Shadow looks like Card View elevation.
I need this type of shadow
Hm. This one is extremely simple and producing very similar (imo) result to what you requested. Obviously params like elevation, corner radius etc can/should be tweaked.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="16dp"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<androidx.cardview.widget.CardView
android:id="#+id/textview_first"
android:layout_width="300dp"
android:layout_height="100dp"
app:cardCornerRadius="20dp"
app:cardElevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Check this one. This works for me.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="7dp" />
</shape>
</item>

Transparent Gradient Toolbar

I am trying to make toolbar gradient with transparent but nothing is working. Here is my code.
activity_main.xml (Updated)
<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"
tools:context="com.amrat.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/gradient"
android:layout_alignParentBottom="true"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#00ffffff"
android:startColor="#ffffff"
android:type="linear" />
</shape>
Trying to do like this:
But got this:
Please help me if anyone knows how to do this.
UPDATE
AndroidManifest.xml
<activity
android:name=".activity.MainActivity"
android:theme="#style/AppTheme.Transparent.NoActionBar" />
style.xml
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#color/light_grey_1</item>
<item name="colorControlActivated">#color/orange_1</item>
<item name="colorControlHighlight">#color/light_grey_2</item>
</style>
Use #android:color/transparent directly. Follow this one -
toolbar layout -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/background_toolbar_transparent" />
background_toolbar_transparent.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#color/black_alpha"/>
</shape>
Add a color with alphaon colors.xml -
<color name="black_alpha">#66000000</color>
You need to change your start and end colors to transparent.
<gradient
android:angle="270"
android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:type="linear" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
android:orientation="vertical">
<!-- Your content comes here -->
<android.support.v7.widget.Toolbar
android:id="#+id/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:elevation="6dp"
tools:targetApi="lollipop"/>
</FrameLayout>
Frame Layout basically works as stack and the last added item comes to top.
Good luck
Emre

Trying to add a menu to ToolBar

Trying to add a menu to my toolbar, however the code supposedly required to do that is showing errors:
Here's my other code:
res/menu/home_menu.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:id="#+id/settings"
android:title="#string/settings" />
<item
android:id="#+id/dark_theme"
android:title="#string/dark_theme" />
</menu>
Here's the layout with the Toolbar I want to add the menu to:
res/layout/activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorVeryDark"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</LinearLayout>
Any idea how I can add my menu to the toolbar?

View with drawable background doesn't show on layout

I'm trying to create a blue background for a row in a listView, but the background only appears in the XML preview and not when I run the app.
The row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<View android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/row_background"
android:id="#+id/mBackground" />
...
</RelativeLayout>
The background layout:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle"
android:thicknessRatio="9"
android:useLevel="false" >
<solid android:color="#FF0000" />
</shape>
</item>
All the other views are showing normally, I tried different ways to draw reacts in the background file but non of them worked.
Can someone help me please.

gradient of Listview items doesnot change ontapping

I have a listview settup using an custom array adapter the list appears fine I have used an gradient ans the background of each item which also appears but the code also states to change gradient on tapping which doesnot occur below are my xml's
main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0.1dp"
android:listSelector="#drawable/list_selector"
/>
row_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txtListText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="20dp"
android:textSize="35sp"
android:textColor="#545454"
android:background="#drawable/ogbg"
/>
</LinearLayout>
list_selector.xml
<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/ogbg" />
<item android:state_pressed="true"
android:drawable="#drawable/ogpress" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/ogpress" />
</selector>
ogbg.xml// normal background gradient
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFAFAFA"
android:centerColor="#FFFFFFFF"
android:endColor="#FFFFFFFF"
android:angle="90"/>
</shape>
ogpress.xml//gradient to change on tapping
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF05c1ff"
android:endColor="#7F05c1ff"
android:angle="90"/>
</shape>
I had a similar problem in the past. Try to switch the order of your selector items, I don´t know why, but the behavior of a selector depends on the order of items. If this doesn´t work, try to deparate "selected" and "pressed". Do an extra item for every state.

Categories