1

I am trying to get an ImageSlider activity using PageAdapter with following features:-

  1. Translucent Background.
  2. Translucent Custom Action Bar, with TextView and ImageButtons.

Problems I am facing:-

  1. To make the activity translucent, I added android:theme="@android:style/Theme.Translucent to the <activity> of Manifest and android:background="#20000000" in XML layout file. But after doing this getActionBar() in my onCreate() method is returning null. If I remove android:theme="@android:style/Theme.Translucent then the activity is no longer translucent but getActionBar() is now working.

I am using getActionBar() to inflate a custom translucent Action bar, so as of now either I can get translucent activity or a custom action bar, not both of them at once.

  1. TextView in my Custom Action Bar is for showing the current image no out of total no of images, like 1 out of 5 and slide to right to get 2 out of 5, and so on.

But sometimes it shows correctly, sometimes just random numbers.

My code:-

TextView header;
int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Getting Action Bar
    ActionBar mActionBar= getActionBar();

    LayoutInflater minflate =(LayoutInflater) mActionBar.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
     mview = minflate.inflate(R.layout.albumactionbar,null);

    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE);
    mActionBar.setCustomView(mview, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

    header =(TextView)mview.findViewById(R.id.action_text);

    setContentView(R.layout.albummainview);
    }

albumactionbar.xml:-

<?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="wrap_content"
    android:background="@drawable/custom_ab"
    android:orientation="horizontal"
     >
    <ImageButton 
        android:id="@+id/album_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_back"
        android:layout_alignParentLeft="true"
        android:background="@color/Translucent"
        android:layout_gravity="center"
        />
    <TextView 
        android:id="@+id/action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:text="Gallery Count"
        android:textSize="18sp"
        android:layout_centerInParent="true"/>
    <ImageButton 
        android:id="@+id/album_after"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_after"
        android:layout_alignParentRight="true"
        android:background="@color/Translucent"
        android:layout_gravity="center"
        />


</RelativeLayout>

custom_ab.xml:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >



    <gradient
        android:startColor="#FF000000"
        android:endColor="#00000000" 
        android:angle="270"
        />

</shape>

Now inside MyAdpater, I am changing the header TextView value of the custom Action bar:-

public class AlbumAdapter extends PagerAdapter

    {

        Context context;

        public AlbumAdapter(Context context){
            this.context = context;

        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();

            View viewItem = inflater.inflate(R.layout.albumitemview, container, false);

            //header is the custom action bar's text view.
            header.setText((position+1)+" out of "+count);

            .....

}
}

So please guide me in solving both the problems, and regarding problem 2, is it the right place to change the textView's value?

Mohit
  • 1,045
  • 4
  • 18
  • 45

1 Answers1

1

Well for the first problem you can change the color of your actionbar in the styles.xml by a <color name="Transparent_Black">#50000000</color> for example, then add getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); in you activities onCreate(). I used this and it works fine. When you do this you can add your header by getActionBar().setTitle("") instead if header.

For your second problem you just need to add a ViewPager.OnPageChangeListener to your ViewPager which has a method onPageSelected(position), there you can update your header.

So if both problems are solved you can add getActionBar().setTitle((position+1)+" out of "+count) to your onPageSelected(position) method.

M090009
  • 1,129
  • 11
  • 20
  • I want a gradient change from opaque black to transparent, as you go up to down in custom action bar background as I am doing in 'custom_ab.xml', setting the color in 'style.xml' will fill the whole action bar with a single solid color, wouldn't it? – Mohit Dec 14 '14 at 09:45
  • @Mohit Ah ok, well you can add your **custom_ab.xml** in the **style.xml** as `@drawable/custom_ab` – M090009 Dec 14 '14 at 09:52
  • Well I am already using it in `albumactionbar.xml` RelativeLayout's background. – Mohit Dec 14 '14 at 09:55
  • ok then if you want to use your custom actionBar then your first problem is solved, so just implement the **PagerListener** and set your **ViewPager** to it then you're set – M090009 Dec 14 '14 at 10:01
  • Well you see that's the problem. I can get the custom action bar but then the activity won't be translucent. And I can make the activity translucent but then I am getting null for `getActionBar()`. – Mohit Dec 14 '14 at 10:05
  • Wait maybe i cant understand you point what do you mean by as you go up to down in custom action bar background?? – M090009 Dec 14 '14 at 10:10
  • Well, just an action bar whose background is not a solid color, but its solid black at top and transparent on bottom. And the change is gradient as I try to apply using `custom_ab.xml`. It has nothing to do with the layout or others views in activity from scrolling or anything. Its just the background of the action bar. Hope I was able to make you understand :) – Mohit Dec 14 '14 at 10:16
  • I can more simply ask, why when using the activity theme as Theme.Translucent, I am getting the null return value for getActionBar(). Because if it is fixed, then the 1st problem will go away. – Mohit Dec 14 '14 at 10:18
  • Ok i got you loud and clear, unfortunately from what i understand is that the translucent theme comes without an actionbar just the title, soo you need to do the effect yourself. I would like to thank you because i was searching for something like this – M090009 Dec 14 '14 at 10:56
  • The first problem is still the issue, but I used your code and my 2nd problem is fixed, so thanks for that. – Mohit Dec 14 '14 at 11:01
  • I found something for the first problem you can do `requestWindowFeature(Window.FEATURE_ACTION_BAR);` to get the actionbar as per this [link](http://stackoverflow.com/questions/22037822/android-activity-with-transparent-background-and-action-bar) – M090009 Dec 14 '14 at 11:04
  • Finally. Both of the problems fixed. Thanks a lot! – Mohit Dec 14 '14 at 11:20