Reputation: 1362
I was going through this tutorial and had the idea of making it so when you press a button, one of the cards borders changes color.
I looked at this solution, but nothing happens.
Here is what I have done:
I have an xml in drawable for the shape (feed_item.xml):
<?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" android:id="@+id/feedsquare">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="@color/white" >
</solid>
<!-- view border color and width (I WANT TO CHANGE THIS) -->
<stroke
android:width="1dp"
android:color="@color/white" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
</shape>
</item>
</layer-list>
Then I have a layout that is basically how each card/list item looks (list_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="@drawable/feed_item"
android:id="@+id/card"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMM"
android:textSize="@dimen/name3"
android:textColor="@color/black" />
<!--android:scaleType="fitXY"-->
<ImageView android:id="@+id/list_image"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:layout_width="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:src="@mipmap/navigation_refresh"/>
<TextView android:id="@+id/description"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
android:textSize="@dimen/description3"
android:textColor="@color/black" />
</LinearLayout>
Now, I have a fragment with a listview. The fragment does exactly what the tutorial does (show 8 cards). I've added a button to the fragment that I want to change a specific cards border color (in the fragments onactivitycreated):
newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
shape.setStroke(1 ,R.color.green);
}
});
Nothing happens when I press the button. Any ideas as to what I can do?
Upvotes: 0
Views: 536
Reputation: 5260
To show selected item (row) in listview :
private int selectedPosition = 0;
Create method in side adapter :
public void setSelectedItem(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
Add check in getView methode
if (position == selectedPosition) { convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg)); } else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
selector_rect_black_trans_bg.xml :
<solid android:color="@color/gray1" />
<stroke
android:width="2dp"
android:color="@color/gray6" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
In Activity, only you need to pass position in adapter to show highlighted. its tested code and it will work.
Upvotes: 1