Question

I have a drawer and in the fragment I host a ListView. Depnding the selected option from the drawer the listview is populated (using an adapter) data applying different layouts for each row.

Most of the rows are a two line TextView but some of the rows are using a different layout that is consisted of one textview and a switch.

I have tried to use the setOnCheckedChangeListener but I found out that I could not get reference from any views in the row layout.

I add in the onCreateView of the Fragment inner class the following

Switch s = (Switch) rootView.findViewById(R.id.selectionSwitch);

but s is always null, hence I cannot use the setOncheckedChangeLister.

So I though to add the

android:Onclick="onSwitchIsClicked"

on the layout and try to catch the event on a custom method

public void onSwitchIsClicked(final View switchView) {
        if (switchView.getTag().equals("0")) {
            switchView.setTag("1");
        } else {
            switchView.setTag("0");
        }
    }

The above code works. On every click it gets the previous value that I have initally stored it through the Adpater (using the setTag and getTag).

But it now only works for the click. there is no android:onChange.

Is there anyone who can think of some workaround? My drawer_list_item.xml layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

My fragment layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:textDirection="ltr"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:orientation="vertical"
    android:weightSum="10">

    <LinearLayout
        android:id="@+id/userPreferencesHeader"
        android:textDirection="ltr"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            style="@style/header"
            android:textDirection="ltr"
            android:id="@+id/userPreferncesHeader"
            android:text="User Preferences"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp" />

        <TextView
            style="@style/header2ndLine"
            android:id="@+id/usePrefernces2ndHeader"
            android:textDirection="ltr"
            android:text=""
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:textDirection="ltr"
            style="@style/StrongDivider"
            android:layout_marginTop="10dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tabHostLayout"
        android:textDirection="ltr"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5">

        <LinearLayout
            android:id="@+id/tab1"
            android:textDirection="ltr"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ListView
                android:textDirection="ltr"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/userPreferencesListView"
                android:layout_gravity="center"
                android:layout_weight="1" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:textDirection="ltr"
        android:id="@+id/dividerAndLegalNotice"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:layout_alignParentBottom="true">

        <View
            style="@style/Divider"
            android:paddingTop="3dp" />

        <TextView
            android:textDirection="ltr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/OPTIONAL_FIELD_CONFIDENTIAL_MESSAGE"
            android:id="@+id/textView"
            android:textSize="10sp"
            android:paddingTop="3dp"
            android:layout_gravity="left|center_vertical" />
    </LinearLayout>
</LinearLayout>

The ListView uses either this row layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layoutDirection="ltr"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/row"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="5"
        android:padding="2dip">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_weight="4"
            android:gravity="start|left"
            android:layout_marginLeft="3dp">

            <TextView
                android:id="@+id/firstLine"
                android:layoutDirection="ltr"
                android:textSize="14sp"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:singleLine="true"
                android:textStyle="bold"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/secondLine"
                android:layoutDirection="ltr"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:textSize="11sp"
                android:gravity="top|left"
                android:singleLine="false"
                android:layout_weight="1"
                android:textColor="@color/darkGrey" />
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right|end"
            android:layout_marginRight="3dp"
            android:layout_weight="1"
            android:paddingTop="5dp"
            android:paddingBottom="5dp">

            <ImageView
                android:id="@+id/readOnlyIcon"
                android:onClick="readOnlyIconClicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="@color/grey"
                android:src="@android:drawable/ic_lock_lock" />

            <ImageView
                android:id="@+id/infoIcon"
                android:onClick="infoIconClicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="@color/grey"
                android:src="@android:drawable/ic_menu_help" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

using ListView with this row produces this

enter image description here

or this row layout that holds the switch component:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layoutDirection="ltr"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/row"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="7"
        android:padding="2dip">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:padding="6dip"
            android:layout_weight="4"
            android:gravity="start|left"
            android:layout_marginLeft="3dp">

            <TextView
                android:id="@+id/firstLine"
                android:layoutDirection="ltr"
                android:textSize="14sp"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:singleLine="true"
                android:textStyle="bold"
                android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right|end"
            android:layout_marginRight="3dp"
            android:layout_weight="2"
            android:paddingTop="5dp"
            android:paddingBottom="5dp">

            <Switch
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:switchMinWidth="14sp"
                android:layout_marginRight="8dp"
                android:text=""
                android:layout_weight="1"
                android:textOn="ON"
                android:textOff="OFF"
                android:id="@+id/selectionSwitch"
                android:onClick="switchClicked"/>

        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right|end"
            android:layout_marginRight="3dp"
            android:layout_weight="1"
            android:paddingTop="5dp"
            android:paddingBottom="5dp">

            <ImageView
                android:id="@+id/readOnlyIcon"
                android:onClick="readOnlyIconClicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="@color/grey"
                android:src="@android:drawable/ic_lock_lock"
                android:layout_weight="1" />

            <ImageView
                android:id="@+id/infoIcon"
                android:onClick="infoIconClicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="@color/grey"
                android:src="@android:drawable/ic_menu_help"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Using the ListView with this row layout produces this enter image description here

Was it helpful?

Solution

Your s is null because this Switch doesn't belong to the rootView, it belongs to the rows, right? In such a case, I assume you have your own custom adapter for populating ListView, and there is getView() method for inflating particular row. If so, you should find your switch inside your getView() method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = inflater.inflate(R.layout.<...>, parent, false);
    } else {
        v = convertView;
    }
    Switch s = (Switch) v.findViewById(R.id.selectionSwitch);
    s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener {
        //do the stuff
    });
}

OTHER TIPS

You can register a listener for the event you are looking for programmatically. Check this function out.

What you are looking for is setOnCheckedChangeListener from CompoundButton base class. So do this:

Switch s = (Switch) rootView.findViewById(R.id.selectionSwitch);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener {
  ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top