Skip to content

Instantly share code, notes, and snippets.

@f2prateek
Created October 30, 2012 18:26
Show Gist options
  • Select an option

  • Save f2prateek/3982054 to your computer and use it in GitHub Desktop.

Select an option

Save f2prateek/3982054 to your computer and use it in GitHub Desktop.
Custom Action Provider in ActionBar - a checkbox with a textview descrption
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_action_provider_action_bar"
android:actionProviderClass=".widget.CheckBoxActionProvider"
android:showAsAction="always"
android:title="Settings"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="?attr/actionBarItemBackground"
android:focusable="true" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="32dip"
android:gravity="center_vertical"
android:text="shadow" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="32dip"
android:layout_height="32dip" />
</LinearLayout>
package com.psrivastava.deviceframegenerator.widget;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.view.ActionProvider;
import com.psrivastava.deviceframegenerator.R;
public class CheckBoxActionProvider extends ActionProvider implements
OnCheckedChangeListener {
/** Context for accessing resources. */
private final Context mContext;
public CheckBoxActionProvider(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView() {
// Inflate the action view to be shown on the action bar.
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.checkbox_action_provider,
null);
TextView textview = (TextView) view.findViewById(R.id.text);
textview.setText(getActionText());
CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(this);
return view;
}
public String getActionText() {
return "Checkbox";
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.checkbox:
Toast.makeText(mContext,
isChecked ? "checked" : "unchecked",
Toast.LENGTH_SHORT).show();
}
}
}
package com.psrivastava.deviceframegenerator.ui;
import android.os.Bundle;
import com.actionbarsherlock.view.Menu;
import com.psrivastava.deviceframegenerator.R;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* {@inheritDoc}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
@Kartik-RTP
Copy link
Copy Markdown

where are you passing the context to ActionProvider?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment