DaynaJuliana
DaynaJuliana

Reputation: 1164

Icon Not Showing Up In Activity Bar

I'm having issues getting my action bar to show icons. It shows the text/title I set in the overflow menu (three dots), but no action. Here is my code, what am I missing?

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vehicle_table);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.vehicle_table, new VehicleTable())
                .commit();
    }
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
    getSupportActionBar().setCustomView(R.layout.custom_vehicle_action_bar_layout);
}

VehicleTable.java (my fragment)

public class VehicleTable extends Fragment {

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.menu_vehicle_table, menu);
}

menu_vehicle_table.xml

<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.roadstruck.mobile.VehicleTable">


<item android:id="@+id/add_vehicle"
    android:icon="@drawable/ic_add_white_24dp"
    android:orderInCategory="100"
    android:title="Add"
    android:showAsAction="always"
    />
</menu>

custom_vehicle_action_bar_layout (custom textview for centering menu text)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="VEHICLES"
    android:textColor="#ffffff"
    android:id="@+id/mytext"
    android:textSize="18sp" />
</LinearLayout>

Upvotes: 1

Views: 188

Answers (1)

Derek Fung
Derek Fung

Reputation: 8211

Try change android:showAsAction="always" to app:showAsAction="always"

Upvotes: 1

Related Questions