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?

3
  • You do not use the menu.xml to setup your tabs - you do that in Java code. Commented May 31, 2015 at 17:39
  • In menu.xml file I have ID of my tab. Probably, I can use it in some way for connect. But I didn't find this way. Is creating tabs from code only way? Commented May 31, 2015 at 21:30
  • I believe so - you will have to do it in Java code. Have a look at freg's answer for using Java code for tabs. Commented May 31, 2015 at 21:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.