I have an activity where there is a list view having text view and a check box on the right. i want checkbox to be checked when the listview item is clicked. (Not on the checkbox). Like the one where android uses to check messages one by one to be deleted.
Can anyone help me in resolving this? I switched off
android:focusable="false
and
android:focusableInTouchMode="false"
on checkbox.
Below is my list view item xml.
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="1."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/textView2"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginRight="22dp"
android:layout_marginTop="-15dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
And this is my code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
if(checkbox.isChecked())
checkbox.setChecked(false);
else
checkbox.setChecked(true);
}
});
Add android:clickable="false" to the CheckBox.
In your situation, it's better to use a CheckedTextView instead, and use ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) to enable multi select.
AbsoluteSizeSpan/RelativeSizeSpan + SpannableStringBuilder could help you implement different text size in one TextView.
My answer is too late but it works perfectly.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
CheckBox cb = (CheckBox) arg1.findViewById(R.id.checkBox1);
TextView tv = (TextView) arg1.findViewById(R.id.textView1);
cb.performClick(); //this will trigger the checkbox
//do here
if(checkbox.isChecked())
checkbox.setChecked(false);
else
checkbox.setChecked(true);
}
});
And if u want get the variable value from BaseAdapter for getting checked position or value etc check this
Below is the xml that i modified to add CheckedtextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Below is the onClick method written inside getView on listAdapter
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
chkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
((CheckedTextView) v).toggle();
}
});
Related
I am trying to make a little FileExplorer.
In the custom_list.xml I have defined a CheckBox (Visible = GONE), an image and 2 textViews. What I want is, that every CheckBoxes in the list change the visibility to View.VISIBLE when I do a LongItemClick.
I already tried it with getChildCount(). But the problem is, it works only on the drawed and doesn't when you are scrolling down.
So how can I show all of the CheckBoxes by doing a longclick on an item in the listview?
customAdapter
adapter = new CustomListAdapter(arrCurrentDirFolders, subheadListFolders, imgList, getActivity().getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
HERE I WANT TO SET VISIBILITY = VISIBLE FOR ALL CHECKBOXES
/*for(int i = 0; i < listView.getChildCount(); i++) {
listView.getChildAt(i).findViewById(R.id.checkBox_checkDir).setVisibility(View.VISIBLE);
}*/
return true;
}
});
// set the list item on click listener
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.v("Position: ", position+" pressed");
}
});
custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkBox_checkDir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:visibility="gone" />
<ImageView
android:id="#+id/img_customlist_icon"
android:layout_marginLeft="5dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/checkBox_checkDir" />
<TextView
android:id="#+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignTop="#+id/img_customlist_icon"
android:layout_toEndOf="#+id/img_customlist_icon" />
<TextView
android:id="#+id/subHeadingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="#+id/headingText"
android:layout_alignStart="#+id/headingText" />
</RelativeLayout>
You could easy just put a parameter called isCheckBoxVisible in the list object you pass to the adapter which will be false at first once you long press make for loop and update the list isCheckBoxVisible to true and then notifydatasetchanged(); on the customadapter check for isCheckBoxVisible if true set checkbox to visible if false set checkbox to Gone
I have created a custom listview with checkbox, the items on the listview are not clickable. what am i doing wrong here, i want to be able to click the items on the list not just the checkbox.
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
data = store.getText().toString();
list.add(data);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view, R.id.textView1, list);
ls.setAdapter(adapter);
}
});
ls.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String click = list.get(arg2).toString();
Toast.makeText(getBaseContext(), "You Clicked " + click, Toast.LENGTH_SHORT).show();
}
});
my code for list_view.xml
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_toRightOf="#+id/checkBox1"
android:text="" />
my activity_main.xml file
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
</ListView>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" />
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/editText1"
android:text="Save" />
It doesnt work because adding CheckBox to ListView steals the focus from ListView and you are not able click the list item . There is a Workaround , do not use checkbox but instead of it you can use for example Drawable , ImageView or TextView with parameter setClickable(true) or XML android:clickable=”true” . It'll do the thing because you will not lose the focus of the ListView
I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.
I've created a popup window and put a spinner inside it. But I can't get values when an item is selected from it.
My code to create popup:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popwindow=new PopupWindow(inflater.inflate(R.layout.addpain, null,false),300,350,true);
popwindow.showAtLocation(this.findViewById(R.id.tabHost), Gravity.CENTER, 0, 0);
XML file of PopUp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB" >
<TextView
android:id="#+id/addpaintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/addpain"
android:textColor="#000000"
android:textSize="18dp"
android:typeface="serif" />
<EditText
android:id="#+id/statustbox"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/addpaintext"
android:layout_marginTop="17dp"
android:hint="#string/addpain"
android:inputType="textMultiLine"
android:textColor="#000000"
android:textSize="20dp"
android:typeface="serif" />
<Button
android:id="#+id/addpainbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/statustbox"
android:layout_marginLeft="18dp"
android:layout_marginTop="98dp"
android:onClick="statusupload"
android:text="#string/statusupload" />
<Button
android:id="#+id/cancelbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/addpainbutton"
android:layout_alignBottom="#+id/addpainbutton"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:onClick="canceladdpain"
android:text="#string/cancel" />
<Spinner
android:id="#+id/spinner_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addpainbutton"
android:layout_alignRight="#+id/cancelbutton"
android:layout_below="#+id/statustbox"
android:layout_marginTop="20dp"
android:prompt="#string/testtxt"
android:entries="#array/list"/>
</RelativeLayout>
</LinearLayout>
My StatusUpload Function:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
category=parent.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
category="Miscellaneous";
}
});
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
What happens is the vraible category isnt getting any values !! IT's just blank.
you're not seeing the results you want because everything hinges on the button to turn on your spinner listener. thus you have have to click the button, make a spinner selection, and then click the button again. I'd seriously reconsider the design. For instance, are you aware that you don't need the listener on to get the item selected? you could just use:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
category = spinner.getSelectedItem().toString();
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
I want to access the child of a ListView at position x. The ListView has the following xml layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/bg_grad_iled">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv_icon" />
<TextView android:id="#+id/tv_name"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".50"
android:layout_width="0dip"
android:padding="3px"
android:layout_marginLeft="8px"/>
<TextView android:id="#+id/tv_comment"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".20"
android:layout_width="0dip"
android:padding="3px"/>
<TextView android:id="#+id/tv_distance"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".30"
android:layout_width="0dip"
android:padding="3px"/>
</LinearLayout>
How can I access, (e.g.) the textview 'tv_name' in the 5th child of the ListView?
Thats my current listener:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "Clicked", 1000).show();
}
});
Perhaps the adapter I use may be important? Refer this
when you click on the list item OnItemClickListener will return the view, so that you can get access through that view ..
Eg:
if you want to get text of tv_name at position 5 simply:
public void onItemClick(AdapterView<?> arg0, View v, final int position,long arg3)
{
TextView text=(TextView)v.findViewById(R.id.tv_name);
Toast.makeText(this, text.getText(), Toast.LENGTH_LONG).show();
}
});
try it and get me the feedback