How to Implement NavigationDrawer icon like in Android L.
I have updated play store app where i can see icon of navigation drawer something like this:
Here big change is the new “hamburger” button, which is bigger, spaced off from the left edge, and no longer has a category icon. Clicking on it opens up the side menu, and the icons turns into a back arrow with a smooth little animation.
Is it possible to implement the same in lower versions of android ?
Any example or sample ?
Edit - October 18th
As of October 17th it's not necessary to use a Third-Party library for this anymore. Google just released the Android 5.0 SDK along with a new v7 appcompat library. The new v7 appcompat added support for material design user interfaces and updated ActionBarDrawerToggle, which contains the menu-to-arrow animation.
So in order to have the Burger-to-Arrow animation in your NavigationDrawer you just have to use the new ActionBarDrawerToggle (import android.support.v7.app.ActionBarDrawerToggle) and the NavigationDrawer like before (see Developer Training).
While the ActionView by markushi gives you the desired transformation between the burger-icon and the arrow it lacks the option for being used in the "Standard" ActionBar. For this you should consider to use the library material-menu by balysv.
The usage of the library itself may be straightforward, but if you've used the ActionBarDrawerToggle previously you should remove it.
Following just a little "How to" for everyone who wants to use the DrawerLayout without the ActionBarDrawerToggle but with the MaterialMenu library:
1 - Add library in build.gradle file
dependencies {
//...
compile 'com.balysv.materialmenu:material-menu:1.3.1'
}
2 - Init MaterialMenuIcon
materialMenu = new MaterialMenuIcon(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN);
3 - Set DrawerListener and change IconState accordingly
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
materialMenu.setTransformationOffset(
MaterialMenuDrawable.AnimationState.BURGER_ARROW,
isDrawerOpened ? 2 - slideOffset : slideOffset
);
}
#Override
public void onDrawerOpened(View drawerView) {
isDrawerOpened = true;
}
#Override
public void onDrawerClosed(View drawerView) {
isDrawerOpened = false;
}
});
Note: isDrawerOpened should be global variable
4 - Open/Close NavDrawer
Add this to onOptionsItemSelected(MenuItem item)
if(item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
Note: Since we're not using the ActionBarDrawerToggle we have do this on our own.
Further steps like saving the state of the MenuIcon can be found here.
Edit
As mentioned by Piyush Kukadiya MaterialMenu uses NineOldAndroids for compatibility reasons. If you don't want this because your app only supports API-Level 11 and above here's what you roughly need to do (though I'd say it’s probably not worth the hassle - furthermore you'll only save 39kb):
Download the Library from GitHub
Import it as a new module to your Project
Set it as a dependency to your app-module
Remove compile 'com.nineoldandroids:library:2.4.0' from build.gradle file of the library and set minSdk to 11
Remove any import referecing com.nineoldandroids.*
As Google release the new support library Android Support Library, revision 21,you can see how to achieve the effect in ActionBarDrawerToggle this page.
Related
I have created a DrawerLayout and also have an ImageView (a 'hamburger') that opens it when clicked.
My problem is that when using the following code, a lint error is shown in AndroidStudio: Must be one or more of: Gravity.LEFT, Gravity.RIGHT..., there is no Gravity.START in there.
ImageView openDrawerImageView = findViewById(R.id.open_drawer_image_icon);
openDrawerImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.START);
}
});
I looked for a while for an answer but did not find one. I decided to leave it for later since this is only a lint error and the app actually does work with Gravity.START.
Later I needed to close the drawer in some scenario so I used: drawerLayout.closeDrawer(START) then I used the autocomplete, and got the answer: GravityCompat
drawerLayout.closeDrawer(GravityCompat.START);
this.navDrawerToggle = new ActionBarDrawerToggle(this,this.getApplicationContext(), this.navDrawerLayout,R.drawable.ic_drawer, R.string.app_name, R.string.app_name ){
#Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(navDrawerTitle);
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle(appTitle);
invalidateOptionsMenu();
}
};
I'm new to android i i'm making an app in which i was using Navigation Drawer i was following a tutorial but then in the end i stuck on ActionBarDrawerToggle() which i think is now deprecated and that tutorial was made earlier please can anyone tell me how to use deprecated ActionBarDrawerToggle or any other way to use android.support.v4.app.ActionBarDrawerToggle; ??
please explain in detail as i'm new to android and programming.
i also imported
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
but still not working.
As the official oracle Java documentation hints,
You may have heard the term, "self-deprecating humor," or humor that
minimizes the speaker's importance.
A deprecated class or method is like that. It is no longer important.
It is so unimportant, in fact, that you should no longer use it, since
it has been superseded and may cease to exist in the future.
And also stated in the official android documentation
This class is deprecated. Please use ActionBarDrawerToggle in
support-v7-appcompat.
That being said, you should try to find a different solution to your problem. For example, you could use the support v7 version as suggested by the Android docs. Here is an example on what you could do instead of using a deprecated class.
Do not use the support v4 ActionBarDrawerToggle. Deprecated libraries will crash your app unexpectedly. I know this by experience. Use the support v7 one. Here is a pretty simple implementation.
Is it possible to have POPUP menu like play store attached to each row of adapter
what i done so far
holder.rl_overflow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, holder.rl_overflow);
popup.getMenuInflater().inflate(R.menu.overflow, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return false;
}
});
popup.show();
}
});
but constructor for POPUP menu says its available from API 11. I go through the developers.android.com and i found, its can be added using SUPPORT V7 library "https://developer.android.com/reference/android/support/v7/widget/PopupMenu.html" but i'm not able to implement this using ABS, please help someone.
Use like this PopupMenu in ActionBarSherlock.
Styling of the PopupMenu -
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
<style name="PopupMenu.MyAppTheme" parent="#style/Widget.Sherlock.ListPopupWindow">
<item name="android:popupBackground">#android:color/white</item>
</style>
If you are using the support library you should discard using the ABS. Instead, import the support library into your workspace which can be found in ~/adt-bundle-linux-x86_64-20130729/sdk/extras/android/support/v7/appcompact and use is in your project. And also don't forget to add the support library which can be brought up right clicking into your project and entering Android Tools -> Add Support Library
Using appcompact you will have to extend your activity class with ActionBarActivity. And also using the appcompact you have to make changes in your styles folder. You could refer to this. Do not also forget to update the values-v11 and values-v14 file. Doing all of this will make your application compatible.
P.S. If any error occurs in your appcompact library. Don't panic look at the error logs and open the file that seems to contain the error. Most probably you will have to refresh the file and after that you just fix project properties, and the error goes away.
Hope this helps :)
I create unit-test from Espresso in Android. My project has NavigationDrawer. I create test which must slide NavigationDrawer and click on button. In understand, how create slide action in my test.
My current solution:
try
{
runTestOnUiThread(new Runnable()
{
#Override
public void run()
{
DrawerLayout drawer = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.LEFT);
}
});
}
catch (Throwable e)
{
e.printStackTrace();
}
But I think it's bad code.
There is a description how to do this in android-test-kit group, explained by ValeraZakharov - link.
Espresso 1.1 has now been out for a little while, but I've still been completely unable to find any Navigation Drawer functionality within it... That is, until I found out that this functionality is actually included within a totally separate library file: espresso-contrib, which is available via gradle with
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
This is super briefly mentioned on the "Why Espresso" page of the Espresso website:
If you need any functionality from the contrib library, such as DrawerActions, copy the espresso-contrib jar from here.
Here's the DrawerActions documentation. And here is some sample code that uses it. Hopefully that puts you in the right direction; the EspressoSamples page has no example code for this...
openDrawer() was deprecated, you can use this code instead.
I can confirm it works
onView(withId(R.id.my_drawer_layout)).perform(DrawerActions.open());
NavigationDrawer support is coming soon in the next release of Espresso. For the time being, you can implement your own ViewAction, where you would add the code inside your Runnable. This is an incomplete solution because you will likely experience timing issues related to opening/closing of the drawer. Espresso 1.1 will take care of this.
I want to show/hide ProgressBar in ActionBar on all android devices.
I am using android support library (android-support-v7-appcompat).
My activity extends ActionBarActivity and in onCreate it requests window feature supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); (before setting content).
On my button click I show/hide ProgressBar.
public void onClick(View v) {
if(v.getId() == R.id.buttonProgress) {
if(progress) {
setProgressBarIndeterminateVisibility(false);
progress = false;
} else {
setProgressBarIndeterminateVisibility(true);
progress = true;
}
}
}
This code works fine on android API higher than 11. But I have proglem with API lower than 11. The ProgressBar is not showing up. There is no error in LogCat.
I have noticed that when I show ProgressBar in onCreate it works. I also can hide it from onCreate.
Do you have solution for this problem?
Thank you!
call
setSupportProgressBarIndeterminateVisibility(true)
if call it from fragment cast the activity for example:
ActionBarActivity ac =(ActionBarActivity) getActivity();
ac.setSupportProgressBarIndeterminateVisibility(true);
There is no action bar for apps lower than API 11.. If you use ProgressBar for Less than API 11 (Honeycomb) it does show up but it will tiny circle revolving on the top right of the title bar (thin bar on top of the app). Well, that depends on the theme.
If you want an actionbar you may want to look into an external library : ActionBarSherlock
The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
And ActionBar is added in support liabrary to allow implementation of the action bar user interface design pattern back to Android 2.1 (API level 7) and higher. Use of this class requires that you implement your activity by extending the new ActionBarActivity class.
Have a look at the official document here
However you can achieve this by using ActionBarSherlock library.
Check out this
Site for ABS Here you can get the sample programs ABS Library