0

Sorry if you seen this question asked severally but I seem to be stuck on something very basic with regards to the action bar.

Essentially I would like to place text on the far right/end of the action bar based on the engaged fragment. I'm aware of how menu buttons are placed there.

Having read up further on the action bar I'm aware of how menu options can be placed on the far right of the action bar but I can't seem to grasp how to place text on the far right/end of the action bar based on the engaged fragment.

At the moment below is how I've gone about customizing my action bar:

In my MainActivtiy.java I've got a custom layout that contains a textview which I set by calling a method contained in my MainActivtiy.java from the currently engaged fragment:

LayoutInflater inflator = (LayoutInflater) this
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mCustomActionView = inflator.inflate(R.layout.actionbar, null);        

tvActionBarTitle = (TextView) mCustomActionView.findViewById(R.id.title);
tvActionBarTitle.setTypeface(officialBoldFont);
tvActionBarTitle.setTextColor(Color.BLACK);

getActionBar().setCustomView(mCustomActionView);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
getActionBar().setBackgroundDrawable(new ColorDrawable(0xffFFCC00));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(false);

Below is the above mentioned method in my MainActivtiy.java which I call from the currently engaged fragment:

//
public void setActionBarTitle(String title) {
    tvActionBarTitle.setText(title);
}

Below is how I call the above method from the currently engaged fragment:

//
((MainActivityNormal800by480) getActivity()).setActionBarTitle("LOG IN");

Below is the actionbar 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:background="@android:color/transparent" >

    <TextView
         android:id="@+id/title"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerVertical="true"
         android:textSize="25dp"
         android:maxLines="1"
         android:ellipsize="end"/>

</RelativeLayout>

Below is a screenshot and I have circled where I desire to place text using the oval shape:

enter image description here

Any ideas how I could place text on the far right/end of the action bar based on the engaged fragment.

TokTok123
  • 753
  • 3
  • 11
  • 27

1 Answers1

0

Try 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="fill_parent"
    android:orientation="horizontal"

     >

     <LinearLayout 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center|start"
        android:id="@+id/confirm_cabs_back"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/b"
            />

        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
             android:id="@+id/city_spinner_layout"
             android:layout_gravity="center"
            >

        <TextView 
            android:id="@+id/action_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/confirm_cab"
            android:textStyle="bold"
            android:textColor="@color/taxi_blue"
            android:layout_gravity="center"
            />

        <!-- <ImageView
            android:layout_gravity="center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/drop_down" /> -->
        </LinearLayout>

    </LinearLayout>

     <!-- <LinearLayout 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="end|center"
    android:gravity="end"
    android:id="@+id/fare_ll"
         > -->

    <LinearLayout
        android:layout_width="2dp"
        android:layout_height="fill_parent"
        android:background="@color/grey" >
    </LinearLayout> 
     <!-- android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp" -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I want to put custom view"
        android:id="@+id/fare_ll"
        android:layout_gravity="center|end"

        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:textColor="@color/black"
        />
   <!--  </LinearLayout> -->

</LinearLayout>

Please check out my solution over here .. Android Action Bar: Can I replace a custom Title in appcompat v7

This is complete way of doing this ..

Hope it helps :)

Community
  • 1
  • 1
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
  • Do you mean in place of my XML as shown in my edited post above I could use your suggestion of nested linear layouts which contain views to enable the appearance of text on the far right? – TokTok123 Jul 30 '14 at 11:36
  • Yes ofcourse .. Just remove unwanted layout. I have put a textview with Text :-- "I want to put custom view" for your purpose . As you are using custom view for Android ActionBar .. you can achieve your requirement like this .. Just set layout like I mentioned in your actionBar custom view and get id of that textView if you need to keep it dynamic .. – AndroidHacker Jul 30 '14 at 12:09