2

I'm new in Android. I stumbled on connecting tabs from menu with code.

Here is my menu.xml file:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/multiTouchExample"
                android:title="MultiTouch"
                android:icon="@drawable/ic_multitouch_grey"
                android:showAsAction="ifRoom">
            </item>
</menu>

And here is my MyActivity.java:

//includes

public class MyActivity extends Activity {

final String LOG_TAG = "TAGLogs";
ActionBar actionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    actionBar = this.getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Log.d(LOG_TAG, "OnCreate start");
    Log.d(LOG_TAG, new Integer(actionBar.getTabCount()).toString());
}
}

And This I have in logcat:

>     D/TAGLogs﹕ OnCreate start
>     D/TAGLogs﹕ 0

So it says me, that I have 0 tabs, but I have 1 in menu.xml and see it when run the program. Where is it? What I need to do?

This is screenshot of working: screenshot of working

UPDATE: When I want to link button from res/layout/main.xml with code:

button = (Button) findViewById(R.id.button);

So I don't create button with operator new in code. Is there some way to solve this problem like with button?

Community
  • 1
  • 1
RevakoOA
  • 601
  • 1
  • 9
  • 20

2 Answers2

2

So, I found the solution on StackOverflow but in another context.

Here is this answer:

Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu

First, you should save the reference to the menu:

publid boolean onCreateOptionsMenu(MenuItem menu){
    getMenuInflater().inflate(R.menu.menu, menu);
    this.menu = menu;// this.menu is just field Menu menu in MyActivity class
    return true;
}

Via this reference you have full access to menuItems.

For example:

MenuItem item = menu.findItem(R.id.multiTouchExample);
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            item.setIcon(R.drawable.ic_multitouch_red);
            MultiTouchExample();
            return true;
        }
    });

After pressing

Community
  • 1
  • 1
RevakoOA
  • 601
  • 1
  • 9
  • 20
1

This might help you:

http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Notice that setContentView() is not used, because we use the root
    // android.R.id.content as the container for each fragment

    // setup action bar for tabs
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab()
                   .setText(R.string.artist)
                   .setTabListener(new TabListener<ArtistFragment>(
                           this, "artist", ArtistFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
               .setText(R.string.album)
               .setTabListener(new TabListener<AlbumFragment>(
                       this, "album", AlbumFragment.class));
    actionBar.addTab(tab);
}
freg
  • 133
  • 1
  • 8