how to change image inside sliding drawer on touch? - android

I am having an sliding drawer opening from right to left navigation when i touch the drawer icon on action bar. Working fine and closing on toggling the icon. no problem
I followed http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
In the sliding layout I have one small icon vibrate_ON.png.
Now i want when i touch the image in the sliding menu it will be changed to vibrate_OFF and so on toggling, keeping the sliding menu open.
how to do that?

You could do something like:
vibrateImageOn.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
vibrateImageOff.setVisibility(View.Visible);
vibrateImageOn.setVisibility(View.GONE);
}
});
vibrateImageOff.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
vibrateImageOn.setVisibility(View.Visible);
vibrateImageOff.setVisibility(View.GONE);
}
});
Assuming you load them both into ImageViews and they have equal positions, then this should work. Though, it is a bit messy..

You can put a layout in sliding menu for more controls. To do that, you must do some step:
1) Layout:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4" />
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="220dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff">
<!-- you can set any layout here -->
<TextView
android:id="#+id/tv_word"
android:text="New words"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_width="210dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
2) Handle Event (plz don't forget closeDrawer)
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
TextView tv_word = (TextView )findViewById(R.id.tv_word );
tv_word.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.closeDrawer(linearLayout);//don't forget it
//.....
}
});

Related

DrawerLayout not functioning and blocks not related widgets

I havd an Activity which has a TextView and i wanted to add a navigation drawer to that Activity. So I changed my XML and implemented DrawerLayout.
After implementing the DrawerLayout it simply does not functioning(meaning it don't open) and the FrameLayout implemented within the DrawerLayout blocks the not related to the DrawerLayout TextView text.
drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.dl.master.lyrics.lyricsmaster.LyricsActivity"
android:id="#+id/drawer_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/frame_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/text_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- The navigation drawer -->
<TextView
android:layout_width="220dp"
android:layout_height="match_parent"
android:text="Try me! NOW"
/>
</android.support.v4.widget.DrawerLayout>
<!--
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
-->
The TextView inside the LinearLayout is the not related DrawerLayout widgets which cannot be seen. And the other TextView is the navigation drawer TextView. I know the navigation drawer widget has to have android:layout_gravity="start" property but it apparently has no layout parent so AS does not let me write it.
When running the Activity i see the text "Try it NOW" of the navigation drawer TextView and that's all.
DrawerActivity.java:
public class LyricsActivity extends AppCompatActivity
{
private TextView text;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer);
this.initializeViews();
this.actionBarDrawerToggle = new ActionBarDrawerToggle(LyricsActivity.this,this.drawerLayout,
null,R.string.open_drawer_description,R.string.close_drawer_description);
this.drawerLayout.addDrawerListener(actionBarDrawerToggle);
//this.drawerLayout.openDrawer(GravityCompat.START); // java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
}
#Override
protected void onResume()
{
super.onResume();
this.text.setText(getIntent().getStringExtra("Tags"));
}
/**
* Initialize all my views.
*
*/
public void initializeViews()
{
this.lyrics = (TextView) findViewById(R.id.text_tv_id);
this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_id);
}
I read all the documentation and a lot of SO post and nothing helped me nor addressed this kind of problem.
DrawerLayout determines which Views to use as a drawers by their layout_gravity attribute. Simply add this attribute to your TextView with the appropriate value. For example:
<!-- The navigation drawer -->
<TextView
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:text="Try me! NOW"
/>

Navigation view that can be opened from left to right and right to left [duplicate]

http://developer.android.com/training/implementing-navigation/nav-drawer.html
According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(
The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
</FrameLayout>
<!-- Left drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice" />
<!-- Right drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
My App crashed with "No drawer view found with gravity LEFT" error.
So added this to the onOptionsItemSelected:
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
To add to https://stackoverflow.com/a/21781710/437039 solution.
If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
Next. In class NavigationDrawerFragment, you have to create 3 methods:
Method 1
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
Method 2
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
Method 3
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
Only now, the right-side drawer will function.
You can use NavigationView from Material design. For ex :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know this is an old question but for those who are still looking for the answer :
Yes, it is possible. Please check my answer on the link below :
https://stackoverflow.com/a/19358114/1572408
Then Use these codes #amal i think this ll help you.
XML:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/counter_bg" >
<ImageView
android:id="#+id/iconl"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/iconr"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<ListView
android:id="#+id/list_slidermenu2"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
//set the required images
Activity code :
ImageView iconl,iconr;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;
ImageView iconl,iconr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconl = (ImageView)findViewById(R.id.iconl);
iconr = (ImageView)findViewById(R.id.iconr);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
iconl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.START);
mDrawerLayout.closeDrawer(Gravity.END);
}
});
iconr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.END);
mDrawerLayout.closeDrawer(Gravity.START);
}
});
}
}
and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.
write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im1=findViewById(R.id.humburgericon);
im1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("WrongConstant")
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.END);});
Navigation Drawer from right hand side is possible.
And this easier than it seems.
In my opinion the most simple solution is:
Extend DrawerLayout class and override open() and close() functions as below
class EndDrawerLayout : DrawerLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
override fun open() = openDrawer(GravityCompat.END)
override fun close() = closeDrawer(GravityCompat.END)
}
Use the custom DrawerLayout in your XML
Set NavigationView attribute android:layout_gravity="end"
<com.custom.myapplication.ui.EndDrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.custom.myapplication.ui.EndDrawerLayout>
Enjoy

Button beneath navigation drawer not working when pressed

