0

I am inflating a row multiple times in a scroll view linear Layout.i am trying to change the background color of the row which the user selects.But i am selecting the row only in some area it is changing the background color of the row .I am not getting why it is happening.Please help me.

ScrollView XML

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainScroll"
        android:layout_marginTop="5dp" >

        <LinearLayout
            android:id="@+id/mainData"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

InnerLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/resultLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:background="@drawable/backgroundcolor"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/id1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="25 Sep 2013" />

        <TextView
            android:id="@+id/id2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="" />

        <TextView
            android:id="@+id/id3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="" />

        <ImageView
            android:id="@+id/id4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic"/>
    </LinearLayout>

</LinearLayout>

BackgroundColor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/grey" />
<item android:state_pressed="true"
    android:drawable="@color/itemselected" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/itemselected" />
</selector>  

Color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="grey">#ffffff</color>
    <color name="itemselected">#EDEDED</color>
</resources>

Image as Image Link

It's working on selection but it is not filling the complete height of the layout.Please help me to find the issue

mmBs
  • 8,421
  • 6
  • 38
  • 46

5 Answers5

0

try changing the width/height of the innerlayout's linear layout to wrap_content? Also, it would help if you posted your code.

Also, I should probably say that for what it looks like you're trying to do, you should probably be using a list view, not a linear layout inside a scroll view.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
0

Try below as your Inner Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/resultLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F00"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/id1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="25 Sep 2013" />

        <TextView
            android:id="@+id/id2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="coloumn 2" />

        <TextView
            android:id="@+id/id3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="Coloumn 2" />

        <ImageView
            android:id="@+id/id4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

My suggestion is to use Custom listview Instead of trying to repeat a Linear Layout content in scrollview. Here also you can achieve same task with more flexible. Here you have to override getView method to change particular row's background color. Also you can use ContextMenu.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
0

I will suggest you to use ListView instead of ScrollView and LinearLayout. Because ListView have both properties of them. So why don't you make a try?

Try following steps..

1) Implement onItemClickListener to your list view like below.

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(
            AdapterView<?> arg0, View arg1,
            int position, long arg3) {
                adapter.setSelectedPosition(position);
    }
});

2) Now, in CustomAdapter class, write following code

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = inflater.inflate(R.layout.special_item_layout, null);

    RelativeLayout relativeLayout = (RelativeLayout) convertView
            .findViewById(R.id.relativeLayout1);

    if ((adapter.getSelectedPosition()) == position) {
        relativeLayout.setBackgroundResource(R.drawable.BackgroundColor);
    } else {
        relativeLayout.setBackgroundResource(R.color.transparent);

    //other code
}

I placed transparent color if item is not selected. You can put any resource as per your requirement.

Note

Your list must have singleChoice option in layout. And no any background.

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice">
</ListView>
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • brother i want have to do this in ListView.It's an order to implement this as i am doing help me in that way please –  Sep 25 '13 at 06:25
  • @Rahul, Just remove `ScrollView` and `LinearLayout` from your main layout. and take `ListView` instead. You have your `item` layout which is going to be used in your list. You can find some easy example http://www.vogella.com/articles/AndroidListView/article.html – Chintan Rathod Sep 25 '13 at 06:48
  • i don't have to use Listview for this that's why i asking you there will be some way so that we can do this as listview –  Sep 25 '13 at 06:56
  • @Rahul, I don't know why you don't want to use that. but this will require more time to do so.. can you tell me the reason why you don't want to use `ListView`? What is in your mind you are thinking for ScrollView with LinearLayout? – Chintan Rathod Sep 25 '13 at 07:05
  • brother first i have done this in a ListView.After when the seniors reviewed my code they said we don't need to use ListView u have to inflate a layout dynamically.so i did after that i am stucked in this issue please please help me @chintan.please –  Sep 25 '13 at 07:09
  • @Rahul, my dear, if you read carefully my code, you can see this line `inflater.inflate(R.layout.special_item_layout, null);`. This is what you need. This will dynamically provide layout my friend. This is called inflating. I think you should ask your senior person why he has not suggested this thing.. – Chintan Rathod Sep 25 '13 at 07:13
-1

Try this way you can do this

final LinearLayout tripdetailsdata = (LinearLayout) inflater
                    .inflate(R.layout.linearLayout1, null);
            final LinearLayout trip_data = (LinearLayout)tripdetailsdata.findViewById(R.id.linearLayout2);

            trip_data.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    trip_data1.setBackgroundColor(Color.WHITE);
                    trip_data.setBackgroundColor(Color.BLUE);
                    trip_data1=trip_data;
                }
            });
Developer
  • 6,292
  • 19
  • 55
  • 115