List view item layout in Android - android

I'm trying to set the background of the right arrow, that when I press it, only the background around the arrow will shown(till the separator).
I tried to do it with Image Button but when I do it like this, the list item can't be clickable.
this is my XML item list layout:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="5dip" >
<ImageView
android:id="#+id/itemlist_checkedd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#drawable/ic_checkitem" />
<ImageView
android:id="#+id/skinpreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/itemlist_checkedd"
android:scaleType="fitXY" />
<ImageButton
android:id="#+id/skinEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:paddingLeft="10dip"
android:background="#null"
android:src="#drawable/ic_go_edit" />
<ImageView
android:id="#+id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/skinEdit"
android:layout_marginRight="5dip"
android:src="#drawable/separator"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dip"
android:layout_marginRight="15dip"
android:layout_toRightOf="#id/skinpreview"
android:layout_toLeftOf="#id/separator"
android:textAppearance="?android:attr/textAppearanceLarge" />
This is how it looks:
Any suggestions???
Thanks.

Create selector xml file in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/arrow_pressed" />
<item
android:state_pressed="false"
android:drawable="#drawable/arrow" />
</selector>
You will need to create another picture of the arrow with the highlighted background you need.
of course then assign your ImageView src to the xml file (arrow_selector.xml) for example

Just set the background color or an image to the parent view in this xml

Related

Android: Setting View background using itemSelector with complex Views

We have a relatively simply Android item template on a ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#android:drawable/list_selector_background">
<!--Encounter Type and Start-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/mid_grey"
android:paddingTop="12dp"
android:paddingRight="24dp"
android:paddingBottom="12dp"
android:paddingLeft="24dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:textSize="#dimen/text_size_large"
android:text="Main Item Heading"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:45"
android:layout_toLeftOf="#+id/startTimeRelative"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
<TextView
android:id="#+id/startTimeRelative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12sp"
android:layout_alignParentRight="true"
android:text="Today"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
</RelativeLayout>
<!--Patient Name and Avatar-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:paddingLeft="24dp">
<ImageView
android:id="#+id/patientAvatar"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:scaleType="fitXY"
android:src="#drawable/patient_avatar" />
<LinearLayout
android:layout_toRightOf="#+id/patientAvatar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/patientname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subsection Heading"
android:textColor="#000000"
android:textSize="#dimen/text_size_large"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
</RelativeLayout>
<!--</LinearLayout>-->
</LinearLayout>
That displays this layout
I want to set the background of the items when they are pressed or selected. I am trying to do this by setting the item's background to a custom item selector
android:background="#android:drawable/list_selector_background"
The is is the selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/debug_blue" />
<item android:state_checked="true" android:drawable="#color/debug_green" />
<item android:state_pressed="true" android:drawable="#color/debug_blue" />
</selector>
The problem is that because I am setting the background colour's of Views within the item the Selector does not show through as expected. I have removed one of the inner colours (used in the "Subsection Heading" in the screenshot) and set that as the colour of the Listview. That means the blue shows through when the item is pressed on the lower part of the template, as seen in this screenshot
All the Android examples showing item selectors working with simple item layouts that have one or no background colour set.
How can I set the whole item's background whilst still having one or more background colours on child view's, when not selected?
I have looked at setting drawSelectorOnTop on the ListView but that did not help

how to create textview with the image and border like the image attached

I want to create a textview like the below image. I tried but not able to get like this format.
This is my xml file.
<View android:background="#ff340c1c"
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_above="#+id/genere"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/genere"
android:text="Search by Movie Genere"
android:layout_above="#+id/rating"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="78dp"
android:textSize="20sp"/>
<View android:background="#ff340c1c"
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_alignBottom="#+id/genere"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Use custom background with border and drawableLeft -
drawable/my_rect.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="0.5dp"
android:color="#color/gray" />
<solid android:color="#color/white" />
<corners android:radius="0px" />
</shape>
In XML:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/genere"
android:text="Search by Movie Genere"
android:layout_above="#+id/rating"
android:drawableLeft="#drawable/someimage" // ADD
android:background="#drawable/my_rect" // ADD
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="78dp"
android:textSize="20sp"/>
Note: Many possible solution exists.
It can be done by using a simple linear layout as a border.
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="1dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_search_category_default" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Search By Venue" />
</LinearLayout>
</LinearLayout>
For adding an image to the TextView, see android:drawableLeft. For adding a border, you could take a look at 9-patch images. Generate a 9-patch image as described, and set that as the background of the TextView.

Custom checkbox image - but can still see original?

