Android ListView style - android

I have a question about list views. I hope someone knows the solution, because I have been trying to solve this problem all weekend.
I have a custom list view in which every row have a custom .xml (item_row.xml) with a set color background for the row item.
<?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="70dip"
android:orientation="vertical"
android:background="#drawable/list_bg">
The problem is that when I try to make a selector for the list. I want two things:
When the item is focused, the typical orange rectangle should appear over it,
and the same When the item is pressed.
I have proved with a selector and a custom style but all I get is or the selector doesn't appear or the selector covering the item so I can't see it.
If anyone could help me with finding the right code I would be very grateful.
Thanks

Add an attribute to your ListView tag.
android:listSelector="#drawable/list_bg"
Now you can create your own style for your background using the XML file as drawable. For more information, check out the API Demos app, drawables section.

There is a special parameter for listview selected backgronud, i.e. android:listSelector. Setting this parameter to the background resource will solve your problem.

Related

How to design the spinner with custom color and bottom border etc?

I want to customize the spinner in android. When i add the xml source for spinner it looks like this:
No border, only down arrow.
But i want to customize it for example like this:
Please don't make this for me. I just want to learn by my self and want to know what are the various ways to design and customize the spinner. I need to know about all the ways for example by using xml attributes of spinner, or separate xml style file, or xml shape file or with java etc. Further more, after knowing the ways i will design it by myself. A little example with each way will be appreciated. Thanks in advance!!!
Try adding style to the spinner
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.Material.Spinner.Underlined"
/>

Android list items overlap listview borders

I am working with the simple ListView. Need to make this ListView round (not the every list item round, but the ListView itself). So for this purpose I use shape xml-file. It looks like that:
The problem appears when I need to change color of the items. This color overlaps border of the listview. And it looks something like that:
As you can see, the listView is square again, not rounded. How can I prevent this? Couldn't found the solution though it seems a pretty much common thing to me.
If you want to add space around you list item add in you list item layout..
android:paddingTop="2dp"
android:paddingRight="2dp"
android:paddingLeft="2dp";
android:paddingBottom="2dp";
or
android:padding="2dp"
Use padding in listview..
android:padding="5dp"

Different layout in android

I don't know whether this question has been asked or not but I cannot find the answer so here I ask it.
In my android app, I want to create a spinner. But the spinner in my layout is terribly ugly.
This image is my layout. Note the yellow part marks the spinner layout in my layout.
And this one is the layout of an online spinner example
I want my spinner style look like in the second image. I tried to copy all the code in xml file. but there is no changes in the style of the spinner.
Anyone know how to have spinner style look the same as in the second image?
UPDATE: when I click on the spinner in the second layout, it shows me a list like this(note the points on the right side of the list)
while my spinner can only show a normal list (have no points on the right side)
I want my spinner can shows the same as that.
if you just want to have the spinner to be "holofied" you can do this:
<Spinner
android:id="#+id/spinnermap"
style="#style/MySpinnerStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
values/styles.xml:
<style name="MySpinnerStyle" parent="android:Widget.Spinner">
</style>
set below attr for your spinner
android:spinnerMode="dropdown"
but it work for newer android version for older you can use support library or using IcsSpinner

Make Custom Navigation drawer like You-Tube in android

Hello friends i wnat to make custom slidng drawer like you-tube in my application
When user click on any item it should be show me like above image which i highlight so any idea how can i achieve this ?
Yesterday I faced same problem
but this examples highlight selector in whole listbackground but i also want red color indicator at starting which is mention
You need to create two selector. 1. For whole item 2. for that red part
For that Place a view in listitem on left side and create a selector for it.
then use following code. Here android:duplicateParentState="true" works for you.
When this attribute is set to true, the view gets its drawable state
(focused, pressed, etc.) from its direct parent rather than from
itself.
<TextView
android:layout_width="3dp"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:background="#drawable/selector_for_red" />

image on top of button android

Is it possible to add an image(view) on top of a button (which as a background image)?
I'm porting an iOS app to Android and it wasn't a problem on iOS, but I'm wondering if it is the right approach on Android because of layouts.
Edit :
To clarify, check this screen shot :
http://a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/7c4bcd53-ba55-94d7-a26c-ce1bfe040003/mza_2736801523527387264.320x480-75.jpg
I need to do the bottom left button "carte" (card in french)
I need :
a button with a background image
an image displayed on top of the button which is loaded from internet (a card , there is a lot of different and news cards are added daily, in the screnshot it s "MIDI PASS" )
a text localised on the button , so I cant use Imagebutton class.
It is not quite clear what you want to achieve, but the following may be helpful for you:
Use an ImageButton
Set the Image as the background of the button either in XML (using android:background) or in code (using setBackgroundResource)
Update:
Looking at your updated requirements, it would be better to use a custom component to achieve what you want.
Your question is somewhat unclear but from what i understood, following may work for you:
<ImageButton android:id="#+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_image"
android:src="#drawable/top_image"/>
Hope it will be helpful.
UPDATE:
If Your background is common, then you can set the bitmap using following code:
((ImageButton)findViewById(R.id.imgButton)).setImageBitmap(bmp);
Here, you will need to get the bitmap of the card image in bmp variable.
You can also use an ImageView and implement the onClickListener.
Yes it is possible.
Use an ImageButton then....
set your android:src="#drawable/foreground Image"
set your android:background="#drawable/background Image"
So if you wanted a an apple for the background image and a 3-d word "apple" for your foreground image.
You could try something like this:
First, you create a selector for the button in the res/drawable/ folder (let's call it selector_button.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/image_resource_for_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/image_resource_for_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/image_resource_for_button_normal" />
</selector>
Here you can define as and android:drawable not just #drawable's, but #color's or #layout's, too. If you want a more complex layout, you should define one with the background image of the button and another image on top of it using a RelativeLayout for example.
In order to do this, you have to have image_resource_for_button_pressed.png (for pressed and focused state) and image_resource_for_button_normal.png (for normal state) in your res/drawable/ folder.
After that, you create a button, like this:
<Button
android:id="#+id/aButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_button"
android:text="Hardcoded string" />
This approach helps you maintain code readability, since you just extracted the changing of the image resource into an .xml file.

Categories

Resources