ActionBarSherlock setCustomView not working in Android 3.2
P

6

14

I recently upgraded my application to use ActionBarSherlock 4.1. Since the upgrade users running the app on Honeycomb experiance a force close due to a null pointer exception when setting a custom view on the actionbar.

I add a custom view containing two spinners to the actionbar this works on Android 4.0 & 4.1 but is not working on 3.2.

            bar.setCustomView(R.layout.custom_action_bar);// spinners
            actionBarViewHolder= new CustomActionBarViewHolder();
            actionBarViewHolder.categorySpinner = (Spinner) findViewById(R.id.actionbar_catergory);
            actionBarViewHolder.sortBySpinner = (Spinner) findViewById(R.id.actionbar_sortby);

On Android 3.2 the spinners can not be found yet on 4.0 and 4.1 they views are found and anything runs smoothly.

I have not tried the application on a 3.0 emulator but I image the problem persists.

Any ideas what could be the problem?

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Category:" />

    <Spinner android:id="@+id/actionbar_catergory"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_catergory"
        android:background="@drawable/spinner_background" />
</LinearLayout>


<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Sort By:" />

    <Spinner android:id="@+id/actionbar_sortby"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_sortby" android:background="@drawable/spinner_background" />

</LinearLayout>

Piano answered 24/7, 2012 at 22:24 Comment(0)
S
7

Try this, its working fine on all devices or you can check the demo code here

getSupportActionBar().setCustomView(R.layout.actionbar_top); // load your layout
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Images

enter image description here

Sowers answered 15/6, 2013 at 9:24 Comment(1)
there is nothing in the link nowPhytography
E
0

Try this:

final View view = inflater.inflate(R.layout.custom_action_bar, null);
bar.setCustomView(view);// spinners
actionBarViewHolder= new CustomActionBarViewHolder();
actionBarViewHolder.categorySpinner = (Spinner) view.findViewById(R.id.actionbar_catergory);
actionBarViewHolder.sortBySpinner = (Spinner) view.findViewById(R.id.actionbar_sortby);
Eta answered 30/7, 2012 at 9:48 Comment(3)
Sadly. This did not work. The spinners are still null on Honeycomb when using the inflater.Piano
The view was created and exists so Spinners aren't children of this view. Potential reasons: 1. wrong ids, 2. spinners aren't children of the view 3. findViewById invoked on wrong viewEta
custom_action_bar contains two children linear layouts which contain the spinners. I have added the xml layout.Piano
H
0

Here is code how I create custom action bar with actiobBarSherlock

 private void createCustomActionBar() {

    List<SiteLink> links = new ArrayList<SiteLink>();
    links.add(...)  
    LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.external_link, links);

    View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
    IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
    spinner.setAdapter(linkAdapter);


    ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
    getSupportActionBar().setDisplayShowCustomEnabled(true);

}

Adapter

private static class LinksAdapter extends ArrayAdapter<SiteLink> {

    private List<SiteLink> strings;
    private Context context;

    private LinksAdapter(Context context, int textViewResourceId, List<SiteLink> objects) {
        super(context, textViewResourceId, objects);
        this.strings = objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (strings == null) return 0;
        return strings.size();
    }

    @Override
    public SiteLink getItem(int position) {
        return super.getItem(position);
    }


    // return views of drop down items
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        final SiteLink siteLink = strings.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // at 0 position show only icon

        TextView site = (TextView) inflater.inflate(R.layout.external_link, null);
        site.setText(siteLink.getName());

        site.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
                context.startActivity(i);
            }
        });
        return site;


    }


    // return header view of drop down
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.icon, null);
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
               android:gravity="right"
        >


    <com.actionbarsherlock.internal.widget.IcsSpinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingRight="20dp"
        android:layout_gravity="center"
            />

     <ImageView  android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:src="@drawable/ic_navigation_refresh"
                 android:paddingRight="20dp"
                 android:paddingLeft="10dp"
                 android:layout_gravity="center"
                 android:background="@drawable/action_buttons_background"
                 android:id="@+id/refresh"/>


    <ImageView  android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_action_settings"
                android:paddingRight="20dp"
                android:background="@drawable/action_buttons_background"
                android:layout_gravity="center"
                android:id="@+id/settings"/>

</LinearLayout>
Halpern answered 3/8, 2012 at 9:4 Comment(3)
No joy using View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);Piano
you can use non static LayoutInflater initialization. for example LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflanter.inflate...Halpern
That also doesn't work. It seems that Honeycomb can not find the layout to inflate?Piano
C
0

Have you tried to put your two children LinearLayout's inside a main LinearLayout?. Also try to not repeat the id from the children LinearLayout's, because you're making your id's system cry. But I'm almost sure your problem is that you have more than one main Layouts in the XML file.

Collaboration answered 6/5, 2013 at 21:5 Comment(0)
S
0

Use below code for setCustomView using actionbarsherlib. It is worked in android 3.2 version also.

    getSupportActionBar().setDisplayShowCustomEnabled(true);
    View view = getLayoutInflater().inflate(R.layout.custom_view, null);
    Button mybutton = (Button)view.findViewById(R.id.button1);            
    mybutton.setOnClickListener(new OnClickListener()
    {
            @Override
            public void onClick(View v)
            {
            /** Your click actions here. */
            }
    });
    getSupportActionBar().setCustomView(view);
Stress answered 22/5, 2013 at 12:43 Comment(0)
E
0

This is my code.here I added Sherlock bar library to my project.then, I used here for set my Title on header(Top). like this.. just try this like.. Set target SDK to Android 3.2(Version 14) or above..

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.header_sherlock_xmllayout);
    header_tvleft = (TextView) findViewById(R.id.header_tvleft);
    header_tvleft.setText("Back");

try this method....

Erie answered 10/7, 2013 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.