Question

So basically I'm making something like 'options' form my app and I want it to look a bit like android options. Im using ListView with BaseAdapter and i managed to put both textview and checkbox into one row (because standard checkbox has its text on the right side and I want it to be otherwise). When I'm pressing checkbox only i want to highlight the textview too (like on second screen) but only small area around the box is highlighted. Second screen presents pressing on whole textview. Both actions (presing on checkbox or textview) have the same results so i don't want them to have different looks.

first screen: https://i.stack.imgur.com/JY8Xw.png second screen: https://i.stack.imgur.com/vNQis.png

//import [..]

public class OptionsAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Option> options;
    private final String TAG = "OptionsAdapter";
    private static final int type_textBox = 0;
    private static final int type_checkBox = 1;
    private static final int type_separator = 2;

    public OptionsAdapter(Context context, ArrayList<Option> options) {
        this.context = context;
        this.options = options;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        final Option opt = this.getItem(position);
        return opt.getMode();
    }

    @Override
    public int getCount() {
        return options.size();
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public Option getItem(int position) {
        return options.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        return options.get(position).getMode() == 2 ? false : true;
    }

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

        final OptionsViewHolder ovh;
        int type = getItemViewType(position);
        final Option opt = this.getItem(position);

        if (convertView == null) {
            ovh = new OptionsViewHolder();

            convertView = inflater.inflate(R.layout.row_text_checkbox, null);
            ovh.setTextview((TextView) convertView
                    .findViewById(R.id.opt_row_text));
            ovh.setCheckbox((CheckBox) convertView
                    .findViewById(R.id.opt_row_cb));
            ovh.getTextview().setText(opt.getName());
            ovh.getCheckbox().setText("");

            switch (type) {
            case type_checkBox:
                ovh.getCheckbox().setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        opt.toggle();
                    }
                });
                break;
            case type_textBox:
                ovh.getCheckbox().setVisibility(View.GONE);
                break;
            case type_separator:
                ovh.getCheckbox().setVisibility(View.GONE);
                TextView tv = ovh.getTextview();
                tv.setTextSize(12);
                tv.setAllCaps(true);
                tv.setTextColor(Color.GRAY);
                tv.setGravity(Gravity.CENTER);
                tv.setPadding(10, 30, 10, 5);
                break;
            }

            convertView.setTag(ovh);

        } else {
            ovh = (OptionsViewHolder) convertView.getTag();

        }
        ovh.getTextview().setText(opt.getName());

        return convertView;
    }

}
Was it helpful?

Solution 3

The correct way to do this is to include both TextView and CheckBox in one LinearLayout, then set clickable="false" and focusable="false" to both, and all onClick events should be managed when layout is clicked (so the layout will need and ID)

OTHER TIPS

Add an OnCheckedChangeListener to your CheckBox view, and call ListView.setSelection(position) when checkbox is checked. You may need some logic function to determine which position the CheckBox locates.

In the getView() method of your BaseAdapter, you should do the following :

get a reference to the TextView or the parent view in your list_item.xml file (the layout file you've created for each row of your listview) and in the onCheckedChangedListener method, set the "backgroundcolor" property of that view , to the desired color. The parent view of each row item maybe a RelativeLayout or LinearLayout. whatever the parent, you have access to the "backgroundcolor" property.

listitem_relativelayout.setBackgroundColor(
                 getApplicationContext().getColor(R.color.yourcolor));

Although I'm not sure about the exact syntax, but the solution is definitely this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top