I managed to center the title of the ABS using a custom view but the home button is gone. How can this be solved? Is the only way to do it to put this home button in the custom view? (To be clear: the home button should be on the left not in the center)
Custom view:
<?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="match_parent"
android:gravity="center"
android:background="#android:color/transparent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</LinearLayout>
Activity code:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.widget_actionbar_title);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View customView = LayoutInflater.from(this).inflate(
R.layout.app_icon, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
getSupportActionBar().setCustomView(customView, params);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.back_button));
Add this:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
solution is very simple)
Change in your Layout
android:layout_width="match_parent"
on this:
android:layout_width="wrap_content"
it will help because your initial layout takes all visible place and there is no place for "home"
I had to add this code to make it work:
getSupportActionBar().setDisplayShowHomeEnabled(true);
Related
I want to insert a centered icon in Actionbar, but i have no idea what code that i need. anyone can help me?
This is my code :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
getSupportActionBar().setTitle("");
Thank you
step 1: create a custom layout in R.layout. directory, as you desire and then set the view
to action bar
step 2: Set the custom layout.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View customView = getLayoutInflater().inflate(R.layout.customActionBar, null);
actionBar.setCustomView(customView );
From your xml add ToolBar as below and set Icon to ImageView inside it.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"/>
</android.support.v7.widget.Toolbar>
Set toolbar as support action bar as you are doing.
Custom action bar does not fill parent.
I am using custom action bar for navigation drawer purpose.
But My action bar keep some space left at left side of action bar.
I used code like this.
// to inflate custom view
LayoutInflater inflator= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflator.inflate(R.layout.custom_action_top_bar, null);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getActionBar().setCustomView(v);
View v1 = getActionBar().getCustomView();
LayoutParams lp = (LayoutParams) v1.getLayoutParams();
lp.width = LayoutParams.FILL_PARENT;
v1.setLayoutParams(lp);
this is action_custom_action_top_bar file for reference
this xml file contains main linear layout with left and right imageviews for opening two sided drawers respectively. but addition space get occured
on left side of the action bar can any one help me out?
this is custom action bar i have used in that some background colour
i have given to the action bar and two image for each side for drawers
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/IV_leftIcon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#color/light_green"
android:src="#drawable/header_menu_btn" />
<TextView
android:id="#+id/actionText"
style="#style/action_bar_top_text_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".6"
android:background="#color/light_green"
android:drawableRight="#drawable/header_dropdown_arrow"
android:gravity="center"
android:text="Sample"
android:textColor="#color/white" />
<ImageView
android:id="#+id/IV_rightIcon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#color/light_green"
android:src="#drawable/plus_btn" />
</LinearLayout>
how make action bar fill width
UPDATED ANSWER
remove android:layout_gravity="center" in root linear to be like this , make height wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0" >
......
</LinearLayout>
Prefer use getSupportActionBar() as default activity extends ActionBarActivity
import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.widget.Toolbar;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
getSupportActionBar().setCustomView(v, layoutParams);
Toolbar parent = (Toolbar) v.getParent();
parent.setContentInsetsAbsolute(0, 0);
}
}
Summary
I understand why you have a problem , you using eclipse with old support library
that dosenot support ToolBar
use appcompat-v21 or later
my advice to you , use Android studio as they stop support eclipse
this code working 100% , prove of concept
Just add these 2 line code into your Toolbar xml code
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
It will solve your problem.
Here is my working code:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp">
<include layout="#layout/your_custom_toolbar_layout"/>
</android.support.v7.widget.Toolbar>
Question:
How can I have the title back?
Here's the screenshot (Missing title):
Actionbar setting:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("My Title");
actionBar.setIcon(drawable.dropdown_user);
View mLogoView = LayoutInflater.from(this).inflate(R.layout.mLogoView, null);
actionBar.setCustomView(mLogoView);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
mLogoView.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img_merchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/infoflex">
</ImageView>
</RelativeLayout >
The first thing which I can think a way to fix that is try to set
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" >
to your RelativeLayout. Because as default in Android every View is added from left to right (if you doesn't specify something else in your code).
And if that doesn't work, I would add android:layout_width="90dip" or some other value depending on the image and your tests. The second option is more like a workaround, but I think it will work on all devices.
Edit:
As I found there is a little problem using RelativeLayout and Custom Views in ActionBar, so the better option is to use LinearLayout and set LayoutParams via code. Change your xml to look like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asus" />
</LinearLayout >
and in your MainActivity.class use this :
import com.actionbarsherlock.app.ActionBar.LayoutParams;
....
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("OMG");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
I am creating a customized home icon. The codes for the customization is as follows,
View customView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
customView.findViewById(R.id.action_home).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
actionBar.setCustomView(customView, new ActionBar.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
actionBar.setDisplayShowCustomEnabled(true);
This is action_bar_display_options_custom.xml,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/action_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_home" />
<TextView
android:id="#+id/action_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
This's the screenshot,
This is home's icon (that space isn't a part of the image view),
So, how can I remove the space in front of the view?
I solved this issue by adding following lines to my code
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setCustomView(view); // set custom view here
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
Hope this helps you.
Remove the padding or margin that you have set in the parent layout, that will solve the problem. Please feel free to ask doubts, also do let me know if that solves your problem.
I am using the ActionBar.Tab, and they always appear at the left of screen. I want to put the activity icon to the left of tabs, but it always appear at the right of tabs. How can I move their position to where I want?
here is what works for me like a dream:
in the Activity I have this:
//hiding default app icon
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
my_action_bar.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/turquoise">
<ImageButton
android:id="#+id/btn_slide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#null"
android:scaleType="centerInside"
android:src="#drawable/btn_slide"
android:paddingRight="50dp"
android:onClick="toggleMenu"
android:paddingTop="4dp"/>
</RelativeLayout>
//use your custom xml view to show your icon at left
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
on your onCreate() method if you are on the activity
actionBar.addTab(actionBar.newTab()
.setIcon(<the icon you want>)
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));