I want to make a listview that has a custom shape background for every item in it.
This is exactly how I want it to look.
When a list item is selected, the color of it is a little bit darker, and it has an extra corner in the right.
What is the best way to achive something like this?
I've tried to make 2 images, and add both to my selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/clicked" />
<item android:state_pressed="true" android:drawable="#drawable/clicked" />
<item android:drawable="#drawable/normal" />
</selector>
And set the listview listselector to it..but is not showing as I expected, and also i'm sure is not the best way to do it...
What do you think is the best way to achive this?
Related
So my graphics artist came to me with a really cool layout for controls on our new app. The problem is getting these images laid out since most views in android are rectangular. See image for what I have to work with.
Any idea how to layout these buttons so they shape around each other if that makes sense?
If the problem is the layout, you can draw the buttons and save it as png. You can layout your app by RelevantLayout . And use the "selector" to change the button image when user press an so on.
selector example: "drawable/selector1.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/buttonClicked" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#drawable/buttonClicked" /> <!-- pressed -->
<item android:drawable="#drawable/button" /> <!-- default -->
</selector>
use it like this:
android:background="#drawable/selector1"
the Views in android are rectangular, that is correct. The only workaround I see here: use multiple "invisible" Buttons (alpha set to 0). You can position them around the screen and assign the same action to some of them. Of course you'll need to implement the OnClickListener and use switch-case.
I have a android.widget.GridView and I'm setting setOnItemClickListener() on it. It works perfectly but adds a style to the UI when a cell is clicked. Any ideas how to remove this style?
Ok thanks #Sam you got me on the right track. Could not get <item android:color="#00000000" />to work, I think for grids you need to use drawables along the lines of Android ListView Selector Color. In the end my code that works is
Java code:
mygrid.setSelector( R.color.empty_selector );
res/color/empty_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/transparent"/>
</selector>
res/color/transparent.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:src="#drawable/transparent" />
Not sure if there had been an easier way but this has worked so I'm happy.
I believe you are referring to the color selector or color state list. You can probably create a dummy selector, that has no alternate values, then set it to the GridView with setSelector() in Java or android:listSelector in XML.
A dummy selector, it is transparent:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#00000000" />
</selector>
I have a linear layout and there is an onClickListener implemented on it.
Now I want that when it is clicked its background color to white is changed and remains that way until something else is clicked. When something else is clicked I want it to have transparent background
How to achieve this?
Thanks in advance
Create two drawable images in your drawable folder. And when it is clicked, you can change the background of the layout.
Following code changes the background:
LinearLayout layout=(LinearLayout) findViewById(R.id.linearlayout);
layout.setBackgroundResource(getResources().getDrawable(R.drawable.drawable_name));
I think you could also just use a selector as background. You can probably take advantage of the 'selected' or 'focused' states to toggle the background between transparent and white. It'll look something like:
<LinearLayout
...
android:background="#drawable/bg_list_selector"
...
</LinearLayout>
And then bg_list_selector.xml in your drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#android:color/transparent" />
<!-- or -->
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:drawable="#android:color/white" />
</selector>
Have a play with the StateListDrawable's different options, I'd say.
I'm working on an application where I'm using a checkedTextView, it all works great. But I really don't like that layout of the "checkbox" within the checkedTextView, it's simply to big. Is there any way to resize it or change the layout to something custom made?
I've tried the android:checkMark attribute, but that resulted in it being marked all the time, and thus showing all the time.
Instead of using a single drawable you should write a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/drawable_checked"
android:state_checked="true" />
<item
android:drawable="#drawable/drawable_unchecked"
android:state_checked="false" />
</selector>
And then set it to the android:checkMark attribute.
I want to remove the default orange focus color from GridView and from the EditText.
I have tried the android:focusable="false" but it is not working.
I think if this works as the other views, you have to use a selector in which you define various states for your view. A selector is a drawable (stored in the drawable folder) and you use it as if it was just an image. For instance, you could do something like that, if you want the focus to be red instead of orange :
selectorgridview.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/darkred" />
<item android:color="#color/white" />
</selector>
Then, you put the background of your GridView with it : android:background="selectorgridview"
I actually never tried it on a GridView but I think it works as the other views. More infos in the docs from google