0

I have been trying to make an ActionBar for a while now. At first I decided just to draw it as an image (2nd actionBar in the image below) but noticed that on some of the devices that Image wouldn`t take up the whole screen width. As for the original ActionBar I use this code in my main Activity onCreate method:

final ActionBar actionBar = getActionBar();
@SuppressWarnings("deprecation")
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.actionbar)); 
actionBar.setBackgroundDrawable(background);
//actionBar.setIcon(android.R.color.transparent);
actionBar.setDisplayShowTitleEnabled(false);

The first ActionBar has a light theme with the image being stretched. How can I make it to be transparent and is there a way to set ActionBar height so it wouldn`t look that stretched ? I also tried doing this with styles and had the same result.

EDIT I followed vinitius answer so I now have this: result the image is 482x104 and my phone is 480x800. This is the same problem that happened when using it as plain image. Image is in drawable-hdpi folder.

EDIT 2 by adding android:scaleType="fitXY" to the ImageView it will make the whole image stretched.

actionbar_layout.xml

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

<View
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#3383A8" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:src="@drawable/actionbar" />

</RelativeLayout>

EDIT 3 If I add the image as a RelativeLayout background it fill not fit in the whole width on the bigger devices. If I use it as an image on bigger width devices it will be stretched. Image

ActionBar

1 Answers1

0

You can design your own actionBar view, you may even inflate it from any xml, and then just:

    getActionBar().setCustomView(yourView);
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

To adjust the height of your bar, you could do it like this:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">

   <!-- the height you desire according to your dimen -->

    <item name="android:height">@dimen/action_bar_height</item>
</style>

Then, apply your theme. It works for me

vinitius
  • 3,212
  • 2
  • 17
  • 22
  • Is this possible when using min API level 11 ? I noticed that you need Atleast API 14 to remove the icon on the action bar, Same goes for the DarkActionBar theme. – Danielius Ašmontas Jan 21 '15 at 17:38
  • Yes, you need min API level 14 to use it this way – vinitius Jan 21 '15 at 17:49
  • Updated. The image isn`t taking up the whole screen width. – Danielius Ašmontas Jan 21 '15 at 18:22
  • To your imageView add scaleType:fitXY or use the image as the background of your root layout. Also, use `match_parent` instead of `fill_parent`, because it's deprecated. Remove `orientation` from your relative layout and why is there a ``? – vinitius Jan 21 '15 at 18:27