Custom checkable list item with checkbox (layout+code)

Here is simple example how to create simple checkable list item with checbox.

This a code for widget:

public class CheckableTextView extends LinearLayout {

private TextView checkLabel;
private CheckBox checkBox;

public CheckableTextView(Context context) {
super(context);
}

public CheckableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();

}

public CheckableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
View v = inflate(getContext(), R.layout.widget_checkabletextview_merged, this);
checkLabel = ((TextView) v.findViewById(R.id.checkLabel));
checkBox = ((CheckBox) v.findViewById(R.id.checkBox));

v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
checkBox.setChecked(!checkBox.isChecked());
}
});
}

public void setText(CharSequence c) {
checkLabel.setText(c);
}

public void setText(String c) {
checkLabel.setText(c);
}

public void setChecked(boolean status)
{
checkBox.setChecked(status);
}

}

This is the widget’s layout:

<?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="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="?android:attr/selectableItemBackground">

<CheckedTextView
android:id="@+id/checkLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Sample text" />

<CheckBox
android:clickable="false"
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

</LinearLayout>

Example of one item:

layout-2015-01-07-115016

Android NavigationDrawer not handling setOnItemClickListener- nothing happens – solution

During my graduation work I had to use navigation drawer for menu list. What is more, I also needed pull to refresh layout. Both items are very sensitive and its order matters where to put them. You might be in situation where you think which one should be in which one. So during rebuilding layout with that two items first I moved pull to refresh layout up, to be a root layout. This showed me unpredictable result.
I was able swipe down to refresh even when I had already opened drawer…

Continue reading “Android NavigationDrawer not handling setOnItemClickListener- nothing happens – solution”