Yet, when the button is placed in the hierarchy so that it can be seen on top of the navigation drawer, the button functions properly. However, the button should be hidden behind the navigation drawer when it is slid out, so this is not desirable.
Below is the code from MainActivity.java
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
public ProgressDialog progBar;
public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
cart_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
start_request();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void start_request()
{
String pkg = getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
startActivity(intent);
if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}
I am assuming the issue lies within the class above, but for completion's sake, I pasted the two XML files of interest below.
activity_main.xml As you can see, the ImageButton is currently "above" the navigation drawer in hierarchy so as to make it be covered by the navigation drawer. Moving the ImageButton "below" makes the button work properly, but causes it to appear on top of the navigation drawer (and not tinted like the rest of the layout).
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fontify="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/myPrimaryColor">
<View
android:id="#+id/block1"
android:layout_height="240dp"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:background="#drawable/block_primary"
/>
<TextView
android:id="#+id/title1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/toolbar_actionbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/title"
android:textColor="#color/white"
android:textSize="34sp"
android:fontFamily="sans-serif"
/>
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<ImageButton
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="268dp"
android:src="#drawable/ic_chevron_up"
android:background="#drawable/fab_simple"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/fragment_drawer"
android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
fragment_navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Provides a margin at the top of the navigation drawer. -->
<View
android:id="#+id/navWhiteSpace1"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#color/myNavigationDrawerBackgroundColor"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_below="#+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="#color/myNavigationDrawerBackgroundColor"/>
</RelativeLayout>
I ran into the same issue when attempting to follow various navigation drawer tutorials online.
What worked for me was using Mike Penz's MaterialDrawer library on GitHub. He has a very simple tutorial in the "readme" file found on the bottom of the page.
Hopefully someone else with a better understanding of Java can explain why your code failed.

Navigation Drawer not occupying the entire screen height

I have a drawer layout with drawer on left.
Drawer has a button on click of which I want to replace the view of the drawer with a new view. The problem is , when new view gets replaced it doesn’t occupy the entire screen height.
To illustrate I have given a background colour to my navigation drawer views.
I want the Green view to occupy the entire screen height. Copy-paste the code below to get going:
MainActivity.java
public class MainActivity extends Activity {
LayoutInflater inflater;
DrawerLayout drawer;
RelativeLayout parentRL;
RelativeLayout viewToBeRemovedRL;
View viewToBeAdded;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
parentRL = (RelativeLayout) findViewById(R.id.parentRL);
viewToBeRemovedRL = (RelativeLayout) findViewById(R.id.viewToBeReplaced);
}
public void onRemoveView(View view) {
viewToBeAdded = inflater.inflate(R.layout.add_view, null);
viewToBeRemovedRL.setVisibility(View.GONE);
parentRL.addView(viewToBeAdded);
}
public void onOpenDrawer(View view) {
drawer.openDrawer(parentRL);
}
}
main_activity.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/leftRL"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="75dp"
android:onClick="onOpenDrawer"
android:text="Open Drawer" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/parentRL"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="left" >
<RelativeLayout
android:id="#+id/viewToBeReplaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" >
<Button
android:layout_width="100dp"
android:layout_height="75dp"
android:onClick="onRemoveView"
android:text="Change View" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
add_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="290dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="New view"
/>
</RelativeLayout>
Usually when this happens, the drawer actually DOES occupy the entire screen height, it's just that the default background colour of the view is clear.
Set you RelativeLayout#id/parentRL 's background to a solid colour or some drawable, e.g.
android:background="#android:color/black"
and your drawer should cover the entire screen height.

Android - Is Navigation Drawer from right hand side possible?

http://developer.android.com/training/implementing-navigation/nav-drawer.html
According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(
The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
</FrameLayout>
<!-- Left drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice" />
<!-- Right drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
My App crashed with "No drawer view found with gravity LEFT" error.
So added this to the onOptionsItemSelected:
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
To add to https://stackoverflow.com/a/21781710/437039 solution.
If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
Next. In class NavigationDrawerFragment, you have to create 3 methods:
Method 1
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
Method 2
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
Method 3
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
Only now, the right-side drawer will function.
You can use NavigationView from Material design. For ex :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I know this is an old question but for those who are still looking for the answer :
Yes, it is possible. Please check my answer on the link below :
https://stackoverflow.com/a/19358114/1572408
Then Use these codes #amal i think this ll help you.
XML:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/counter_bg" >
<ImageView
android:id="#+id/iconl"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/iconr"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<ListView
android:id="#+id/list_slidermenu2"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
//set the required images
Activity code :
ImageView iconl,iconr;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;
ImageView iconl,iconr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconl = (ImageView)findViewById(R.id.iconl);
iconr = (ImageView)findViewById(R.id.iconr);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
iconl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.START);
mDrawerLayout.closeDrawer(Gravity.END);
}
});
iconr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.END);
mDrawerLayout.closeDrawer(Gravity.START);
}
});
}
}
and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.
write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im1=findViewById(R.id.humburgericon);
im1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("WrongConstant")
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.END);});
Navigation Drawer from right hand side is possible.
And this easier than it seems.
In my opinion the most simple solution is:
Extend DrawerLayout class and override open() and close() functions as below
class EndDrawerLayout : DrawerLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
override fun open() = openDrawer(GravityCompat.END)
override fun close() = closeDrawer(GravityCompat.END)
}
Use the custom DrawerLayout in your XML
Set NavigationView attribute android:layout_gravity="end"
<com.custom.myapplication.ui.EndDrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
android:id="#+id/app_bar_main"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.custom.myapplication.ui.EndDrawerLayout>
Enjoy

Categories

Resources