0

I'm trying to customize the title bar in my android application. In order to do that, I used accepted answer of this question.

Here is the code I used.

Here is my titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView android:id="@+id/ImageView01"
        android:layout_width="57dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_prof"/>

    <TextView

        android:id="@+id/myTitle"
        android:text="This is my new title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#FFA500"
        />
</LinearLayout>

Here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

Here is my TitleBar.java

package com.myayubo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

public class TitleBar extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
        }

        final TextView myTitleText = (TextView) findViewById(R.id.myTitle);

        if (myTitleText != null) {
            myTitleText.setText("NEW TITLE");
            // user can also set color using "Color" and then
            // "Color value constant"
             myTitleText.setBackgroundColor(Color.GREEN);
        }
    }
}

Here is my strings.xml

 <string name="hello_world">Hello world!</string>
    <string name="app_name">My Ayubo</string>
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>

But, I cannot customize my title bar using following code. I mean, code is running without any errors. But, it is not doing what I expected. What I need is to add an image and a title in to my header. How am I suppose to do that?

Community
  • 1
  • 1
Manuli
  • 1,203
  • 4
  • 20
  • 43
  • 1
    ActionBar is no more needed as per Google. Check this link https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar/ – Mohd Asif Ahmed Feb 16 '16 at 04:42
  • 1
    Here are some tips, Use AppCompatActivity. Use the theme with NoActionBar. And use Toolbar in your layout.xml – capt.swag Feb 16 '16 at 04:43
  • minimum SDK version of my app is 15. So, in that case, I can't use Toolbar instead of this, ryt? – Manuli Feb 16 '16 at 04:53

1 Answers1

0

figured it out in my own.

First added following items in my menu.xml

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="1"
        android:showAsAction="never" />

    <item
        android:id="@+id/action_example"
        android:title="Logout"
        android:orderInCategory="2"
        android:showAsAction="withText|ifRoom" />

    <item
        android:id="@+id/profPicture"
        android:title=""
        android:orderInCategory="3"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_prof"
        android:onClick="userProfile"
        />

    <item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>

    <item
        android:id="@+id/addLoc"
        android:title=""
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_location"
        android:onClick="gotoLocation"/>

    <item
        android:id="@+id/namePerson"
        android:title="Nathalia Smith"
        android:orderInCategory="4"
        app:showAsAction="ifRoom"
        android:onClick="userProfile"/>
</menu>

Then added following methods to java file to implement onlick events of images and buttons.

  public void doThis(MenuItem item){
       // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), DealsList.class);
        finish();
        startActivity(intent);
    }

    public void gotoLocation(MenuItem item){
        // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), AddLocation.class);
        finish();
        startActivity(intent);
    }

    public void userProfile(MenuItem item){
        // Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), UserProfile.class);
        finish();
        startActivity(intent);
    }

Thanks for everyone who answered. :)

Manuli
  • 1,203
  • 4
  • 20
  • 43