ActionBar Default Logo Has Wide Space - android

I'm working with actionBar in android and the default logo is not showing. So I have added this code to show the Logo in my onCreate method in MainActivity.
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
Why is the logo has a wide space causing the title to not be seen?
Please help. Thanks.

This is very simple to accomplish
If you want to change it in code call
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
In Manifest XML:
<activity android:name=".MyActivity"
android:icon="#drawable/my_icon"
android:label="My new title" />
To enable the back button in your app use
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

Related

how to set title in toolbar?

Seems pretty simple question, but could't find any answer.
How to change the toolbar title.
The toolbar is created by AppCompat Activity,not a custom created one.
It has the app name as default.I would like to change its Name corresponding to the action the activity performs.
Note : I can change custom toolbar title using the below code,so please i am not looking for that.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("My Title");
You can set default Toolbar title as following:
YourActivity.this.setTitle("Your Title");
For fragment:
getActivity().setTitle("Your Title");
getSupportActionBar().setTitle("Title")
Set ActionBar title based on your need
ActionBar ab = getActionBar();
ab.setTitle("My Title");

SetHomeButtonEnabled not working but setDisplayHomeAsUpEnabled is working

I want to add the application icon in the actionbar for all the activities in my application and on the icon click , I would like to navigate to the home page of my application.
I tried with the following code in onCreate
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
Now the application icon is coming in the actionbar , but on clicking it, onOptionsItemSelected is not getting called. But if use actionBar.setDisplayHomeAsUpEnabled(true) instead of actionBar.setHomeButtonEnabled(true) , onOptionsItemSelected is getting called with item.getItemId() . Below is the code snippet
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
The documentation says using both setDisplayHomeAsUpEnabled and setHomeButtonEnabled , onOptionsItemSelected will be called and the only difference is the up arrow. I dont need the up arrow in the actionbar, I only require the application icon. How can that be done?
My minSdkVersion 14 and targetSdkVersion 21.
From http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(int)
You can use:
actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
actionBar.setDisplayShowHomeAsUpEnabled(true);
and this should replace the back arrow with your icon
you can use this:
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
finish();
}
});
You can use :
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
fist code shows left arrow on toolbar and second code lets you to put your icon

getActionBar returns null - how to have NoTitleBar

So I have this under my Activity's onCreateView()
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
And my manifest looks like this
<activity
android:name="com.example.nfcproducttracing.ProductTracer"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light"
android:launchMode="singleTask">
What I have achieved is have my application with Tabs, but no TitleBar. However, during application launch I can briefly see the TitleBar before it disappears, and I do not want that.
What am I supposed to do? When I set my activity theme to anything related to NoTitle, getActionBar returns null and the app crashes.
If I understand correctly your questions, you can use the requestWindowFeature(Window.FEATURE_NO_TITLE) from your activity.
Flag for the "no title" feature, turning off the title at the top of the screen.
Not really sure but have you tried this:
getActionBar().setTitle("");
It will set a blank title for your ActionBar.
You can try using getSupportActionBar() instead.
this is from android.support.v4.app.actionbar, not from android.app.actionbar!

android - ActionBar custom View without icon

In my application a have an ActionBar with Tabs as navigation mode. I also use a custom View which adds another "line" to the ActionBar. Setup looks like:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.ab);
Typeface font = Typeface.createFromAsset(getAssets(), "cs_regular.ttf");
TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.tvActionTitle);
title.setTypeface(font);
The problem: I do not want an icon to be shown, but when I use actionBar.setDisplayShowHomeEnabled(false); my custom layout gets placed BELOW the Tabs and I need it to be above the Tabs in the ActionBar. Setting it to true places the layout as I need it, but shows the unnecessary icon.. any suggestions?
I assume by icon you mean the app icon. You can essentially make it invisible using styles. Add this to the style your Activity is using:
<item name="android:icon">#android:color/transparent</item>

Removing left arrow from the actionbar in android?

I want to remove the left arrow from the action bar and only icon and title needed.
My code:
getSupportActionBar().setIcon(R.drawable.logo);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.dashboardtitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
But this doesn't remove the left arrow . Any help!!
According to specification android specification:
To enable the icon for up navigation (which displays the "up" indicator next to the icon), call setDisplayHomeAsUpEnabled(true) on your ActionBar:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
So you should set actionBar.setDisplayHomeAsUpEnabled(false);
UPDATE:
I test next code with ActionBar sherlock and it's work. There is no back arrow:
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.about_event_location);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
You need to add this to your actionBar theme. You can replace arrow image with your own image which can be transparent
<item name="android:homeAsUpIndicator">#drawable/action_arrow</item>
getActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);
Where action_arrow is a 25x10px transparent png.
Same code for sherlock would be:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);
#vivek: works great. I do not recommend to use #android:color/transparent as Tim Cooper said, because the Title is aligned to the left. So a empty image is better
In AndroidManifest.xml
remove the below code, which will remove back button from your ActionBar
android:parentActivityName=".Main.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main.MainActivity" />
Just keep it like below:
<activity
android:name=".YourActivity"
android:label="#string/your_activity_label">
</activity>

Categories

Resources