I was using the standard Android checkbox, but when I installed my app on other peoples phones for testing, the checkbox was different on each phone.
On one of the phones, the checkbox was so light you could hardly see the box when it was checked.
So I tried to make a custom checkbox checked image.
This worked ok, but I can still see the original deafult checkbox image in the background?
Any ideas why?
Here is my checkable linear layout, to be honest I'm not even sure if I need this?
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.myappname.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:background="#drawable/customcheckbox"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.myapp.myappname.CheckableLinearLayout>
Then I have a custom list row which is my custom ListView:
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.myappname.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="60dip"
android:layout_height="60dip"
android:focusable="false"
/>
</LinearLayout>
<!-- File Name -->
<TextView
android:id="#+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:textColor="#343434"
android:textSize="12dip"
android:layout_marginTop="10dip"
android:layout_toRightOf="#+id/thumbnail"
android:focusable="false"
android:text="Some Text." />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:background="#drawable/customcheckbox"
android:enabled="false" />
</RelativeLayout>
</com.myapp.myappname.CheckableLinearLayout>
Any ideas how to suppress the original?
Custom checkbox:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--<item android:state_checked="false" android:drawable="#drawable/unchecked" />-->
<item android:state_checked="true" android:drawable="#drawable/checked" />
<!--<item android:drawable="#drawable/unchecked" /> <!– default –>-->
</selector>
The attribute you want to change is button, not background.
First, design your custom graphics for different button states. Use a state list drawable (docs) to associate the various images. For example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checked" />
<item android:state_checked="false" android:drawable="#drawable/unchecked" />
</selector>
Save this into your drawable resources folder, something like my_checkbox.xml.
Now, use the button attribute on your checkbox layout definition:
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:button="#drawable/my_checkbox"
android:enabled="false" />

set background color in imagebutton on click

Here is my file. I want to change the background color of my image button when it is clicked so the user can see it is clicked because when I clicked it does not seem I clicked it but it is working. I can see on other questions that you need a drawable to be able to do it but i do not know what will I put in the xml file in the drawable folder. Can you please help me? Thank you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/webViewHeaderArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageButton
android:id="#+id/btnCloseWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="false"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/close"
android:background="#null"
android:contentDescription="#string/button" />
<ImageButton
android:id="#+id/btnReload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnCloseWebView"
android:src="#drawable/reload"
android:background="#null"
android:contentDescription="#string/button" />
<ImageButton
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/back"
android:background="#null"
android:contentDescription="#string/button" />
<ImageButton
android:id="#+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:layout_toRightOf="#+id/btnBack"
android:src="#drawable/stop"
android:background="#null"
android:contentDescription="#string/button" />
<ImageButton
android:id="#+id/btnForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/btnStop"
android:src="#drawable/forward"
android:background="#null"
android:contentDescription="#string/button" />
</RelativeLayout>
<WebView android:id="#+id/web_engine"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
you need to create one xml file in drawable folder named contact_bg.xml.
Then, in that you have to write like below.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/contact_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/contact"/>
</selector>
set this as a image source in your image button.
and put two file in drawable-hdpi named contact.png and contact_hover.png
Hope, this will help
You can use selector for this. Either use drawable images or you can define your own shapes for each of the state. You can do some thing like this.
<item android:state_pressed="true" >
<shape>
<gradient
<!-- set values here -->
/>
<stroke
<!-- set values here -->
/>
<corners
<!-- set values here -->
/>
<padding
<!-- set values here -->
/>
</shape>
</item>
Also check out this link

How to add an image to a button in android

I was wondering how I put an image to a button.
I have tried to use "android:icon="#drawable/search.png"
and I add this image to the drawable-hdpi folder but the image doesn't show up. I am developing an application for the nexus 4 so I don't know what size the image should be. The image I am trying to add is a normal search icon used in the contact manager of the nexus 4.
The code I have written so far.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/lbl_group_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="#+id/lbl_group_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.70"
android:icon="#drawable/search.png" />
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addGroup"
android:icon="#drawable/addgroup.png"/>
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.11"
android:text="#string/Button3" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addGroup"
android:text="TEXT"
android:background="#drawable/addgroup"/>
To add background and image and text at the same time.
Just replace
android:background="#drawable/addgroup"
with
android:background="#android:color/transparent"
android:drawableTop="#drawable/addgroup"
android:text="TEXT"
You can also use attributes in your Button Layout with properties defining image source to placed inside the button
android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop
use the attribute
android:background="#drawable/...."
You can set the background of the Button using :
android:background="#drawable/..."
Please make drawable folder in res and create buttonanimation.xml in drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/add_incident_resetfocus" android:state_focused="true"/>
<item android:drawable="#drawable/add_incident_reset" android:state_focused="false" android:state_pressed="false"/>
</selector>
And use this buttonanimation in layout button background.
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonanimation"
/>

Categories

Resources