I´m trying to add in my ActionBar my app icon, but I was reading on Google Developers and I can get the solution. I´m doing this on my ActivityMain:
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
But it doesn´t work.
This is my first day with Android and I just want make an ActionBar with the main icon on the left and an icon search.
Thank You.
With API21 you should use the new Toolbar class.
Put the Toolbar in your layout:
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="attr/actionBarSize"
android:background="?attr/colorPrimary" />
Then in your code (onCreate in your Activity for example):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_myNavigationIcon);
You can find more info in the official post.
Add:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher)
You should be changing the logo, which is by default the same as the launcher icon.
You should do the following steps to implement ActionBar :
1 - Extend ActionBarActivity like this :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Notice to import this :
import android.support.v7.app.ActionBarActivity;
2 - Add lines below into onCreate :
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
3- Create your xml menu under res/menu/your_menu.xml
somthing like this :
your_menu.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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
4- Inflate the menu to the action bar, and handle the action bar item clicks :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.your_menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Hope this help you!!!
Related
I have a toolbar that has only the menu icon, and it is setted programmatically, like this:
myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
myToolbar.setTitleTextColor(Color.BLACK);
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
Now, i would like to have another icon in the toolbar, and associate to it my src image and onClick Method. As you can see I don't have a xml menu file, and i also don't want to set a default android icon, so how can I perform this?
The main purpose of Toolbar is using custom layout!
You can add whatever UI elements, like button, to your Toolbar and find them in code.
You can set a CustomView to the ActionBar by supplying a layout file or View object. In that custom view you can do whatever you want.
Here is how you can set custom view to the action bar
protected void setCustomActionBarTitle(String title) {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.layout_action_bar_title);
TextView titleView = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
titleView.setText(title);
ImageView customIconImageView = (ImageView) actionBar.getCustomView().findViewById(R.id.custom_icon);
customIconImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call my method on the activity
}
});
ImageView imageView = (ImageView) actionBar.getCustomView().findViewById(R.id.up_icon);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
You're going to have to use a separate xml file to setup your menu and define any extra buttons (item) in there. Here is sample menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
Add the icon which you want in your menu --> menu_main.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=".ui.MainActivity">
<item
android:id="#+id/action_scan"
android:icon="#drawable/camera_icon"
android:orderInCategory="100"
android:title="#string/action_scan"
android:visible="true"
app:showAsAction="always" />
and in your activity
#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;
}
This code is work for me,
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Use this to add your icons into your tool bar. Override these methods and use the code in your project..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_about));
menu.getItem(1).setIcon(getResources().getDrawable(R.drawable.street_view_icon));
return true;
}
//Notification Icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_notify_found:
Intent aboutIntent=new Intent(DashboardActivity.this, HomeAboutActivity.class);
startActivity(aboutIntent);
case R.id.action_notify_found1:
Intent ARStreetviewintent = new Intent(DashboardActivity.this, ARStreetviewActivity.class);
startActivity(new Intent(DashboardActivity.this, StreetviewActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Notification Found -->
<item android:id="#+id/action_notify_found"
android:icon="#drawable/about"
android:title="About"
app:showAsAction="ifRoom"
/>
<!-- Notification Found -->
<item android:id="#+id/action_notify_found1"
android:icon="#drawable/street_view_icon"
android:title="StreetView"
app:showAsAction="ifRoom"
/>
I'm trying to use Android ActionBar in my app, and have an option that's hidden away in the overflow menu.
There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently.
This is in my Activity layout
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="#android:color/white"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And this is in my Activity's onCreate() method
// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);
I've also tried inflating a menu xml file from the onCreateOptionsMenu(), but that also didn't give me the results I wanted.
Define a Menu for your Toolbar in the res/menu resource folder, for example:
toolbar_menu.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=".activity.MainActivity">
<item
android:id="#+id/action_sign_out"
android:title="#string/toolbar_sign_out"
app:showAsAction="never"/>
</menu>
Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.
The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).
In your Activity, set up your Toolbar:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
And override onCreateOptionsMenu() to inflate your previously defined menu resource:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sign_out: {
// do your sign-out stuff
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
in manifest file declare
android:theme="#style/AppTheme.NoActionBar"
like this :
<activity
android:name=".ActivityName"
android:label="#string/label"
android:theme="#style/AppTheme.NoActionBar" />
and add this to your style :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and call this in Activity onCreate() :
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
override this method in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.product_list, menu);
//U can find item set icon and stuff...
MenuItem item= menu.findItem(R.id.action_search);
return true;
}
and declare your menu like this for overflow menu:
<?xml version="1.0" encoding="utf-8"?>
<menu >
<group>
<item
android:id="#+id/sign_out"
android:title="#string/sign_out" />
<item
android:id="#+id/about"
android:title="#string/about" />
</group>
</menu>
and for handle item selection call this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.sign_out:
//do stuff
break;
}
return super.onOptionsItemSelected(item);
}
done :)
Simple do This copy this code on your MainActivet`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, 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) {
Group gp=(Group)findViewById(R.id.order);
return true;
}
return super.onOptionsItemSelected(item);
}`
Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name
and past this code on menu2.xml file
<?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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"
android:icon="#android:drawable/ic_input_add"
/>
</menu>
if any Query Please text me
I don't really know how to put this but all I want is a toolbar (action bar) like the WhatsApp style below.
Meanwhile, I used the Android toolbar and inflated a custom view (which holds the cart button) and added it using the setCustomView() method. I tried tweaking the textSize attribute of the TextView but even the most reasonable size doesn't align the TextView with the rest of the Toolbar menu items. Any help on how to do this? Or is there a different approach?
If I understood you correctly I suggest a different approach, you can set toolbar as a SupportActionBar and add your custom icons as you would do in a normal actionBar
Set your toolbar as SupportActionBar
ex.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Create a menu file as you would do to a normal actionBar
ex.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action1"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="#drawable/custom_icon1"
app:showAsAction="always" />
<item
android:id="#+id/action2"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="#drawable/custom_icon2"
app:showAsAction="ifRoom" />
</menu>
Finally use onCreateOptionsMenu and onOptionsItemSelected to finish to setup.
ex.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, 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.
switch (item.getItemId()) {
case R.id.action_custom_view:
.....Your code.....
break;
case android.R.id.action_custom_view2:
.....Your code.....
break;
}
return false;
}
I am trying to create an android application with custom actionbar.
my code is
package com.sample.actionbar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.action_custom, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0C2640")));
}
#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);
menu.findItem(R.id.action_settings).setVisible(false);
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
But it shows margin on all sides.
I want it to fit with actionbar.
How can i remove this margin?
I tried many codes. but didnt gt what i want.
I had the same Problem. The solution for me was to add the following entries to my ActionBar style:
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
So the parts of my styles.xml related to it are resulting:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/Actionbar</item>
...
</style>
<style name="Actionbar" parent="Widget.AppCompat.Light.ActionBar">
...
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
</resources>
I have done this way:
ViewGroup actionBarLayout = (ViewGroup) contextAct.getLayoutInflater().inflate(R.layout.actionbar_customview,null);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarLayout, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
Hope this will help you.
The right way is to use Toolbar. Create it in your layout xml file like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<!-- Your custom action bar views -->
</android.support.v7.widget.Toolbar>
...
</LinearLayout>
Then in your activity just set the the Toolbar to be your action bar:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
I have been porting Material to an open sourced app. I have used the AppCompat v21 library. I hit the menu key and I get nothing. Everything else works.
What am I doing wrong? I will attach code later as I am sending this from my phone.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_view_source" android:title="#string/action_view_source" />
<item android:id="#+id/action_view_translation" android:title="#string/action_view_translation" yourapp:showAsAction="never" />
<item android:id="#+id/action_view_bugs" android:title="#string/action_view_bugs" yourapp:showAsAction="never" />
</menu>
Java
public class MainActivity extends ActionBarActivity
…
#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;
}
Nevermind, I figured it out. It appears that the Menu key was disabled in the new AppCompat. I actually found a way to fix it and I guess I will share it with you.
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
public void onCreate(Bundle icicle){
...
setContentView(R.layout.main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar.isOverflowMenuShowing()) {
toolbar.dismissPopupMenus();
} else {
toolbar.showOverflowMenu();
}
return true;
}
return super.onKeyUp(keyCode, event);
}