I have a sliding drawer implemented at the bottom of my layout. I want to show one cell as a default. I have been stuck on this for a couple of hours, so any suggestions or ideas will be greatly appreciated.
Here is my XML file:
<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" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#color/Red"
android:text="Notification"
android:textColor="#color/White" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/LightGrey">
<ListView
android:id="#+id/notificationList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/listselector_yellow"
>
</ListView>
<View
android:id="#+id/dummieView"
android:layout_width="match_parent"
android:layout_height="800dp"
android:background="#color/LightGrey" >
</View>
</LinearLayout>
</SlidingDrawer>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/White"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:divider="#color/LightGrey"
/>
</android.support.v4.widget.DrawerLayout>
You can use ListView's setSelection(int position) method to scroll to a row. See docs here.
Related
I followed this tutorial from CodePath to create the navigation drawer in android: https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer.
What i want to achieve is to add the image view above of the list view, i tried different solution but none works.
this is the activity_main.xml
<my.custom.package.path.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/lvDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:paddingTop="24dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="0dp"
android:background="#android:color/background_light" />
</my.custom.package.path.FragmentNavigationDrawer>
This is the drawer_nav_item.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="48dp">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ivIcon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
</RelativeLayout>
and the final result is this one:
What i want to achieve is that:
But only the background image on the top.
Can someone please help me?
Best
Brus
<my.custom.package.path.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<!--Place your ImageView here-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ListView
android:id="#+id/lvDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:paddingTop="24dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="0dp"
android:background="#android:color/background_light" />
</LinearLayout>
</my.custom.package.path.FragmentNavigationDrawer>
Place the ImageView Above the Listview within LinearLayout
If you're interested in the Material Design style nav drawer, take a look at this answer: https://stackoverflow.com/a/27664931/4764088
I recently added a Navigation Drawer to my Android layout. The Drawer itself is working fine, however the old layout is not displaying at all (the elements are there, but not visable).
I have tried to play around with the positioning of the elements, but nothing seems to work.
This is my Layout:
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="#+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFFFF">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="#+id/displayframe"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Content"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#AAAAAA"
android:id="#+id/spinner"
android:layout_margin="15dp"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1399cb"
android:layout_weight="1"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:background="#1399cb"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#1399cb"
android:gravity="center" >
</RelativeLayout>
</LinearLayout>
</FrameLayout>
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="schoolbus.tracker.datavoice.schoolbusdrawer.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer"
/>
</android.support.v4.widget.DrawerLayout>
This is the navigation drawer fragment:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="match_parent" android:choiceMode="singleChoice"
android:divider="#android:color/black" android:dividerHeight="0dp"
android:background="#cccc" tools:context=".NavigationDrawerFragment"
/>
and the layout I use to inflate it:
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#1399cb"
tools:context=".MainActivity$PlaceholderFragment">
<ImageView android:id="#+id/icons" android:layout_height="40dp" android:layout_width="40dp"
android:contentDescription="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/textView4"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:textSize="19dp"
android:layout_alignTop="#+id/icons"
android:layout_toRightOf="#+id/icons"
android:layout_toEndOf="#+id/icons" />
</RelativeLayout>
Thank you in advance.
Why don't go like this:
main activity layout
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Drawer
then in your activity just add fragment to content_frame and that's it:
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragmentToShow)
.commit();
Actually you haven't closed your FrameLayout inside this layout file,nevertheless i can't see any mistake placed except for that.
EDITED: You must put your frameLayout inside the LinearLayout
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="#+id/displayframe">
<FrameLayout android:id="#+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Content"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#AAAAAA"
android:id="#+id/spinner"
android:layout_margin="15dp"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1399cb"
android:layout_weight="1"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:background="#1399cb"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#1399cb"
android:gravity="center" >
</RelativeLayout>
</FrameLayout>
</LinearLayout>
Also Is there any type of rendering problem when you compile it?
I am using a custom list view to display data in my app, Then i tried to add navigation drawer
Clicks on drawer layout doesnt work. It end up opening the custom list view as it is behind the drawer list view. Drawer list view never gets focus, Even when the drawer is opened
Below is my main activity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:focusable="true"/>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"/>
</android.support.v4.widget.DrawerLayout>
<ListView
android:id="#+id/listScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="7dp">
</ListView>
</RelativeLayout>
Use this way :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/actionbar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<include layout="#layout/activity_main" />
<!-- The navigation drawer -->
<include layout="#layout/drawer_layout_right" />
<include layout="#layout/drawer_layout_left" />
</android.support.v4.widget.DrawerLayout>
drawer_layout_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="horizontal" >
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
drawer_layout_left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical" >
<ListView
android:id="#+id/lv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
<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" >
<include layout="#layout/activity_main" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:focusable="true"/>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
I am using Navigation Drawer Pager Sliding Tab Strip. It is showing menus in Slider. Now, I want to add Profile Photo and User Name in Slider. I have tried adding LinearLayout but It is giving ClassCastException.
Source : https://github.com/Balaji-K13/Navigation-drawer-page-sliding-tab-strip
activity.xml :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
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/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/lightish"
android:choiceMode="singleChoice"
android:divider="#color/blue"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
Use this layout and you'll get what you need.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- Left drawer -->
<LinearLayout
android:id="#+id/leftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/imgProfilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/user_placeholder_88"
android:contentDescription="#string/profile_pic" />
<TextView
android:id="#+id/lblUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Medium" />
<ListView
android:id="#+id/drawerList"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#color/drawerListDivider"
android:dividerHeight="0dp"
android:listSelector="#drawable/drawer_list_selector"
android:background="#color/drawerListBackground"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Hello i have such layout and i don`t understand how to add static header with photo and text
to Navigation Drawler. Any example? Thanks
like this one
<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">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#9d9d9d"
android:dividerHeight="1dp"
android:background="#252525" />
It's actually not as simple as user665 suggests. If you're going straight off the example provided by Google, you need to let the DrawerLayout know what its content is by changing DrawerLayout.isDrawerOpen and DrawerLayout.closeDrawer to the View your ListView is wrapped in, otherwise you're going to throw a ClassCastException because the LayoutParams won't match up.
So, to make a static header, you could do somehting like:
<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" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/drawer_content"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<ImageView
android:id="#android:id/icon"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="#null"
android:scaleType="centerCrop" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Then reference R.id.drawer_content when using the methods I listed above.
After the FrameLayout, the first view will accepted as Navigation Drawer, so you create a layout after FrameLayout, then put your Views inside of it. Here is a example, it is very basic but you will get the point.
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_height="match_parent"
android:layout_width="320dp"
android:orientation="vertical"
android:layout_gravity="start" >
<RelativeLayout
android:id="#+id/NavDrawerInfo"
android:layout_width="320dp"
android:background="#drawable/bg"
android:layout_height="100dp">
<ImageView android:src="#drawable/ic_launcher"
android:id="#+id/userImage"
android:layout_width="200dp"
android:layout_marginRight="200dp"
android:layout_height="100dp"/>
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="125dp"
android:textSize="20sp"
/>
<TextView
android:id="#+id/userImage"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="125dp"
android:textSize="20sp"
/>
</RelativeLayout>
<ExpandableListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:childDivider="#color/list_divider"
android:groupIndicator="#drawable/group_indicator"
android:dividerHeight="2dp"
android:gravity="start"
android:background="#f1f1f1"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>