SherlockActionBar: How to adjust CustomView against actionBar - android

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);

Related

Android, Centering image in actionbar

I have created an actionbar with below code :
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar_about, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
I just have a simple image in the actionbar which I want to be vertically centered. here's my layout code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="40dp"
android:src="#drawable/myImg"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="5dp"/>
</LinearLayout>
There are multiple questions on this topic on StackOverflow and I tried almost all of them and none of them worked for me. The result is just my image on the left side of the actionbar.
Thanks
Could you use toolbar instead. A toolbar is more flexible and has more functions.

Actionbar title font not changing in android

I want to change a custom font to my action bar title, have gone through couple of accepted answers,but no luck,I have tried as below,But my custom font is not applied to my actionbar title,Please help me to get rid of this,my code is as below:
java
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"neuropolx.ttf");
SpannableString s = new SpannableString(
" KOLAY RANDEVU");
s.setSpan(new CustomTypefaceSpan("", font), 0, 4,
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
getActivity().getActionBar().setTitle(s);
You can solve this problem by using this code
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
Changing Actionbar title font is not completely supported, but you can use a custom view for your action bar. Use custom view and disable native title. It will display between icon and action items. You can inherit all activities from a single activity, which has this code in onCreate:
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.customTitleView, null);
//you can customise anything about the text here.
//Using a custom TextView with a custom font in layout xml so all you need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
and your layout xml (R.layout.customTitleView in the code above) looks like this:
<?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:background="#android:color/transparent" >
<com.your.package.CustomTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:maxLines="1"
android:ellipsize="end"
android:text="" />
</LinearLayout>
you can make your own custom action bar and then change the title font. and you can put your font file in assets folder and access from it. Try like this:
tf= Typeface.createFromAsset(getAssets(), "FontType.TTF");
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#9B66AB")));
LayoutInflater inflater = LayoutInflater.from(c);
#SuppressLint("InflateParams")
View v = inflater.inflate(R.layout.titleview_2, null);
TextView actionbar_title = (TextView)v.findViewById(R.id.actionbar_title);
actionbar_title.setTypeface(tf);
actionBar.setCustomView(v);
and it's xml file will be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/actionBar"
android:paddingTop="7dp"
android:orientation="horizontal"
android:background="#9B66AB">
<TextView
android:id="#+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:padding="8dp"
android:text="#string/appTitle"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_centerVertical="true" />
</RelativeLayout>
Hope it helps...
use
getActivity().getActionBar().setCustomView(myTextView);
in that view you can use TextView for which you can set Font of your choice
TextView myTextView = new TextView(context);
myTextView.setTypeface(font);

Applying custom layout to ActionBar in FragmentActivity

I'm trying to change the layout of the actionBar in my app. I managed to display the layout, but there's a problem - the layout doesn't stretch to entire width of the bar and I don't see the title and the icon. Here's how I see it right now:
This is the XML of my custom 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="vertical"
android:background="#drawable/app_bg"
android:layout_gravity="fill_horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
And this is how I tried to display it in my main FragmentActivity, along with the title:
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setSubtitle("welcome to my app");
actionBar.setTitle("My App");
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I want the layout to stretch to entire width of the bar. Also, I'd like to have the options button on the right of the menu later (to the left of the button).
What am I doing wrong and how do I fix it?

Android ABS center title with home button

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);

Title bar color change issues

I want to change the color of the title bar dynamically, ie: someone clicks a button, it changes the color. However, I can't seem to get it to fill the entire title bar. This occurs on both the emulator and a Nexus One. Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/title_bar" android:background="#color/title_bar_blue" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="#+id/title_left_text" android:layout_alignParentLeft="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="#+id/title_right_text" android:layout_alignParentRight="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
titleBar = (RelativeLayout) findViewById(R.id.title_bar);
titleBar.setBackgroundResource(R.color.title_bar_green);
Try this ((View) titleBar.getParent()).setBackgroundColor(R.color.title_bar_green);
It is not the best way to do the job. But if it works, then you will know that you need to set background color of R.id.title_bar parent.
try this
titleBar = (RelativeLayout) findViewById(R.id.title_bar);
final FrameLayout titleContainer = (FrameLayout)titleBar.getParent();
titleContainer.setPadding(0, 0, 0, 0)
titleBar.setBackgroundResource(R.color.title_bar_green);

Categories

Resources