how to highlight selected item in recyclerview? - android-studio

I am new to Material Card view and I want to change the card Foreground color programmatically when it is long pressed.
But the parameter to the setCardForegroundColor() method requires an instance of ColorStateList to be passed and I dont know how to create ColorStateList instance for my needs.
So can anyone please tell me how to use the ColorStateList to highlight cardview when it is long pressed ?

Instead of using the ColorStateList, to change colors of my RecyclerView items when they are pressed I use a drawable item in the background of the list item. Here's how I implement it:
List item example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_selector"
android:padding="8dp">
<!-- Above: android:background defines the list item Touch Selector to highlight row -->
<TextView
android:id="#+id/rating_name_textview"
style="#style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#color/colorStatListItemText"
android:textSize="18sp" />
<TextView
android:id="#+id/rating_score_textview"
style="#style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textColor="#color/colorStatListItemText"
android:textSize="18sp"
tools:text="100" />
</LinearLayout>
List item selector example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Adding Touch Selectors for when items in the RecyclerView are touched -->
<!-- Three states: pressed, activated, and selected -->
<item android:drawable="#color/colorPrimaryLight" android:state_pressed="true" />
<item android:drawable="#color/colorPrimaryLight" android:state_activated="true" />
<item android:drawable="#color/colorPrimaryLight" android:state_selected="true" />
<!-- Define background color for Touch Selectors when item is not selected (light gray) -->
<item android:drawable="#color/colorBackgroundLight" />
</selector>
The list item references the list item selector and highlights the RecyclerView item appropriately, based on the colors I've defined in the color value file.

Related

How to change textView shape and color in Android Studio

I have looked everywhere to change the textView shape AND color and the only solution I can find is to make a drawable resource file. Which I did:
rounded_blue.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00BCD4" />
<stroke
android:width="3dp"
android:color="#ffffff" />
<corners android:radius="30dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
But when I apply it to my textView I get the shape but not the color. The color goes to default.
<Button
android:id="#+id/loginButton"
android:layout_width="249dp"
android:layout_height="45dp"
android:text="Login"
android:background="#drawable/rounded_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/button_standard"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/button_standard"
app:layout_constraintTop_toBottomOf="#+id/edittext_password"
app:layout_constraintVertical_bias="0.086" />```
[This is what I get ][1]
[1]: https://i.stack.imgur.com/f5P8Y.png
The edges are rounded as I wanted, but I want to change the button color. Why won't the color translate over as well?T

Automatically Increase layout height and width when view animate to bottom

I have three views(A, B, and C) which are stacked over one another. So when I click on a button the two views (B and C) animate from behind View A and scroll down to the bottom. Now my animation is working fine but the views B and C are extending out of the screen and my screen's height is not increasing.
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:animateLayoutChanges="true">
<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:id="#+id/trips_menu_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBackgroundGrey">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/layout_id_1"
android:layout_margin="8dp"
card_view:contentPadding="8dp"
card_view:cardElevation="16dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/layout_id_2"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="40dp"
card_view:contentPadding="8dp"
card_view:cardElevation="2dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/layout_id_3"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="40dp"
card_view:contentPadding="8dp"
android:visibility="invisible">
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
And my animation is simple translation Y animation.
Any suggestions as to how to increase the height of the layout as the two views animate to the bottom?

Child Views do not expand to the same height as their parent

I've got the following setup:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/list_item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
<!--otherwise nothing important is happening here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
android:visibility="gone"
android:orientation="horizontal" >
<TextView
android:id="#+id/undo_acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_vertical|start"
android:layout_weight="0.5"
android:textSize="20sp"
android:text="#string/undo"
android:singleLine="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="fill_vertical|end"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textSize="20sp"
android:text="#string/delete"
android:singleLine="false"
android:textColor="#color/black" />
</LinearLayout>
</RelativeLayout>
Some background:
I've got a Recycler View containing these layout as list items. When I slide an item, the LinearLayout (I've tried also RealtiveLayout) "sliding_layout" becomes visible and it's two TextViews appear.
The "list_item" layout's height is calculated using wrap_content and therefore the "sliding_layout" uses
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
to match it's height.
Up to this point it works fine. But I want the two child TextViews of "sliding_layout" to have the same height as their parent (and therefore as "list_item"). As you can see, I tried to achieve this by multiple ways (all of them separate and combined, as far as possible):
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:layout_alignParentTop="true" & android:layout_alignParentBottom="true" (with RealtiveLayout as parent)
But none of those seems to do anything: The vertical size of the TextViews seems to be calculated according to the text size.
My Build configuration is the following:
minSdkVersion 16
targetSdkVersion 21
Do you have any suggestions what I could do to achieve getting my TextViews the same height as their parent?

Android-Aligning a element + margin with relative to parent element

To align a element with respect to an other element using their I can use.
android:layout_alignLeft etc..
But how to add a margin or a padding:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/students"
android:textSize="30sp"
android:typeface="normal"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:id="#+id/student" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/student"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:text="#string/stud_describe"
android:textSize="10sp"
android:textStyle="italic"
android:typeface="sans" />
</RelativeLayout>
Padding top in the second TextView is not having any effect.
Looks like you're getting padding and margin mixed up. Change the second TextView to:
layout_marginTop="30dp"
instead of
paddingTop="30dp".
Padding pads the View itself, making it bigger. Margin changes the position of the TextView within its container (RelativeLayout) and doesn't alter its size.

ListActivity with bottom button

Hi I've created a listActivity which contain some rows of data and at the bottom of the screen (not the list) I've put a button.My problem is that in case that my list is full ,and therefore needs to be scrolled in order for the other rows to be seen,the button seems to be transparent and overlapping the last element of the list.So it doesn't seem pleasant to the eye.Do you have any suggestion of how to fix such problem?is there any way for my list to leave some space for the bottom button?I wouldn't like to implement it with footer view.I'll post the xml file so that you can have a clearer picture.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1" >
</DragNDrop.DragNDropListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:alpha="1"
android:drawableLeft="#drawable/back"
android:text="Return" />
</RelativeLayout>
Try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:alpha="1"
android:drawableLeft="#drawable/back"
android:text="Return" />
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/btn" >
</DragNDrop.DragNDropListView>
</RelativeLayout>
In a RelativeLayout, elements are placed on the top left corner by default, there's no need to specify that for the List
The simplest way is to use LinearLayout with orientation = vertical and ListView with layout_weight = 1 and Button below the ListView.
Put the button first in the relative layout and then set attribute
android:layout_above="#id/btn"
to list view. So the last elements will not be discovered by the button because the height of the list is max height minus button height.
change your xml as shown below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_above="#id/btn"
android:layout_marginBottom="50dp"
android:layout_weight="1" >
</DragNDrop.DragNDropListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:alpha="1"
android:drawableLeft="#drawable/back"
android:text="Return" />
</RelativeLayout>
Try this one, in this way you will have 10% for the button in the whole view, and 90% for the list view and the button will not overlap the last list element.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90" >
</ListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="10"
android:text="Return" />
</LinearLayout>

Resources