Android: ActionBarSherlock Title Ellipsize - android

Is there a way to prevent the title of the ActionBar (sherlock) from getting cropped (like ThisIsMyTi...)
I'd like to always see the title in full length, so the title should always have the highest priority except the overflow menu button on devices without a hardware menu button.
Would be nice if there was a solution. Setting the ellipsize of the title to null doesn't work, it just gets cropped without the ...

Add on the top:
private TextView titleTV;
In your OnCreate:
ActionBar Bar=getSupportActionBar();
Bar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.bar, null);
titleTV = (TextView) customView.findViewById(R.id.title);
titleTV.setSelected(true);
Bar.setCustomView(customView);
Bar.setDisplayShowCustomEnabled(true);
And in bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="19dp"
android:singleLine="true"
android:duplicateParentState="true"
android:fadingEdge="horizontal"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="">
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
</TextView>
</RelativeLayout>
You can set title with
titleTV.setText(t);

Related

How to make search widget larger in actionbar?

XML code here
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="#+id/find_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:contentDescription="#string/find_text"
android:background="#drawable/url_bar_entry"
android:singleLine="true"
android:textColor="#000000"
android:textCursorDrawable="#null"
android:inputType="text"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:textColorHighlight="#color/url_bar_text_highlight"
android:imeOptions="actionSearch"
android:selectAllOnFocus="true"
android:gravity="center_vertical|left"/>
<ImageButton android:id="#+id/find_prev"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_prev"
android:src="#drawable/find_prev"/>
<ImageButton android:id="#+id/find_next"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_next"
android:src="#drawable/find_next"/>
<ImageButton android:id="#+id/find_close"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_close"
android:src="#drawable/find_close"/>
</RelativeLayout>
I am inflating this menu by using this:
LayoutInflater inflater = LayoutInflater.from(mContext);
View content = inflater.inflate(R.layout.find_in_page_content, null);
EditText tv = (EditText) content.findViewById(R.id.find_text);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
//CODE TO ADD TO only EditText view to menu object I have
The outcome of this is seen here: http://imgur.com/sQZ7Lbh
My question is:, how can I make the url bar take up as much space as availble in the actiobar rather than be restricted to a tiny square. I also want the cursor to show up when pressed but it doesn't. However, I think that this is fixed when I make the url bar longer.
Set this gravity attribute in XML in your RelativeLayout
android:layout_gravity="fill_horizontal"
Taken from https://stackoverflow.com/a/12884297/2158970

Actionbar custom style with title highlight on clicked

I have a quick question regarding custom style navigation in the action bar.
I have used this code to create a custom title layout:
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
View customTitle = getLayoutInflater().inflate(R.layout.custom_title, null);
ab.setCustomView(customTitle);
which looks like this:
But when I click the navigate up button, only the icon and the indicator highlight... like this:
I'm looking for a way to get the whole title clickable, but with the custom layout. Is this possible?
For reference here is my actionbar layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ferries"
android:textSize="20sp"
android:textColor="#FFF"
android:id="#+id/actionbar_title_depart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:textSize="10sp"
android:textColor="#FFF"
android:id="#+id/actionbar_title_arrive" />
</LinearLayout>
Thanks for taking a look, and I look forward to hear some suggestions!

Sherlock actionBar.setIcon(android.R.color.transparent) leaves gap to left

I have tried searching on Google and stackoverflow but I didn't find the solution.
Problem:
Code:
actionBar = activity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
//actionBar.setDisplayUseLogoEnabled(false);
//actionBar.setDisplayHomeAsUpEnabled(false);
//actionBar.setDisplayShowHomeEnabled(false);
actionBar.setIcon(android.R.color.transparent);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LayoutInflater inflator = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout v = (LinearLayout) inflator.inflate(R.layout.header, null);
actionBar.setCustomView(v, lp);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/topheaderbackground" >
<TextView
android:id="#+id/topcaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
What I have tried:
actionBar.setDisplayShowHomeEnabled(false);
But result is:
Please Help!!!
[Edit]:
I am using Theme.Sherlock.
[Result of Doctoror Drive's suggestion]:
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.topheaderbackground));
new header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/topcaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
As you can see text is not in center, any suggestion???
It's not the icon, it's a CustomView that's leaving the gap.
You should change the color of ActionBar in styles.
http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles
And then add custom view with no background, just a TextView in center.
EDIT:
After you've set it to center, the space for your text is space of actionbar content minus the size of icon to the left (which is transparent). Try setting ActionBar display options.
remove
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
and add instead
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Tell me if you still have the issue, then I will tell plan B.
EDIT, plan B:
Alright, there is a problem with flipping when above is set so try adding padding to root of your TextView. Set ActionBar mode:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
ActionBar.DISPLAY_SHOW_HOME);
And in xml, I think the LinearLayout there is useless. Try setting plain and clean TextView
<TextView
android:id="#+id/topcaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="?android:attr/actionBarSize"
android:gravity="center"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
If that will not work, try
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="?android:attr/actionBarSize"
android:orientation="horizontal">
<TextView
android:id="#+id/topcaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>

Title not showing on the mainActivity

I have put up a spinner on Android Action Bar, i have placed the spinner in test.xml layout and i am calling it like this in the main activity in the OnCreate() method
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.activity_test);
setContentView(R.layout.activity_main);
actionBar.setTitle("Home");
But my title is not showing. i want to show HOME as title on that action bar what should i do?
My activity_test
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout>
I have solved the problem by adding a textView to my activity_test, as this whole activity is showing on the ActionBar so i have added a text view and problem is solved :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:drawSelectorOnTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/planets_spinner"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="Scene" />
</RelativeLayout>
When you set android:layout_alignParentRight="true" , RelativeLayout uses all the space including the title space and title is not displayed. You can check the behaviour by drawing a border around the RelativeLayout. But RelativeLayout doesnt consume more space if you dont use android:layout_alignParentRight="true". It doesnt consume more space if you use android:layout_alignParentLeft="true", too. It works weird for only android:layout_alignParentRight="true". In order to show a view at the right of the parent, the following worked for me:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profile_image"
android:layout_width="40dp"
android:src="#drawable/ic_launcher"
android:layout_height="40dp"
android:background="?android:selectableItemBackground"
android:padding="3dp"
android:scaleType="centerCrop" />
Do not use RelativeLayout. Just the view you need.
Then you can align right with the LayoutParams shown as following:
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayShowCustomEnabled(true);
ImageView imageProfile = (ImageView)actionBar.getCustomView();
int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
length,
length, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
imageProfile.setLayoutParams(layoutParams);
Now you should be able to view both ActionBar title and the ImageView.

Actionbar centered title with buttons

I would like to center the title in the action bar, while having action icons next to it(and ignoring them for the gravity setting).
I would like to have this:
Instead I have this:
Here is my code:
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="8dp"
android:src="#drawable/launcher_icon" />
<TextView
android:id="#+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
</RelativeLayout>
So my problem is, centering the title only works, if I have 0 actionbar items next to it, as soon as I add an item, the centering gets screwed up.
It would also be great, if I could use the up affordance button with it(which also screws up the centering now).
Any help would be appreciated.
I don't wan to implement my own actionbar item/button logic.
You can try this in the TextView:
<TextView
android:id="#+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
Remove the launcher icon from your RelativeLayout.
Change the width of the RelativeLayout to wrap_content
Remove:
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Add:
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
actionBar.setCustomView(yourInflatedCustomView, lp);
Have you tried to implement to the text this code?
android:layout_centerHorizontal="true"

Categories

Resources