Custom ActionBar and RoboGuice injection

v4r

I have created a custom ActionBar for my project where I want to inject the view elements for my ActionBar using (InjectView). However, this does not work. On the other hand, findViewById works fine.

I think this problem comes from the fact that the ActionBar is created after the MainActivity has finished its layout. However, this doesn't make sense either since I can still use findViewbyId to get the views.

Here is the XML definition for the actionbar:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/activityTitleTxt"
            android:layout_alignParentTop="false"
            android:layout_gravity="center"
            android:textColor="@color/white" />

    </LinearLayout>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:showDividers="middle"
        android:id="@+id/filterRadioGroup"
        android:layout_toLeftOf="@+id/layoutRadioGroup">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All"
            android:id="@+id/selectAllBtn"
            android:background="@null"
            android:button="@null"
            android:textColor="@color/white"
            android:layout_marginRight="5dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@android:drawable/btn_star"
            android:id="@+id/selectFavBtn" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/layoutRadioGroup"
        android:layout_toLeftOf="@+id/shareSearchLayout"
        android:gravity="center">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_view_as_list"
            android:gravity="center"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tileViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_picture"
            android:gravity="center" />
    </RadioGroup>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="0dp"
        android:id="@+id/shareSearchLayout">

        <SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchView"
            android:layout_gravity="center" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/shareBtn"
            android:background="@android:color/transparent"
            android:src="@android:drawable/ic_menu_share"
            android:layout_gravity="center" />

    </LinearLayout>

</RelativeLayout>

The ActionBar View:

public class ActionBarView extends LinearLayout {

    private MainActivity activity;

    @InjectView(R.id.activityTitleTxt)
    private TextView activityTitleTxt;
    @InjectView(R.id.filterRadioGroup)
    private RadioGroup filterGroup;
    @InjectView(R.id.layoutRadioGroup)
    private RadioGroup layoutGroup;
    @InjectView(R.id.searchView)
    private SearchView searchView;
    @InjectView(R.id.shareBtn)
    private ImageButton shareBtn;

    public ActionBarView(MainActivity activity) {
        super(activity);

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.action_bar_view, this);

//        this.activityTitleTxt = (TextView) findViewById(R.id.activityTitleTxt);
////        this.selectAllTxt = (TextView) findViewById(R.id.selectAllTxt);
////        this.selectFavBtn = (ImageButton) findViewById(R.id.selectFavBtn);
//        this.filterGroup = (RadioGroup) findViewById(R.id.filterRadioGroup);
//        this.layoutGroup = (RadioGroup) findViewById(R.id.layoutRadioGroup);
//        this.searchView = (SearchView) findViewById(R.id.searchView);
//        this.shareBtn = (ImageButton) findViewById(R.id.shareBtn);

        this.activity = activity;
        setListeners();
    }

    private void setListeners() {
        this.filterGroup.setOnCheckedChangeListener(activity);
        this.layoutGroup.setOnCheckedChangeListener(activity);
    }

    public void setActivityTitle(String title){
        this.activityTitleTxt.setText(title);
    }
}

Here is how I attach the ActionBar in the main activity:

public class MainActivity extends BaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{


@InjectView(R.id.quoteGridView)
private AbsListView absListView;
@InjectView(R.id.header1Txt)
private TextView header1Txt;
@InjectView(R.id.header2Txt)
private TextView header2Txt;
@InjectView(R.id.header3Txt)
private TextView header3Txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ActionBar configurations
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);

    ActionBarView actionBarView = new ActionBarView(this);
    actionBarView.setActivityTitle("Quote");
    actionBar.setCustomView(actionBarView);
v4r

Since RoboGuice injects views at setContentView(R.layout.activity_main), I was able to fix this problem by moving actionBar.setCustomView(actionBarView) before setContentView(R.layout.activity_main).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related