Last active
April 11, 2021 13:40
-
-
Save mohsin/8c9df2838d9a52830eb9 to your computer and use it in GitHub Desktop.
Function to convert Android menu.xml into string array for use in ArrayAdapter for NavigationDrawer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.content.Context; | |
import android.support.v7.view.menu.MenuBuilder; | |
public class StringUtils { | |
/* | |
* Usage: String[] mMenuItems = StringUtils.getArrayFromMenu(this, R.menu.menu_main); | |
* And then mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.item_listview_drawer, mMenuItems)); | |
*/ | |
public static String[] getArrayFromMenu(Context context, int menu_main) | |
{ | |
Menu menu = new MenuBuilder(context); | |
new MenuInflater(context).inflate(menu_main, menu); | |
String[] temp = new String[menu.size()]; | |
for(int i = 0; i < menu.size(); i++) | |
temp[i] = menu.getItem(i).getTitle().toString(); | |
return temp; | |
} | |
private StringUtils() { | |
//No instances. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Alot