1

I have created ActionBar with some number of tabs dynamically like the below code.

public void addTabBar(Context context)
{       
    sActiveContext=context;      
    sActionBar = getActionBar(); 
    sActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}  

public void addTabItem(final String url, String tabTitle)
{   
    arrayList.add(url);
    Tab tab = sActionBar.newTab();
    if(tabTitle.equals(""))
    {
        int childcount=sActionBar.getTabCount();
        tabTitle="Tab" + String.valueOf(childcount+1);          
    }
    tab.setText(tabTitle);          
    tab.setTabListener(this);
    sActionBar.addTab(tab);        
}

  @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        linearLayout=new LinearLayout(sActiveContext);      
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
        linearLayout.setOrientation(LinearLayout.VERTICAL); 
        setContentView(linearLayout);
        CustomWebView webview=new CustomWebView(sActiveContext);
        FrameLayout layout=webview.createwebview();

        for (int i = 0; i < arrayList.size(); i++) {
            if(tab.getPosition()==i)
            {
                webview.initwebview(arrayList.get(i));
                break;
            }
        }
    linearLayout.addView(layout);
    }

If I have converted this code as library and call those methods and I can create n number of tabs in action bar. Now, I wish to add menu items and dropdown menu(ellipse with three dots) like in the link below. http://developer.android.com/guide/topics/ui/actionbar.html#Tabs If I am passing the image to the showMenu(R.drawable.menu_image) method, the menu items have to be dynamically created. How can I achieve this? Please give some suggestions.

Karthick
  • 1,241
  • 5
  • 20
  • 43

2 Answers2

1

Try this, In your Activity,

private Menu menu=null;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   this.menu=menu;
   //  call here as, showMenu(R.drawable.menu_image,2222);
   return true;
}
private void showMenu(int imageIcon,int id)//Where imageIcon is R.drawable.menu_image
{
   menu.add(0, id, 0, actionString).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
   MenuItem item=menu.findItem(id);
   item.setIcon(imageIcon);
}
Sino Raj
  • 6,431
  • 2
  • 22
  • 23
  • Mr. Karthik, check this answer. – Sino Raj Apr 16 '13 at 07:23
  • I have checked.But the menu is always null. I have called the shoMenu() method in the onCreate() method. – Karthick Apr 16 '13 at 07:31
  • 1
    When call showMenu() on onCreate, the menu is null.Because onCreateOptionsMenu(Menu menu) is calls after of onCreate. So you should call that funtion inside of onCreateOptionsMenu(Menu menu) after the being menu initialized. See above answer... – Sino Raj Apr 16 '13 at 08:02
  • Now executed successfully.Is any other way to call the showMenu() method, outside the onCreateOptionsMenu() method. – Karthick Apr 16 '13 at 08:23
  • You can call anywhere, only after the onCreateOptionsMenu() executed like view pager transaction, button click etc. – Sino Raj Apr 16 '13 at 08:27
  • Yes, this is what I really need. Many thanks is not enough to say. – Karthick Apr 16 '13 at 08:33
  • Could you please give me some suggestions to enable the overflow menu(three dots) and how can I listen the click of the overflow menu. – Karthick Apr 16 '13 at 08:36
  • item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); this line is needed for displaying the menu items. – Karthick Apr 16 '13 at 08:42
  • Yes, I have got expected UI. Now, have to add the overflow menu dynamically. – Karthick Apr 16 '13 at 09:25
  • Are you have account in facebook?, if yes search Sino Raj their had a account, You may add friends request.. – Sino Raj Apr 16 '13 at 09:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28299/discussion-between-karthick-and-sino-raj) – Karthick Apr 16 '13 at 09:42
  • Yes, I am having, but now I am restricted to login into facebook. Are you having the Google Plus account? shall I give you my mail id.? – Karthick Apr 16 '13 at 09:54
  • raisingkingkarthick@gmail.com This is my mail id – Karthick Apr 16 '13 at 11:10
  • Thanks a lot Sino. If you have code for enabling the overflow menu (three dots) dynamically means. Please send that too. I need enabling the overflow menu and listening the click event of the overflow menu. That's all. – Karthick Apr 16 '13 at 11:24
  • In action bar, the menu items are bundled together in a single icon/item(3 dots) has their on process when the large no; of menu items are added. For checking these, to add 5 or more items in to action bar. – Sino Raj Apr 16 '13 at 11:31
  • If I am adding more than three menu items using the showMenu() method, only two menu icons are displayed in portrait mode. In landscape mode, three menu items are displayed. There is no display of the 3 dots menu. – Karthick Apr 16 '13 at 12:03
  • Can you please give the facebook details to give request to you.? This is my FB id.https://www.facebook.com/raisingkingkarthick. – Karthick Apr 16 '13 at 15:15
  • Is the overflow menu is not displayed in the devices which having the hardware menu? If I need the overflow menu button at the right top of the layout in the devices having physical menu button? – Karthick Apr 16 '13 at 15:35
  • Add menus as sub menus(means menu.add(submenu)),try this, for seeing menu's inside of 3 dots – Sino Raj Apr 18 '13 at 12:29
  • Hi Sino,I have fixed that issue. Now, I have create a class TabFragmentClass for all the tabs, and get the tab position and loaded the url into the webview like in this post http://stackoverflow.com/questions/16077002/fragment-webview-java-script-function-is-not-working/. The fragment is shown fine. But If I am calling the native java method using the javascript function, the url loaded into the webview is always the lastly visited url, not the current url. How can I fix this issue.? Please help me? – Karthick Apr 18 '13 at 12:58
  • Hi, I am successfully created the menu items by calling the showMenu() method which was called in the activity start. I have created 4 tabs.But for all the 4 tabs the menu items is always static. How can I dynamically change the menu items. That is, Now, I am displaying save,cancel menu item for tab1, I need to display choose color,close in the tab2. How can I achieve this like the showMenu() method as updateMenu().? – Karthick Apr 21 '13 at 15:44
  • Hi,Initially I'm using Android 4.1.2 device for testing in which the menu items are displayed properly. But in the Android 4.0.4 tab is created successfully, the menu is also added, but the menu items is not at all visible. Is there any other options needed for showing the menu items in the Android 4.0.4 device. – Karthick Apr 23 '13 at 18:03
