set focus on any item of listview in android
Asked Answered
A

6

10

I have a listview which contains textviews as its elements.

  1. Now I want the first item of the list to be automatically focused when I launch the application
  2. How can I set the focus on any item of the list when I click on the some other view for example a button?
Anadem answered 2/7, 2010 at 18:31 Comment(0)
H
11

Setting selection and setting focus are two different things. If you want to just setSelection to some item then you can use below code.

    mListView.setSelection(position);

But this definitely does not mean the Listview is focused.For focussing you have to use

    mListView.requestFocus();

For changing focus on click of a button you can place the code on onClick() of the button.

Hypnotherapy answered 21/6, 2012 at 7:57 Comment(0)
A
4

ListView has a setSelected method that takes the index of the item in the list.

Amylose answered 2/7, 2010 at 19:43 Comment(3)
Hi but selected listitem is same as focused listitem?Anadem
AFAIK selection is not the same as focus.Barrio
Hey Robby, what does setItemsCanFocus(boolean) mean?Inkberry
H
2

for me the problem was solved by

listView.setItemsCanFocus(true);
Hernando answered 30/1, 2015 at 7:38 Comment(1)
Then how to enable onItemClick for Listview? Because in this case we need to write separate code for click individual item view.Leishaleishmania
R
1

I guess I was in the same situation. I wanted to be able to control the focus of the listview programmatically with buttons .

One solution is to deal with the setFocusableInTouchMode, but I've never achieved to make it work.

The other solution is to forget about the focus and use a checkable listview. First set your listview to "single choice mode" in XML or in java : Mylistview.setChoiceMode(1)

Then you'll be able to check any item you want with Mylistview.setItemChecked(position, true)

So when you launch the application (OnCreate), use Mylistview.setItemChecked(0, true) to check your first item.

Then if you want your button to select the next item for exemple, use :

Mylistview.setItemChecked(Mylistview.getCheckedItemPosition() + 1, true)

You can specify the look when the item is checked or not and there's different pre-built chekable listviews.

If you want more explanations, see my post

Ramification answered 12/6, 2012 at 14:59 Comment(0)
S
1
  1. Your ListView should be like this:
<ListView
    android:id="@+id/my_list"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_margin="10dp"
    android:choiceMode="none"
    android:focusable="true"
    android:fadeScrollbars="false">
    <requestFocus />
</ListView>
mListView.setSelection(3); //or any other number
Sibert answered 3/4, 2015 at 18:21 Comment(0)
T
0

I know this is an old question, but in my case I was trying to programmatically focus a menu option in a listView to highlight the item and none of these worked for me. This is what worked:

MenuOption model:

public class MenuOption {

    public static final int ALL_ITEMS = 0;
    public static final int CATEGORIES = 1;
    public static final int DISCOUNTS = 2;

    private int id;
    private String name;
    private String description;
    private boolean isFocused;

    public MenuOption(int id, String name, String description, boolean isFocused) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.isFocused = isFocused;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isFocused() {
        return isFocused;
    }

    public void setFocused(boolean focused) {
        isFocused = focused;
    }
}

MenuAdapter:

public class MenuAdapter extends BaseAdapter {

    private ArrayList<MenuOption> menuOptions; // Original Values
    private LayoutInflater inflater;
    private MenuOptionListener listener;
    private Context context;

    public MenuAdapter(Context context, ArrayList<MenuOption> menuOptions, MenuOptionListener listener) {
        this.context = context;
        this.menuOptions = menuOptions;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    private class ViewHolder {
        TextView menuName;
    }

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

        ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_menu_option, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.menuName = convertView.findViewById(R.id.menu_name);

        MenuOption option = menuOptions.get(position);
        holder.menuName.setText(option.getName());
        if (option.isFocused()) {
            convertView.setBackgroundResource(R.color.menu_focus);
            holder.menuName.setTextColor(ContextCompat.getColor(context, R.color.white));
            holder.menuName.setTypeface(null, Typeface.BOLD);
        } else {
            convertView.setBackgroundResource(R.color.menu_un_focus);
            holder.menuName.setTextColor(ContextCompat.getColor(context, R.color.black));
            holder.menuName.setTypeface(null, Typeface.NORMAL);
        }
        convertView.setOnClickListener(view -> {
            if (!option.isFocused()) {
                resetFocus(position);
                listener.onClicked(option);
            }
        });

        return convertView;

    }

    public void resetFocus(int position) {
        for (int i = 0; i < menuOptions.size(); i++) {
            menuOptions.get(i).setFocused(i == position);
        }
        notifyDataSetChanged();
    }

    public interface MenuOptionListener {
        void onClicked(MenuOption option);
    }
}

And then in your Activity, you can call menuAdapter.resetFocus(0).

Tippler answered 5/11, 2021 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.