gedr
gedr

Reputation: 316

Can't create action buttons

I am currently trying to learn android programming and have encountered a problem. When I try to complete the Action Bar tutorial it does not work. It runs, but on the Action Bar only the overflow button diplays and that contains the settings button. I have no idea why this does not work. Here is the code:

package com.example.usingui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.usingui.MESSAGE";
@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();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

}

/**
 * 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;
    }
}
}

and res/menu/main.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" />
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />
</menu>

I have no idea what I am doing wrong. Please help me!

Upvotes: 0

Views: 48

Answers (3)

user3705399
user3705399

Reputation:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.action_settings:
            //Do something here
            return true;

        case R.id.action_search:
            //do something here
            return true;



        default:
        return super.onOptionsItemSelected(item);
        }
    }

That is my code for my action bar

Upvotes: 1

Zafer Celaloglu
Zafer Celaloglu

Reputation: 1418

@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();
    if (id == R.id.action_settings) {
        return true; ->you can handle every item of action bar and you can do any interaction in here!
    }
    return super.onOptionsItemSelected(item);
}

I am agree with Cybergei you should set visible of every item of action bar by writing android:showAsAction="always" and as I wrote above,you can do anything what you want by handling id of every item inside of the onOptionsItemSelected method

Upvotes: 0

user3705399
user3705399

Reputation:

I see that in your XML for the menu, you have android:showAsAction="never" change it to always for each menu item that you want to see in the action bar so it would be this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

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

<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="always" />

</menu>

Upvotes: 0

Related Questions