0

try this for handle all menu's for activity, and mange all menu's for fragment transaction on tab change,

private Menu menu=null;

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   this.menu=menu;
   //  call here as, showMenu(R.drawable.menu_image,2222);
   // add all menu's when that needed in your activity like save,cancel,choose color,  //close etc.
   menuHandler(0);
   return true;
}
private void showMenu(int imageIcon,int id)//Where imageIcon is R.drawable.menu_image
{
   menu.add(0, id, 0, actionString).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
   MenuItem item=menu.findItem(id);
   item.setIcon(imageIcon);
}
    private menuHandler(int position)
{
if(position==0)//For tab 1
{
if(!menu.findItem(1111).isVisible())    //check menu item with id 1111 is visible
    menu.findItem(1111).setVisible(true);
if(menu.findItem(2222).isVisible()) 
    menu.findItem(2222).setVisible(false);
}
else if(position==0)
{
if(menu.findItem(1111).isVisible()) 
    menu.findItem(1111).setVisible(false);
if(menu.findItem(2222).isVisible())
    menu.findItem(2222).setVisible(false);
}
else
{
if(menu.findItem(3333)
        menu.findItem(3333).setVisible(false);
if(!menu.findItem(2222).isVisible())
    menu.findItem(2222).setVisible(true);
}
}
@Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) 
    {}
    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) 
    {
menuHandler(tab.getPosition());
}
Sino Raj
  • 6,431
  • 2
  • 22
  • 23
  • Thanks Sino. Let me try. Can you please give me the suggesstions about this http://stackoverflow.com/questions/16139259/changing-actionbar-tabs-underline-color-progrmatically. – Karthick Apr 22 '13 at 04:16
  • Hi, This is working fine. I'm loading the webview into the tabs. If I am click a link that's goes to some other page in the first tab. I need to update the menu items in that specific page. How can I achieve this? Please help me. – Karthick Apr 22 '13 at 06:33
  • Is there any options to delete the menu items.? Instead of giving the image R.id.save like this, how can I add the image from the path to the menu item icon.? – Karthick Apr 22 '13 at 07:16
  • You just remove 'android:targetSdkVersion=" " ' from manifest – Sino Raj Apr 24 '13 at 04:35