highlight selected item on listview - android

before someone marked this question as duplicate, I must say I already know that this question is already asked several times, but every time I tried I always failed.
so I have a listview of date on the left and I want to show data on the right, I want to make a listview that can show which item is selected so I know which date that I picked.
So far I've tried this;
on listview I've added android:choiceMode="singleChoice";
<ListView
android:id="#+id/listTglRMPasien"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_above="#+id/btnObatAlkes"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:choiceMode="singleChoice">
</ListView>
on item of listview I've added android:background="#drawable/list_selector";
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtListRekamMedikTanggal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/list_selector"/>
<TextView
android:id="#+id/txtListRekamMedikTabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
</LinearLayout>
and on list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher" android:state_activated="false"/>
<item android:drawable="#drawable/ic_launcher" android:state_pressed="false"/>
<item android:drawable="#drawable/blue" android:state_pressed="true"/>
<item android:drawable="#drawable/blue" android:state_activated="true"/>
</selector>
note that ic_launcher and blue here are png file, it's supposed to be "#drawable/listitem_pressed" and "#drawable/solid_white", I find this answer from here
but somehow it got an error, it says No resource found that matches the given name and I think something is missing so I changed it to png file
anyway, when I tried to run this, it always show ic_launcher whether I select it or not, it never show blue.

apparently it happens because my API level is too low, I changed it from API level 8 to API level 14.
this is a good answer about how to change min. API level for your Android app in Eclipse

Here is what I did on my ListView. Create your own theme on res/values folder, you can name it for example mytheme.xml. Here what I added on mytheme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="android:Theme.Holo.Light">
<item name="android:textColor">#fff</item>
</style>
</resources>
Then in your Manifest add this android:theme="#style/mytheme" inside the activity of your ListView Class.
Hope it helps.

Related

Custom buttons doesn't display android studio

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ImageButton Selected"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button"/>
This is in my activity_main.xml
and this
<item android:drawable="#drawable/round" android:state_pressed="true"/>
<item android:drawable="#drawable/round2" android:state_focused="true"/>
in my button.xml
the custom buttons are made with image button function
when I start the application It doesn't give error and shows only "imagebutton selected"
The problem can be with my image?being too large?
here are the images and they are png btw with 1000x1000..I know is big but being png I thought will be everything fine..now I have to find how to make the white background dissapear.
here are the images round and round2
I don't know what is a selector,I know that I've put them in the folder drawable and then I followed a tutorial to make custom buttons.
the images are vectors
here is how the emulator looks like emulator
Your button.xml should look like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/round2" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/round3" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/round"/>
</selector>
Also, you should reduce the size of your PNGs, that would take up a lot of space, and a lot of memory.

color selector for activatedBackgroundIndicator not working

I am trying to have a selected item from a list view stay highlighted. after searching, activatedBackgroundIndicator should be the easy way to go, but I can't seem to get it working. Here is my code segment
for my individual item layout, I have res/layout/simplerow.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:gravity="bottom"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="?android:attr/activatedBackgroundIndicator" >
</TextView>
and my color selector, I have res/drawable/playerbg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/frame_selected"/>
<item android:state_activated="true" android:drawable="#drawable/frame_activated"/>
<item android:state_pressed="true" android:drawable="#drawable/frame_pressed" />
</selector>
I can see that the color selector is working because when I press on one of the item, I get the picture_frame_pressed. but I can't get any of the color to stay high lighted. I am using API 17 running on a real device with android 4.2

