Add this
android:descendantFocusability="blocksDescendants"
To the root element in xml
When you click on list item the checkbox takes focus. So add the above to the RelativeLayout
in xml and re-run the app.
Edit:
Looking closely you already have android:focusableInTouchMode="false"
so the above might not work.
Edit 2:
I tried your code with just strings with same layout
public class MainActivity extends Activity
{
LinearLayout linear_top_right,linear_top_left,linear_top,linear_All,linear_list;
Button btn_delete,btn_add;
CheckBox chk_select_all, chk_src_to_dest;
ListView listView;
MyCustomAdapter dataAdapter;
String allWordList[]= {"A","B","C"};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (chk_select_all != null)
chk_select_all.setChecked(false);
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
linear_top_right = new LinearLayout(this);
linear_top_right.setOrientation(LinearLayout.HORIZONTAL);
linear_top_left = new LinearLayout(this);
linear_top_left.setOrientation(LinearLayout.HORIZONTAL);
linear_top = new LinearLayout(this);
linear_top.setOrientation(LinearLayout.HORIZONTAL);
linear_list = new LinearLayout(this);
linear_list.setOrientation(LinearLayout.VERTICAL);
linear_All = new LinearLayout(this);
linear_All.setOrientation(LinearLayout.VERTICAL);
btn_delete = new Button(this);
btn_delete.setWidth(15);
btn_delete.setHeight(15);
btn_add = new Button(this);
btn_add.setWidth(15);
btn_add.setHeight(15);
linear_top_right.addView(btn_delete);
linear_top_right.addView(btn_add);
chk_select_all = new CheckBox(this);
chk_src_to_dest = new CheckBox(this);
chk_select_all.setText("All");
chk_src_to_dest.setText("SRC to Dest");
linear_top_left.addView(chk_select_all);
linear_top_left.addView(chk_src_to_dest);
listView = new ListView(this);
linear_list.addView(listView);
dataAdapter = new MyCustomAdapter(this, R.layout.m,
allWordList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(dataAdapter);
linear_top_right.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linear_top_right.setGravity(Gravity.RIGHT);
linear_top_left.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linear_top.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linear_list.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
RelativeLayout relative1 = new RelativeLayout(this);
relative1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linear_top.addView(linear_top_left);
linear_top.addView(linear_top_right);
linear_All.addView(linear_top);
linear_All.addView(linear_list);
relative1.addView(linear_All);
setContentView(relative1);
final Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep(5000); // As I am using LENGTH_LONG in Toast
MainActivity.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
//Here I have used listview click
chk_src_to_dest
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
}
});
chk_select_all
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
dataAdapter = new MyCustomAdapter(this, R.layout.m,
allWordList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,"BETA VERSION",
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<String> {
private String[] list;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] allWordList) {
super(context, textViewResourceId, allWordList);
this.list = allWordList;
}
private class ViewHolder {
CheckBox checkBx;
TextView txt_result;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.m, null);
holder = new ViewHolder();
holder.checkBx = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.txt_result = (TextView) convertView
.findViewById(R.id.txt_result);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
m.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="checkbox" />
<TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text=" " />
</RelativeLayout>
Snap