'ExpandableListview set bold and normal text when click menu

I am working on my expandablelistview menu that when I click on the menu item, I want to set the menu item to bold while set the other menu item to normal textype.

ExpandableListAdapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    private HashMap<String, String> imgnamedata;
    private boolean hasChild = false;
    private int selectedItem;


    public ExpandableListAdapter(MainActivity context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild, HashMap<String, String> img_data) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listDataChild;
        this.imgnamedata = img_data;
    }


    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(
                this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }


    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return childPosition;
    }

    public boolean isHasChild (boolean hasChild) {

        return hasChild;
    }

    public void selectItem(int selectedItem){
        this.selectedItem = selectedItem;
        notifyDataSetChanged();
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition,
                childPosition);
        //final HeaderModel header = (HeaderModel) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item1, null);
        }
        final TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
        //txtListChild.setTypeface(null, Typeface.NORMAL);
        txtListChild.setText(childText);


        txtListChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChildSelectable = true;
                txtListChild.setTypeface(null, Typeface.BOLD);
                mDrawerLayout.closeDrawers();
            }
        });

        return convertView;
    }

    private boolean isHasChild() {
        return hasChild;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(
                this._listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);

        int c_size = getChildrenCount(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        // changing the +/- on expanded list view

        // this is the +/- text view that act as image.
       // on expand this change to  - and on close it again change to +

        ImageView txt_plusminus = (ImageView) convertView
                .findViewById(R.id.plus_txt);

        if (c_size > 0) {
            txt_plusminus.setVisibility(View.VISIBLE);

            if (isExpanded) {
                txt_plusminus.setImageDrawable(this._context.getResources().getDrawable(R.drawable.downarrow));
            } else {
                txt_plusminus.setImageDrawable(this._context.getResources().getDrawable(R.drawable.rightarrow));

            }

        } else {
            txt_plusminus.setVisibility(View.GONE);
        }


        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        // lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        lblListHeader.setTypeface(null, Typeface.BOLD);


        // Either you can use this below for setting icon in main menu:

        // adding icon to expandable list view
        ImageView imgListGroup = (ImageView) convertView
                .findViewById(R.id.ic_txt);

        String icn = this.imgnamedata.get(headerTitle);
        if (icn != null) {

            String uri = "@drawable/" + icn;

            int imageResource = getResources().getIdentifier(uri, null, getPackageName());
            Drawable res = null;
            try {
                res = getResources().getDrawable(imageResource);
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }

            if (res != null) {
                imgListGroup.setImageDrawable(res);

            } else {
                imgListGroup.setImageResource(R.drawable.ic_launcher_background);

            }
        } else {
            imgListGroup.setImageResource(R.drawable.ic_launcher_background);
        }


        return convertView;
    }

MainActivity:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

ExpandableListView interface1,if_list,adons_list,ss_list,tool_list;
ImageView close_d;
Toolbar toolbar;
WebView webview;
TextView lblListHeader;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
RelativeLayout drawer_relative, interface1_test;
private boolean isSelected = false;
private boolean isChildSelectable = false;
private String selectedText = null;

private DrawerLayout.DrawerListener mDrawerListener = new DrawerLayout.DrawerListener() {

    @Override
    public void onDrawerStateChanged(int status) {

    }

    @Override
    public void onDrawerSlide(View view, float slideArg) {

    }

    @Override
    public void onDrawerOpened(View view) {
         // toolbar.setTitle(mDrawerTitle);
        // calling onPrepareOptionsMenu() to hide action bar icons
       //  invalidateOptionsMenu();
    }

    @Override
    public void onDrawerClosed(View view) {
        // toolbar.setTitle(mTitle);
        // calling onPrepareOptionsMenu() to show action bar icons
       // invalidateOptionsMenu();
    }
};

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    close_d = findViewById(R.id.close_d);

    drawer_relative = findViewById(R.id.drawer_relative);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setDrawerListener(mDrawerListener);
    interface1 = (ExpandableListView) findViewById(R.id.interface1);
    if_list = (ExpandableListView) findViewById(R.id.if_list);
    adons_list = (ExpandableListView) findViewById(R.id.adons_list);
    ss_list = (ExpandableListView) findViewById(R.id.ss_list);
    tool_list = (ExpandableListView) findViewById(R.id.tool_list);
    lblListHeader = (TextView) findViewById(com.dvinfosys.R.id.lblListHeader);
    webview = (WebView) findViewById(R.id.webview);
    isSelected = false;


    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            toolbar, // nav menu toggle icon
            R.string.app_name, // nav drawer open - description for
            // accessibility
            R.string.app_name // nav drawer close - description for
            // accessibility
    ) {
        public void onDrawerClosed(View view) {
            // toolbar.setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            // invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            // toolbar.setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            // invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    ArrayList dashbord_listDataHeader = new ArrayList<String>();
    HashMap<String, List<String>> dashbord_listDataChild = new HashMap<String, List<String>>();
    HashMap<String, String> img_data = new HashMap<>();
    dashbord_listDataHeader.add("Dashboard");
    dashbord_listDataChild.put("Dashboard",new ArrayList<String>());
    img_data.put("Dashboard","dashboard");
    ExpandableListAdapter listAdapter = new ExpandableListAdapter(MainActivity.this, dashbord_listDataHeader,
            dashbord_listDataChild,img_data);
    interface1.setAdapter(listAdapter);


    interface1.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);

            selectedText = "Dashboard";
            TextView dashboard = v.findViewById(R.id.lblListHeader);
            dashboard.setTypeface(null, Typeface.BOLD);
            mDrawerLayout.closeDrawers();
            return false;
        }
    });


    HashMap<String, String> img_data1 = new HashMap<>();
    HashMap<String, List<String>> interface_listDataChild = new HashMap<String, List<String>>();
    ArrayList interface_listDataHeader = new ArrayList<String>();
    List<String> sub_data = new ArrayList<>();
    List<String> sub_data1 = new ArrayList<>();
    interface_listDataHeader.add("Contacts");
    interface_listDataHeader.add("Newsletters");
    sub_data.add("Search Contacts");
    sub_data.add("Add Contacts");
    sub_data.add("Import Contacts");
    sub_data1.add("Create Newsletter");
    sub_data1.add("My Newsletters");
    interface_listDataChild.put("Contacts", sub_data);
    interface_listDataChild.put("Newsletters", sub_data1);
    img_data1.put("Contacts","contacts");
    img_data1.put("Newsletters","newsletter");

    ExpandableListAdapter listAdapter1 = new ExpandableListAdapter(MainActivity.this, interface_listDataHeader,
            interface_listDataChild,img_data1);
    if_list.setAdapter(listAdapter1);

    if_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);

            return false;
        }
    });

    ArrayList addons_listDataHeader = new ArrayList<String>();
    HashMap<String, String> img_data2 = new HashMap<>();
    HashMap<String, List<String>> addons_listDataChild = new HashMap<String, List<String>>();
    List<String> sub_data2 = new ArrayList<>();
    addons_listDataHeader.add("Autoresponder");
    addons_listDataHeader.add("My Campaign");
    addons_listDataHeader.add("Split Tests");
    sub_data2.add("Create Autoresponder");
    sub_data2.add("My Autoresponder");
    addons_listDataChild.put("Autoresponder", sub_data2);
    addons_listDataChild.put("My Campaign",new ArrayList<String>());
    addons_listDataChild.put("Split Tests",new ArrayList<String>());
    img_data2.put("Autoresponder","autoresponder");
    img_data2.put("My Campaign","campaign");
    img_data2.put("Split Tests","splittest");
    ExpandableListAdapter listAdapter2 = new ExpandableListAdapter(MainActivity.this, addons_listDataHeader,
            addons_listDataChild,img_data2);



    adons_list.setAdapter(listAdapter2);
    adons_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);

            TextView addon_header = v.findViewById(R.id.lblListHeader);

            if (id == 1) {
                selectedText = "Campaign";
            }
            else if (id == 2) {
                selectedText = "Split Tests";
            }
            addon_header.setTypeface(null, Typeface.BOLD);

            mDrawerLayout.closeDrawers();
            return false;
        }
    });
}

When I click on the menu item, example: My Newsletter. The menu item "Dashboard" have a bold so I want to set the "Dashboard" to normal typetext and set the menu item "My Newsletter" to bold typetext. And when I click menu item "Create Newsletter", I want to set the the "My Newsletter" to normal typetext and set the menu item "Create Newsletter" to bold.

On my code, what it will do that it will set the menu item to bold when I click on the menu item but it wont set the other menu item to normal.

Can you please show me an example how I could do that using with my code?

EDIT: I have tried this:

ss_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);

            TextView ss_header = v.findViewById(R.id.lblListHeader);
            ss_header.setTypeface(null, Typeface.NORMAL);

            if (id == 0) {
                //do something
            }
            else if (id == 1) {
                 //do something
            }

            ss_header.setTypeface(null, Typeface.BOLD);
            mDrawerLayout.closeDrawers();
            return false;
        }
    });`


Solution 1:[1]

you can try this for changing the text style

button/textView.setTypeface(Typeface.DEFAULT_BOLD/NORMAL);

and on each button press, set the text as you wish. I hope it could help

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 schtalman