2

I want to show a text in the right top corner of the actionbar. i have added the textview to the action bar using the following layout.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="#00000000" android:layout_gravity="right" android:gravity="right" android:layout_height="fill_parent"> <TextView android:id="@+id/balance_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:textAllCaps="true" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" android:layout_gravity="center_vertical|right" android:gravity="right" android:text="test balance" android:textStyle="bold" /> 

and in the activity as

 LayoutInflater inflator = LayoutInflater.from(this); View v = inflator.inflate(R.layout.actionbar_layout, null); ((LinearLayout) v).setGravity(Gravity.RIGHT); actionBar.setCustomView(v); 

It works perfectly. But it adds text in the center of the action bar. I want the text to be placed in right. I tried with gravity as well. But nothing works.

Any suggestions.

2
  • set mActionbar.setDisplayHomeAsUpEnabled(false); and mActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); for your Actionbar. Commented Mar 20, 2015 at 7:14
  • If the textView is only component you need in actionBar then why don't you create a LinearLayout with only textView at put it at the top? Commented Mar 20, 2015 at 7:22

3 Answers 3

5

First step, you must create a 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="50dp" android:background="@drawable/black_pattern" > <TextView android:id="@+id/title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textAllCaps="true" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" android:textStyle="bold" /> <ImageView android:id="@+id/imageView1" android:layout_width="35dp" android:layout_height="35dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:src="@drawable/ic_launcher" /> <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="8dp" android:background="@null" android:src="@android:drawable/ic_menu_rotate" /> 

In onCreate:

 ActionBar mActionBar = getActionBar(); mActionBar.setDisplayShowHomeEnabled(false); mActionBar.setDisplayShowTitleEnabled(false); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); mTitleTextView.setText("My Own Title"); ImageButton imageButton = (ImageButton) mCustomView .findViewById(R.id.imageButton); imageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getApplicationContext(), "Refresh Clicked!", Toast.LENGTH_LONG).show(); } }); mActionBar.setCustomView(mCustomView); mActionBar.setDisplayShowCustomEnabled(true); 
Sign up to request clarification or add additional context in comments.

2 Comments

In that blog, they designed for total action bar. i just want to add textview in the right side. I dont to want to put again logo and title in the XML.
When you put textView in ActionBar you must add everything because textView will never be in center.
0

Replace your LinearLayout by a RelativeLayout and set property android:layout_alignParentRight to true in your TextView.

That should do it works.

1 Comment

I tried that too. But when using relative layout, actionbar title got hidden.
-3

the following codes worked for me! Hope it will work for u too!! In your Edit text property add this line

"android:layout_gravity="center"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.