Last active
December 16, 2015 09:59
-
-
Save DazzyG/5416834 to your computer and use it in GitHub Desktop.
A quick demonstation of using custom views in the actionbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private DemoFragment mFragment; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mFragment = new DemoFragment(); | |
setContentView(R.layout.content_frame); | |
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragment) | |
.commit(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getSupportMenuInflater().inflate(R.menu.demo, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
// Do some stuff | |
return true; | |
case R.id.menu_edit | |
// Select our editmode | |
mFragment.editModeSelected(true); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.demo, container, false); | |
// Inflate a "Done" custom action bar view to serve as the "Up" | |
// affordance. | |
inflater = (LayoutInflater) getSherlockActivity().getSupportActionBar().getThemedContext() | |
.getSystemService(SherlockActivity.LAYOUT_INFLATER_SERVICE); | |
customActionBarView = inflater.inflate(R.layout.actionbar_custom_view_done, null); | |
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// "Done" | |
editModeSelected(false); | |
} | |
}); | |
return view; | |
} | |
public void editModeSelected(boolean selected) { | |
final ActionBar actionBar = this.getSherlockActivity().getSupportActionBar(); | |
if (selected) { | |
// Show the custom action bar view and hide the normal Home icon and | |
// title. | |
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, | |
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | |
| ActionBar.DISPLAY_SHOW_TITLE); | |
actionBar.setCustomView(customActionBarView); | |
} else { | |
// Leave edit mode. | |
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE, | |
ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | |
| ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/actionbar_done" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" > | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:drawableLeft="@drawable/ic_action_done" | |
android:drawablePadding="8dp" | |
android:gravity="center_vertical" | |
android:paddingRight="20dp" | |
android:text="@string/done" | |
android:textColor="@color/white" /> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment