Last active
December 17, 2015 17:09
-
-
Save lamprosg/5643842 to your computer and use it in GitHub Desktop.
(Android) Action Bar
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
<!-- | |
Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the Theme.Holo theme | |
(or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute | |
is set to "11" or greater. | |
--> | |
<!-- Example --> | |
<manifest ... > | |
<uses-sdk android:minSdkVersion="4" | |
android:targetSdkVersion="11" /> | |
... | |
</manifest> | |
<!-- REQUIRED HOLO THEME FOR THE ACTION BAR TO LOAD --> | |
<application | |
.... | |
.... | |
android:theme="@android:style/Theme.Holo.Light" | |
.... | |
<!-- Removing the action bar --> | |
<activity android:theme="@android:style/Theme.Holo.NoActionBar"> | |
<!-- Android 4.0 (API level 14) and higher, "split action bar" (In earlier versions system just ignores this) --> | |
android:uiOptions="splitActionBarWhenNarrow"> | |
</activity> |
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
//Might need this | |
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); | |
//Get the action bar | |
ActionBar actionBar = getActionBar(); | |
//You can hide it by code | |
actionBar.hide(); | |
//Or show it | |
actionBar.show(); | |
actionBar.setHomeButtonEnabled(true); //enable or disable the Home button in the action bar | |
actionBar.setDisplayHomeAsUpEnabled(true); //Enable UP navigation | |
// When the activity first starts, the system populates the action bar and overflow menu by calling | |
// onCreateOptionsMenu() for your activity. | |
//YOU CAN ALSO USE onCreate() just to load the action bar | |
//Make an actionbar layout and load it like so | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.main_activity, menu); | |
return true; | |
} | |
//When the user selects an action item, your activity receives a call to onOptionsItemSelected(), | |
//passing the ID supplied by the android:id attribute | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.home: | |
// app icon in action bar clicked; go home | |
Intent intent = new Intent(this, SomeActivity.class); | |
//With this flag, if the activity you're starting already exists in the current task, | |
//then all activities on top of it are destroyed and it is brought to the front | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
//OR in case the user can enter the current activity from another application | |
//you should add the FLAG_ACTIVITY_NEW_TASK flag. | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
//You can also change the text which is displayed alongside the application icon at runtime. | |
actionBar.setSubtitle("mytest"); | |
actionBar.setTitle("Some Title"); | |
//Background of the actionbar | |
Drawable d = getResources().getDrawable(R.drawable.background_image_name); | |
actionBar.setBackgroundDrawable(d); | |
//See here for more options | |
//http://android-developers.blogspot.gr/2011/04/customizing-action-bar.html |
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
/* | |
You can also add a custom View to the ActionBar. For this you use the setCustomView method for the ActionView class. | |
You also have to enable the display of custom views via the setDisplayOptions() method | |
by passing in the ActionBar.DISPLAY_SHOW_CUSTOM flag. | |
*/ | |
/******************************* | |
//The layout | |
<?xml version="1.0" encoding="utf-8"?> | |
<EditText xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/searchfield" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:inputType="textFilter" > | |
</EditText> | |
*******************************/ | |
ActionBar actionBar = getActionBar(); | |
// add the custom view to the action bar | |
actionBar.setCustomView(R.layout.actionBarEditText_layout); | |
//Get the edit text | |
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield); | |
//Attach a listener to the custon view | |
search.setOnEditorActionListener(new OnEditorActionListener() { | |
@Override | |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { | |
Toast.makeText(MainActivity.this, "Search triggered", Toast.LENGTH_LONG).show(); | |
return false; | |
} | |
}); | |
//set options to display the custom view | |
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME); |
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
/* | |
A contextual action mode activates a temporary ActionBar that overlays the application's ActionBar | |
for the duration of a particular sub-task. | |
For example use this feature to replace an action item with a ProgressBar view. | |
*/ | |
//The following activity replace the icon at runtime with an action view which contains a ProgressBar view. | |
public class MainActivity extends Activity { | |
private MenuItem menuItem; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); //Load the activity's layout | |
ActionBar actionBar = getActionBar(); | |
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) | |
{ | |
case R.id.menu_load: //The icon's id we want to replace | |
menuItem = item; | |
//Set the temporary view on the action bar | |
menuItem.setActionView(R.layout.progressbar); //Replace it with this | |
menuItem.expandActionView(); | |
//Do whatever task you want | |
TestTask task = new TestTask(); | |
task.execute("test"); | |
break; | |
default: | |
break; | |
} | |
return true; | |
} | |
private class TestTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
// Simulate something long running | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
//WE'RE DONE. REMOVE THE ACTION VIEW | |
menuItem.collapseActionView(); | |
menuItem.setActionView(null); | |
} | |
}; | |
} | |
/************************************************************** | |
* Layout for the progress bar | |
* | |
<?xml version="1.0" encoding="utf-8"?> | |
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/progressBar2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
</ProgressBar> | |
* | |
* *************************************************************/ |
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
public class MainActivity extends FragmentActivity implements | |
ActionBar.TabListener { | |
/** | |
* The serialization (saved instance state) Bundle key representing the | |
* current tab position. | |
*/ | |
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Set up the action bar to show tabs. | |
final ActionBar actionBar = getActionBar(); | |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
// For each of the sections in the app, add a tab to the action bar. | |
actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this)); | |
actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this)); | |
actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this)); | |
} | |
@Override | |
public void onRestoreInstanceState(Bundle savedInstanceState) { | |
// Restore the previously serialized current tab position. | |
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { | |
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
// Serialize the current tab position. | |
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
@Override | |
public void onTabSelected(ActionBar.Tab tab, | |
FragmentTransaction fragmentTransaction) { | |
// When the given tab is selected, show the tab contents in the | |
// container view. | |
Bundle args = new Bundle(); | |
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); | |
Fragment fragment = new DummySectionFragment(); | |
fragment.setArguments(args); | |
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); | |
} | |
@Override | |
public void onTabUnselected(ActionBar.Tab tab, | |
FragmentTransaction fragmentTransaction) { | |
} | |
@Override | |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { | |
} | |
/** | |
* A dummy fragment representing a section of the app | |
*/ | |
public static class DummySectionFragment extends Fragment { | |
public static final String ARG_SECTION_NUMBER = "placeholder_text"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
TextView textView = new TextView(getActivity()); | |
textView.setGravity(Gravity.CENTER); | |
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); | |
return textView; | |
} | |
} | |
} |
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
public class MainActivity extends FragmentActivity implements | |
ActionBar.OnNavigationListener { | |
/** | |
* The serialization (saved instance state) Bundle key representing the | |
* current dropdown position. | |
*/ | |
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Set up the action bar to show a dropdown list. | |
final ActionBar actionBar = getActionBar(); | |
actionBar.setDisplayShowTitleEnabled(false); | |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); | |
final String[] dropdownValues = getResources().getStringArray(R.array.dropdown); | |
// Specify a SpinnerAdapter to populate the dropdown list. | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(actionBar.getThemedContext(), | |
android.R.layout.simple_spinner_item, android.R.id.text1, | |
dropdownValues); | |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
// Set up the dropdown list navigation in the action bar. | |
actionBar.setListNavigationCallbacks(adapter, this); | |
// Use getActionBar().getThemedContext() to ensure | |
// that the text color is always appropriate for the action bar | |
// background rather than the activity background. | |
} | |
@Override | |
public void onRestoreInstanceState(Bundle savedInstanceState) { | |
// Restore the previously serialized current dropdown position. | |
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { | |
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
// Serialize the current dropdown position. | |
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onNavigationItemSelected(int position, long id) { | |
// When the given dropdown item is selected, show its contents in the | |
// container view. | |
Fragment fragment = new DummySectionFragment(); | |
Bundle args = new Bundle(); | |
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); | |
fragment.setArguments(args); | |
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); | |
return true; | |
} | |
/** | |
* A dummy fragment | |
*/ | |
public static class DummySectionFragment extends Fragment { | |
public static final String ARG_SECTION_NUMBER = "placeholder_text"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
TextView textView = new TextView(getActivity()); | |
textView.setGravity(Gravity.CENTER); | |
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); | |
return textView; | |
} | |
} | |
} |
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
final String[] actions = new String[] { | |
"About", | |
"Terms & Conditions", | |
"Privacy Policy" | |
}; | |
/** Create an array adapter to populate dropdownlist */ | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions); | |
/** Enabling dropdown list navigation for the action bar */ | |
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); | |
/** Defining Navigation listener */ | |
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() { | |
@Override | |
public boolean onNavigationItemSelected(int itemPosition, long itemId) { | |
if (actions[itemPosition] == "About") | |
{ | |
} | |
if (actions[itemPosition] == "Terms & Conditions") | |
{ | |
Intent intent = new Intent(DashboardActivity.this, WebActivity.class); | |
intent.putExtra(URL, getString(R.string.terms_url)); | |
startActivity(intent); | |
} | |
if (actions[itemPosition] == "Privacy Policy") | |
{ | |
Intent intent = new Intent(DashboardActivity.this, WebActivity.class); | |
intent.putExtra(URL, getString(R.string.privacy_url)); | |
startActivity(intent); | |
} | |
return false; | |
} | |
}; | |
/** Setting dropdown items and item navigation listener for the actionbar */ | |
getActionBar().setListNavigationCallbacks(adapter, navigationListener); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment