0

I'm not really new to programming but a complete fresh noob regarding android and java.

I recently made the decision to programm an Android App using this tutorial. I got till the actionbar and can even display some actions like search and settings in the overflow. What I cant manage to do is to let an action appear on the actionbar(with or without icon).

I managed to it once with this tutorial but I somehow cant repeat it on my actual "Tutorialapp".

Here is my code:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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.example.newapp.MainActivity" >

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

MainActivity.java:

package com.example.newapp;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.newapp.MESSAGE";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    switch (item.getItemId()) {

    case R.id.action_search:
        openSearch();
        return true;

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

private void openSearch() {
    Toast.makeText(this, "This is a Search Button", Toast.LENGTH_SHORT).show();

}

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);
}
}
Levaru
  • 51
  • 4

2 Answers2

0

try swapping the menu items:

<?xml version="1.0" encoding="utf-8"?>
<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:orderInCategory="101"
   android:title="@string/action_settings"
   app:showAsAction="never"/>
</menu>
Parnit
  • 1,032
  • 2
  • 8
  • 16
  • This is weird. The only other option i can think of is to change the menu tags as I have done in my edit above. – Parnit Mar 12 '14 at 22:45
  • Maybe is my Eclipse faulty? I cant seem to create a Virtual Device, my items wont show up in the string list and various other things... – Levaru Mar 12 '14 at 23:25
-2

I dont know why, but you need to specify in java code the action always. I had the same problem, and I solved it like that:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    menu.findItem(R.id.actionSearch).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
} 

Hope it solve your problem. :D!! Good Luck!

Krayo
  • 2,492
  • 4
  • 27
  • 45