2

i'm struggling with the ActionBar in android.

heres my problem: my Action items are not shown in the ActionBar, instead they are stacked in the action overflow, no matter what i do ..

i have spent a hole day looking for a solution to this but i can't seem to find whats missing .. :-s

i tried setting android:showAsAction to "Always" : it didnt work. when i change ifRoom to Always, the logcat says : testmenu.xml:5: error: Error: String types not allowed (at 'showAsAction' with value 'Always'). + it doesnt campile at all.

i've tried a lots of differents utorials and posts here , but unfortunately i cant find a solution..

heres my code:

Mainactivity.java package com.locklock;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
//import android.support.v7.app.ActionBar.Tab;
//import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    // Set up the action bar to show tabs.     
    final ActionBar actionBar= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //        actionBar.setDisplayShowTitleEnabled(false);

    // adding tabs
    ActionBar.Tab tab1=actionBar.newTab();
    tab1.setText("TEST");
    tab1.setTabListener(this);

    ActionBar.Tab tab2=actionBar.newTab();
    tab2.setText("EVENTS");
    tab2.setTabListener(this);

    ActionBar.Tab tab3=actionBar.newTab();
    tab3.setText("USERS");
    tab3.setTabListener(this);

    ActionBar.Tab tab4=actionBar.newTab();
    tab4.setText("SETTINGS");
    tab4.setTabListener(this);

    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);
    actionBar.addTab(tab4);        

   // tab = getActionBar.newTab();
   // tab.setText(tabText);
   // tab.setIcon(R.drawable.tab_icon);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.action_users, menu);
    //return true;        
    //MenuInflater Mymenu = getMenuInflater();
    //Mymenu.inflate(R.menu.action_users, menu);

    return super.onCreateOptionsMenu(menu);        
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings:
            // todo
            return true;
        case R.id.action_add_user:
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            return true;
        case R.id.action_set_permission:
            // todo
            return true;
        case R.id.action_delete_user:
            // todo
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

    //if (id == R.id.action_settings) {
    //    return true;
    // }
    // return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    Log.e("mytag","test");
}


@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    Log.d("mytag","xxxx");
}


@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    //Log.d("mytag","xxxx");
}


}

action_users.xml

<!-- Add User -->
<item android:id="@+id/action_add_user"
      android:icon="@drawable/ic_action_remove"
      android:title="@string/add_user"
      android:showAsAction="ifRoom" />

 <!-- Set permission for a User-->
<item android:id="@+id/action_set_permission"
      android:icon="@drawable/ic_action_remove"
      android:title="@string/set_permission"
      android:showAsAction="ifRoom" /> 

 <!-- delete User-->
<item android:id="@+id/action_delete_user"
      android:icon="@drawable/ic_action_remove"
      android:title="@string/delete_user"
      android:showAsAction="ifRoom" /> 

androidmanifest.xml

 <?xml version="1.0" encoding="utf-8"?> 
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.locklock"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:uiOptions="splitActionBarWhenNarrow" >
        <activity
            android:name="com.locklock.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

can anyone outhere take a quick look into this please?

in advance, thanks a lot .

2
  • You have to use always with a lowercase 'a'
    – Karakuri
    Jul 10, 2014 at 14:54
  • thanks . thats a little better , i can compile it now. but still , the action items are stacked under the "action overflow"
    – Asma_15
    Jul 10, 2014 at 15:32

2 Answers 2

1

after a few days of testing , i finally found the answer .

in the xml menu file, i had to change this :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myappname="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      myappname:showAsAction="always"  />    
</menu>

to this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.myappname.MainActivity" >

<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      app:showAsAction="always" /> 
</menu>

and it worked .

0

Try to set yourapp in action_users.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      yourapp:showAsAction="ifRoom"  />
...
</menu>
1
  • i tried it before, it doesnt seem to work. i'm targeting API level 18 at minimum , so it is not useful here i think .? (maybe i'm wrong)
    – Asma_15
    Jul 10, 2014 at 14:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.