How to arrange the rows and columns in gridview in android? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to set a row for grid-view in android?
Now i am displaying the data from XML file in to grid view in android.This is the exact xml's file link
"http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders"
which i am trying to show.
I have done that concept successfully, but here the problem is,i am not getting the answer like the below image
i need to show like the below image, in android
but i am getting only like the below image.....
![enter image description here][3]
How to overcome this concept?can any one please make me clear?
thanks for your precious time!..
Try the below layout in your code has grid_layout.xml, it may help you a while.Better to use Listview for these kind of implementation.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
style="#style/gridViewStyle" />
under styles.xml place the below code
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- GridView Style -->
<style name="gridViewStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:columnWidth">60dp</item>
<item name="android:gravity">center</item>
<item name="android:horizontalSpacing">10dp</item>
<item name="android:numColumns">3</item>
<item name="android:padding">10dp</item>
<item name="android:stretchMode">columnWidth</item>
<item name="android:verticalSpacing">10dp</item>
</style>
</resources>
place your relevant Listitems in your grid_items.xml has below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/icon_image"
style="#style/wrapViewScale"/ >
<TextView
android:id="#+id/icon_text"
style="#style/wrapViewScale"
android:gravity="center_horizontal"
android:textColorHighlight="#color/text_color_highlight" />
</LinearLayout>
stick with gridview , and change the childview by inflating custom xml file

main.xml: Failed to parse file

I'm developing a test application for Android, and I'm trying to add a custom image to a button. But when I try to save I get the error "main.xml: Failed to parse file C:\Program Files\Eclipse Workspace\App\res\drawable\custom_button.xml". I don't know how to solve it. I think both main.xml and custom_button.xml are correct.
custom_button.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal" />
</selector>
main.xml:
<?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="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_button"/>
</LinearLayout>
For making this, I followed these two tutorials:
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
And:
http://developer.android.com/reference/android/widget/Button.html
This is not an answer but since I dont have comment perm I will write it here anyways.
Maybe "#drawable/button_pressed" or the other drawable may not be present.
Or like already said in the comment try recreating it. I have had this problem previously. And make sure that if you are copy pasting, not to include any invalid characters.

Defining IDs within style, is it safe or a disaster?

Following question has kept me puzzled for a while and I thought maybe asking about this does no harm. I have the following layout.xml and style.xml files;
res/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
style="#style/headerContainer" />
<LinearLayout
style="#style/footerContainer" />
<ScrollView
style="#style/contentContainer" />
</RelativeLayout>
res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="container">
<item name="android:layout_width">fill_parent</item>
</style>
<style name="headerContainer" parent="container">
<item name="android:layout_height">40dp</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:background">#80FF0000</item>
<item name="android:id">#+id/header</item>
</style>
<style name="footerContainer" parent="container">
<item name="android:layout_height">50dp</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:background">#8000FF00</item>
<item name="android:id">#+id/footer</item>
</style>
<style name="contentContainer" parent="container">
<item name="android:layout_height">60dp</item>
<item name="android:layout_below">#id/header</item>
<item name="android:layout_above">#id/footer</item>
<item name="android:background">#800000FF</item>
</style>
</resources>
Now, the question is, is there a danger of overlapping IDs as I'm introducing them in style.xml? Funny thing is that this approach works, on the emulator I'm using at least, but the created IDs are not being added to the R class. And I'm a bit confused how they are defined once my layout is inflated.
Don't use #+id/... in styles.
#+id/... can only be used in layouts.
Otherwise you can get Error executing apt: return code 139 during build.
Use #id/... and generate ids with help resource file if needed:
res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="header" />
<item type="id" name="footer" />
</resources>
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Its not safe my friend. You should use different id for different files. Here the emulator will not act problem. It will understand because for every xml file there is unique code defined R.java file automatically. So emulator will understand from there very easily. But if you need to improve or edit the code for any up gradation surely you will get confused which id is belonging to which xml file's layout or wedged. So provide unique id to every widget of layout. It will be helpful if you provide the id including some tag of respective file name.
Example: If file name is filldetails.xml then you can use id=#+fd_name
It will be helpful to know the flow of application.
I am doing it this way and have had good luck:
Layout
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="#layout/action_bar"/>
</RelativeLayout>
Common:
res/layout/action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_container"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="20dip"
android:textStyle="bold"
/>
</RelativeLayout>

Categories

Resources