0

Absolute Android development beginner here, so please bear with me. I am trying to achieve 2 things with my title in the ActionBar:

  1. Center align the title text
  2. Use a custom font for the title text

I'm trying to follow this answer, so please look at that answer first.

Contents of MainActivity.java

    // ATTEMPT TO STYLE THE ACTIONBAR
    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayShowTitleEnabled(false);

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

    //if you need to customize anything else about the text, do it here.
    //I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
    ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());

    //assign the view to the actionbar
    this.getActionBar().setCustomView(v);

I created a new XML file: layout/titleview.xml to correspond to R.layout.titleview above. It contains:

<com.muppethead.app.name.CustomTextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:textSize="20dp"
    android:maxLines="1"
    android:ellipsize="end"
    android:text="MY APP TITLE" />

This gives the error: The following classes could not be found: com.muppethead.app.name.CustomTextView

Question

Where am I going wrong? In addition to fixing the above error, where do I reference my font, which is located in assets/fonts/font.otf? And what about centering the text?

I gotta say, shame on Google for not making this possible from the XML. It removes the possibility for new developers to create anything attractive.

Thanks in advance for your help.

Community
  • 1
  • 1
izolate
  • 1,590
  • 4
  • 22
  • 35
  • Did you create the class `com.muppethead.app.name.CustomTextView`? – Cat Nov 07 '12 at 01:00
  • No I haven't. One of the options to resolve this error is to create the class. I'm not really sure what goes in the class and what attributes I need to set TBH. – izolate Nov 07 '12 at 01:01

1 Answers1

1

What I would honestly do is forget about the custom TextView, and just use the default one in the XML. You're using it just this once, and only for the custom font.

You can do something like this instead:

TextView tv = (TextView) v.findViewById(R.id.title);
Typeface tf = Typeface.createFromFile(getAssets(), "fonts/font.otf");
tv.setTypeface(tf);
tv.setText(this.getTitle());
Cat
  • 66,919
  • 24
  • 133
  • 141
  • Ok, forgive me for having to dumb this down. Could you outline what I need to do to get from where I am to where you're suggesting? I get that I should delete the code in my `MainActivity.java` and use yours. Also, I should delete `titleview.xml` entirely. But how does `R.id.title` (from your code) correspond to the ActionBar title? And can I center align the text using your method? – izolate Nov 07 '12 at 01:11
  • 1
    No no no, keep `titleview.xml`. Replace `com.muppethead.app.name.CustomTextView` with `TextView`. And the only thing to replace in your existing file is the `setText` line (replace that line with the code I provided). – Cat Nov 07 '12 at 01:13
  • Genius. That worked. The only thing I had to change is `createFromFile` to `createFromAsset` - The only reason I knew about this is because I had tried somebody else's code beforehand. Thank you for your help dude! You're excellent at this. Got any idea how to center the text too? – izolate Nov 07 '12 at 01:21
  • Should be able to set `android:gravity="center"` in the XML. I'll add that to my post if it works for you. – Cat Nov 07 '12 at 01:22
  • No, unfortunately that didn't work. :( Still, you deserve the answer though. – izolate Nov 07 '12 at 01:24
  • Hmm... you may have to wrap it in a `LinearLayout` (with `match_parent` width), then the center gravity should take effect. – Cat Nov 07 '12 at 01:48
  • Alright, I've learned a lot in this last 2 days of AndroidDev. I needed to set the title's TextView to `layout_width="fill_parent"` in order for the center gravity to work correctly. Thanks Eric! – izolate Nov 08 '12 at 21:52