Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

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>

Upvotes: 2

Views: 156

Answers (2)

Murat
Murat

Reputation: 3294

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.

Upvotes: 0

Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

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>

Upvotes: 1

Related Questions