I have a shape which I need to change the colour of using the below code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
LockListDataModel locks = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_row, parent, false);
viewHolder.lockName = (TextView) convertView.findViewById(R.id.lockName);
viewHolder.colour = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}// Populate the data into the template view using the data object
viewHolder.lockName.setText(locks.lockName);
viewHolder.colour.setVisibility(View.VISIBLE);
viewHolder.colour.setBackground(ResourcesCompat.getDrawable(getContext().getResources(), R.drawable.time_profile_shape, null));
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(getContext(),R.drawable.time_profile_shape);
GradientDrawable color = (GradientDrawable)(shape.findDrawableByLayerId(R.id.time_profile_lock_colour));
color.setColor(Color.parseColor(locks.color));
// Return the completed view to render on screen
return convertView;
}
For reference the shape looks like:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="2dp" android:top="2dp">
<shape android:shape="rectangle" android:padding="10dp">
<solid android:color="#color/colorPrimaryDark"></solid>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
<item android:right="2dp" android:bottom="2dp" android:id="#+id/time_profile_lock_colour">
<shape android:shape="rectangle" android:padding="10dp" >
<solid android:color="#color/backgroundColour"></solid>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
This is populating a Custom List Adapter for a List View, and is working on all API levels 18(Minimum SDK)+ except 21 & 22 (Lollipop).
API 18 = Works, colour changes
API 19 = Works, colour changes
API 21 = Colour doesn't change
API 22 = Colour doesn't change
API 23 = Works, colour changes
How can I get this working on Lollipop?
Thanks
Related
I use layer-list to create button shape and add image
myshape.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FDD400"/>
<corners android:radius="15dp" />
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/buy_card_24dp"
android:gravity="center" />
</item>
</layer-list>
I add this myshape.xml as the background resource of an appcompact button using an adapter class since this button is in gridview
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.menu_view_item, null);
TextView textView = (TextView) v.findViewById(R.id.menuName);
AppCompatButton appCompatButton = (AppCompatButton) v.findViewById(R.id.menuImage);
appCompatButton.setBackgroundResource(menuList.get(position).getMenuImage());
textView.setText(menuList.get(position).getMenuName());
return v;
}
and i specify the myshape.xml file in the main class like
ArrayList<MenuGridItem> menuList=new ArrayList<>();
menuList.add(new MenuGridItem("Home",R.drawable.myshape));
but the icon in the button is too small and is it possible to cover the icon some percent of the shape
I was using 24dp material icon now i changed to 48dp...now icon size is almost ok...
I want to set selector on its groupView but when I click selector on groupView it goes to childView in expandable listview..
Here is my expandable listview.
<ExpandableListView
android:id="#+id/lvLeft"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:listSelector="#drawable/group_indicator"
android:transcriptMode="alwaysScroll
/>
Here is my group_indication.xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/normal" android:state_expanded="true"></item>
<item android:drawable="#drawable/normal"></item>
</selector>
Here is my normal.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/red" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
actually I want to set custom color on groupView in expandable listview but when I click on groupView, custom color set on groupView 's childView. I have attach screen shot which problem I am getting now in which Transaction History is GroupView and My Transaction is childView . Please help me solve this issue. Thanks in advance.
I have solve the problem by Prakash Gavade's answer. here is answer. In expandable listiview adapter, I have added below code.
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
TextView tvUserName = null;
ImageView imgMenuIcon = null;
View drawerView = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (groupPosition == 0)
{
drawerView = inflater.inflate(R.layout.profile_image, parent, false);
ImageView ivProfilePic = (ImageView) drawerView.findViewById(R.id.imgProfile);
tvUserName = (TextView) drawerView.findViewById(R.id.tvFname);
TextView tvLname = (TextView) drawerView.findViewById(R.id.tvLname);
}
else if(isExpanded){
convertView.setBackgroundResource(R.color.gray);
}
return drawerView;
}
Does anyone have an idea why the setItemChecked() function doesn't work for my below example:
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int pos = position;
...
default:
break;
}
listView.setItemChecked(pos, true);
setTitle(titles[pos]);
drawerLayout.closeDrawer(listviewLayout);
}
}
In all examples I've seen so far, the setItemChecked is done inside the DrawerItemClickListener method. But for some reason it does not want to work. I've tried to put it inside onCreate() or onResume(), but still no result.
I tested for simple listView, and it seems it doesn't work there either. Any suggestions ?
Code for my adapter:
public View getView(int position, View convertView, ViewGroup parent) {
Account account = this.items.get(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layoutResID, parent, false);
viewHolder.textViewTitle = (TextView) convertView
.findViewById(R.id.textViewTitle);
viewHolder.imageViewImage = (ImageView) convertView
.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
return convertView;
}
It almost doesn't matter where you call setItemChecked(). Are you using custom ListView views?
If your answer is yes, you have to specify selectors for ListView items.
This might be your drawer's ListView item:
<TextView
android:id="#+id/drawer_item_text_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/drawer_list_item_background"/>
Your item's background (R.drawable.drawer_list_item_background) must be specified as selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_menu_active" android:state_checked="true"/>
<item android:drawable="#drawable/bg_menu_active" android:state_selected="true"/>
<item android:drawable="#drawable/bg_menu_active" android:state_activated="true"/>
<item android:drawable="#drawable/bg_menu_normal" android:state_checked="false"/>
</selector>
R.drawable.bg_menu_active:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/white"/>
</shape>
R.drawable.bg_menu_normal:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/black"/>
</shape>
After this done, you are able to set chosen item as checked and highlight it somehow.
I have an adapter which extends ArrayAdapter> filling fragment layout with multiple choices each time.
The problem is that sometimes background disappears completely on items choose before, or all other items. If you check this image
http://i.imgur.com/S1KEfM1.png
you can see that ccc option lost background. Layout inflates this Relative Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="67dp"
android:background="#drawable/selectable_background_example"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="10dp" >
With this background xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#drawable/list_focused_example" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/pressed_background_example" android:state_pressed="true"/>
<item android:drawable="#drawable/pressed_background_example" android:state_selected="true"/>
<item android:drawable="#drawable/pressed_background_example" android:state_activated="true"/>
<item android:drawable="#drawable/new_strip"/>
</selector>
Pressed_background_example
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/pressed_example" />
<stroke
android:width="3dp"
android:color="#color/pressed_example" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Pressed_example
<resources>
<color name="pressed_example">#CCCC0000</color>
</resources>
And new_strip is basically just that background white image with border, and list_focused_example is image for checkbox pressed.
GetView in my class
public View getView(int position, View convertView, ViewGroup parent) {
Holder mhHolder;
if (convertView == null) {
mhHolder = new Holder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_a, parent, false);
mhHolder.txtValue = (TextView) convertView.findViewById(R.id.txtValue);
mhHolder.txtCheckbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
mhHolder.txtText = (TextView) convertView.findViewById(R.id.txtText);
convertView.setTag(mhHolder);
} else {
mhHolder = (Holder) convertView.getTag();
}
HashMap<String, String> hm = values.get(position);
mhHolder.txtValue.setText(hm.get("Value"));
mhHolder.txtText.setText(hm.get("Text"));
mhHolder.txtCheckbox.setVisibility(View.VISIBLE);
//TODO CHANGE WHEN CHECKED IS SEND
mhHolder.txtCheckbox.setChecked(Base.checked[position]);
}
Does anyone have any clue on why the background dissapears on previous selected items or sometimes on all items except the selected one.
I think it´s a kind of bug (or I don't really get the real function).
Anyhow you should try to call
mhHolder.txtValue.setbackground(R.drawable.selectable_background_example);
in your code so that it is executed for each item.
In the end the problem was the image (new_strip). I had to make a drawable the same as the image (which had only white background and gray rounded corners), so that fixed the problem.
i wanna set my listview's background like in the link's pic, it should be shown as button but i cant set background separately.
http://www.delta.edu/images/OIT/Android%20List%20View.png
i've tried android:listSelector="#drawable/pic" and this didnt work for me.
i tried tools:listitem="#layout/row" too, but it didnt worked for me.
I hope i was clear about my problem.
Check out a tutorial like this for some hints.
Create an XML file in drawable similar to this:
rounded_corners.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00FF00"/>
<corners android:radius="5dp" />
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
<stroke android:width="3dp" android:color="#00CC00"/>
</shape>
listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/rounded_corner" android:state_enabled="true"/>
<item android:drawable="#drawable/rounded_corner" android:state_pressed="true"/>
<item android:drawable="#drawable/rounded_corner" android:state_focused="true"/>
</selector>
Apply it to the ListView Adapter similar to this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
PlanetHolder holder = new PlanetHolder();
// First let's verify the convertView is not null
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_layout, null);
// Now we can fill the layout with the right values
TextView tv = (TextView) v.findViewById(R.id.name);
holder.planetNameView = tv;
v.setTag(holder);
v.setBackgroundResource(R.drawable.rounded_corner);
}
else
holder = (PlanetHolder) v.getTag();
Planet p = planetList.get(position);
holder.planetNameView.setText(p.getName());
return v;
}