Icon Not Showing Up In Activity Bar - android

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>

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

Related

SearchView is not displayed on toolbar with nested Fragments

I must have read almost all the question and answers related to this, but still cannot get it to work. Below is the rough sketch of how the app looks.
Adding code below to give structure of the layout and where I might be doing wrong.
activity_main.xml
<android.support.constraint.ConstraintLayout >
<android.support.design.widget.BottomNavigationView>
</android.support.design.widget.BottomNavigationView>
<FrameLayout>
</FrameLayout>
bottom_nav_fragment.xml
<android.support.design.widget.CoordinatorLayout >
<android.support.design.widget.AppBarLayout
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/liquor_type_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:tabMode="scrollable">
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/price_list_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
For each of tab item, I have used a common fragment which will have different data in recyclerView.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.nandu.oru10ml.price_list.CommonPriceFragment">
<android.support.v7.widget.RecyclerView >
</android.support.v7.widget.RecyclerView>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater inflater = getS
Log.d("Activity Options", "inside onCreateOptionsMenu ");
getMenuInflater().inflate(R.menu.search_item, menu);
return true;
}
BottomNavFragment.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment onCreate", "inside onCreate ");
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Fragment Options", "inside onCreateOptionsMenu ");
inflater.inflate(R.menu.search_item, menu);
super.onCreateOptionsMenu(menu,inflater);
}
CommonFragment.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment onCreate", "inside onCreate ");
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Fragment Options", "inside onCreateOptionsMenu ");
inflater.inflate(R.menu.search_item, menu);
super.onCreateOptionsMenu(menu,inflater);
}
and finally, search_item.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/action_search"
android:title="Search for Brand"
android:icon="#drawable/ic_search_black_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView">
</item>
</menu>
The reason why it is not working would also help me understand the root cause.
This SearhView is applicable to 2 of the bottom nav Fragment and all of the nested Fragments inside, to search Recyclerview present in each of it.
setSupportActionBar should take the argument as a toolbar. So what you should do is, inside your fragment call
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar)

Adding Android studio main menu on actionbar

Please give me an advise .How to add the menu icon(that three lines symbol) for an already created project. Also I wanna add the menu icon only for some layouts.
Please help
would be very thankful..I mean this symbol
To create menu in your application first create on Folder inside res directory named menu.
Then in menu create one file main_menu.xml
Add below code into main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Item1" android:id="#+id/item1"></item>
<item android:title="Item2" android:id="#+id/item2"></item>
</menu>
Here title which show in your menu.
To add menu in any activity put below code in your activity file.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.item1:
//your action
break;
case R.id.item2:
//your action
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
// enables the activity icon as a 'home' button.
getActionBar().setHomeButtonEnabled(true);
You can use hamburger icon as in toolbar.
<RelativeLayout 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"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wolfmatrix.dummy.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarId"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/toolbarTextId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:textColor="#android:color/white"
android:textSize="14sp" />
</android.support.v7.widget.Toolbar>
<ImageButton
android:id="#+id/backButtonIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="left"
android:padding="10dp"
app:srcCompat="#drawable/ic_hamburger_icon_black_24dp" />
</RelativeLayout>
If you are using Toolbar in your layout XML then you can change Home icon as below:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.hamburger_icon);

Android - App bar action buttons disappearing when device orientated landscape

I've set my app so it changes to a different layout when the device is orientated, which works fine but now my action buttons are not being displayed. Do I need to create a new app bar xml file for the landscape orientation? Here is my code, appreciate any help.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
if (savedInstanceState != null) {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
setContentView(R.layout.activity_main_landscape);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml - I want this to be used in both landscape and portrait orientations, meaning I want the same action buttons on both.
<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.james.task02b.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/action_search"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_delete_white_24dp"
android:title="#string/action_delete"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
activity_main_landscape.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.james.task02b.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is the landscape layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/textView2" />
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Is adding a colored icon to a transparent toolbar possible?

Is it possible to add a circular icon like this to a transparent Toolbar (styled like an action bar), and maintain its coloring?
EDIT: this icon will be right aligned on the toolbar, similar to where you would see the three dot menu icon in most apps.
You want to inflate a menu for the toolbar, set the background to transparent, inflate the menu with the icon.
Activity with the Toolbar
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getSupportActionBar().hide();
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello World");
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
// Your action here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
}
activity layout
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="56dp"
android:layout_width="match_parent"
app:theme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="5dp"
android:background="#android:color/transparent" />
</LinearLayout>
Menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/icon"
android:title="Icon Title"
android:icon="#drawable/ic_launcher"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>

How to add a switch to android action bar?

I would like to add a button switch similar to jellybean native look. (Blue/gray switch at the top of the view)
Documentation shows how to create a menu there or add icons, but it does not say, how to add a custom elements. eg. a switch.
http://developer.android.com/guide/topics/ui/actionbar.html
Create a layout for the switch switch_layout.xml. Custom layouts for menu should always be RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Then, in your mainmenu.xml add the item as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
</menu>
And in your activity, inflate the mainmenu.xml as you always do
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
Finally figured out my problem: for those that's using the new AppCompat, you should be using android.support.v7.widget.SwitchCompat instead of Switch on the switch layout...otherwise, it won't show on the ActionBar (assumed you're using AppCompat ActionBar as well), well, the actionLayout attribute doesn't work, it has to be set in the code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.SwitchCompat
android:id="#+id/switchForActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Then set the layout in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.on_off_switch);
item.setActionView(R.layout.on_off_switch);
return true;
}
If the widget is not appearing in the action-bar it is probably because you are using appCompat for your action-bar. To solve this switch "android:" to "app:" in front of "showAsAction" and "actionLayout" in your menu.xml
Add item to xml, with app: in place of android:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/switch_layout"
/>
</menu>
Make layout that you are using for your "app:actionLayout"
switch_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Inflate the menu in your ActionBarActivity as you would normally
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
This should make the switch appear in your action-bar, if it was not appearing.
For those who want to add,
Checked change Listener to the same Switch
Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val item = menu!!.findItem(R.id.my_switch_item)
item.setActionView(R.layout.switch_layout)
val mySwitch = item.actionView.findViewById(R.id.switch_id)
mySwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(p0: CompoundButton?, isChecked: Boolean) {
// do what you want with isChecked
}
})
return true
}
Java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.my_switch_item);
item.setActionView(R.layout.switch_layout);
Switch mySwitch = item.getActionView().findViewById(R.id.switch_id);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something based on isChecked
}
});
return true;
}
P.S. You can change reference to Switch or SwitchCompat
The solution given by Ezequiel is awesome and works. Here goes another approach:
Define your custom layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<Switch
android:id="#+id/actionbar_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Inflate it programatically:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
Switch button = (Switch) findViewById(R.id.actionbar_switch);

Categories

Resources