I'm trying to write an android app with a listview of a few different items and also a drawerlayout that you can drag from the left side of the screen. Here's what i mean....
This is what the main screen looks like:
And here's what the drawer menu thing looks like:
The problem i'm having is that when I open the side drawer menu thing and tap an option it doesn't work and the menu just closes. However i'm able to interact with the main listview page. Here's what my code looks like:
String[] homeArray = { "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test" };
private ListView homeListView;
private ArrayAdapter arrayAdapter;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerTitles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mDrawerTitles = getResources().getStringArray(R.array.Menu);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerTitles));
And the activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<FrameLayout
android:id="#+id/content_frame"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp"
android:background="#111"
/>
<LinearLayout 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:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="#+id/homeListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
I have a feeling the problem is in the xml but i'm not sure.
Please post the full code of your Activity that holds the NavigationDrawer.
Furthermore, I would strongly recommend (and so would Google) that you use Fragments for the individual NavigationDrawer sections ("Settings", "Submit Bug" and "Help" in your case).
Do not put all the layout components into the layout file of your Activity.
The layout therefore should look like the following:
<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>
The FrameLayout content_frame is where your Fragment that holds the ListView will go into.
Simply create a custom Fragment (or use a ListFragment) that contains the ListView you displayed in your picture and insert it into the "content_frame".
Use this to replace Fragments inside the content frame: (when you switch your section)
/** Swaps fragments in the main content view */
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new YourListFragment(); // this fragment contains the list with all the "test" items
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
An Example of how your Fragment could look like: YourListFragment.java
The layout file "list_fragment.xml" simply contains whatever layout components you want. For example a ListView. Initialize an populate the ListView inside the Fragments onCreateView() method.
public class YourListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_fragment, container, false);
return rootView;
}
}
All taken from Googles example on how to create a NavigationDrawer:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Related
I have an application composed by a pageviewer with 3 fragments.
one of this fragment is a map and the idea is to show a bottosheetbehaviuor on map marker click.
here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.android.activities.MainActivity"
android:nestedScrollingEnabled="true">
<RelativeLayout
android:id="#+id/rlMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="#layout/content_main"
/>
<androidx.core.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#android:color/white"
app:behavior_hideable="true"
app:behavior_peekHeight="155dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<TextView
android:id="#+id/bottom_sheet_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textSize="16sp" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
then... during oncreate I setup both view pager , map and bottom sheet
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
bottomSheetText = findViewById(R.id.bottom_sheet_text);
View bottomSheet = findViewById(R.id.bottom_sheet);
mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_HIDDEN);
ViewPager viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
the viewpager and map via a call to a a func
private void setupViewPager(ViewPager viewPager) {
Log.d(TAG, "setupViewPager");
ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager());
createUpMap();
adapter.addFragment(mapFrag, "Map");
//TEMP
adapter.addFragment(new SupportMapFragment(), "List");
adapter.addFragment(new SupportMapFragment(), "Session");
viewPager.setAdapter(adapter);
private void createUpMap() {
Log.d(TAG, "createUpMap");
if (mapFrag == null) {
Log.d(TAG, "mapFrag is null, creation of map...");
mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.rlMap);
mapFrag = new SupportMapFragment();
lastly, I ovveride marker click to display the bottomsheet:
mMap.setOnMarkerClickListener(marker -> {
Log.e(TAG, "POINT HAS BEEEN CLICKED status: " + mBottomSheetBehaviour.getState());
if (mBottomSheetBehaviour.getState() == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetText.setText(marker.getTag().toString());
mBottomSheetBehaviour.setPeekHeight(200);
mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
I launch the application, click on the marker and in the log in log I can see correct state ( I'm printing it as you see from calls) but absolutely no way to see it on screen. I 'm little out of ideas as there's no error and i don't know how to move.
Someone can move me in the right direction ?
many thanks!
You can try to build your own BottomSheetDialogFragment and show it with
val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
//show it
bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)
So you don't have to create the view inside your layout file and can simple reuse it.
Update:
You can set your layout in the setupDialog function. Just overrride it and inflate your custom BottomSheetLayout.
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.custom_layout, null);
dialog.setContentView(contentView);
}
I know that this question has been asked a lot of times already but I have went to all of the answers and none of them worked for me.
So, issue with the App is that I am trying to populate an ArrayList into a ListView using an ArrayAdapter and the app MainActivity doesn't show anything but white screen when launched.
Here's
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceholderFragment extends Fragment{
private ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/46",
"Weds - Cloudy - 72/63",
"Thurs - Rainy - 64/51",
"Fri - Foggy - 70/46",
"Sat - Sunny - 76/68"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
return rootView;
}
}
fragment_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
Here's list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="#+id/list_item_forecast_textview"
>
</TextView>
Of course the activity is not showing anything, you're not placing the fragment inside your activity! Use this in your activity's onCreate():
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
Where R.id.container is a layout inside your main_activity_layout file (empty layout)
UPDATE:
have these 2 file:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
fragment_main:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
keep the 3° layout file, it's ok.
Now, in your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
}
also:
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
//move this after the initialization of adapter
listView.setAdapter(mForecastAdapter);
When you set the ListView adapter,the adapter's reference is null and you set the null adapter for ListView.You should create the adapter before set ListView adapter.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
listView.setAdapter(mForecastAdapter);
Is your PlaceHolderFragment within your MainActivity.java file?
If so, try separating it on its own file PlaceHolderFragment.java..
As far as I know, java only allows one public class per source file.. That maybe the reason of your problem..
I am adding a drawer layout to my Activity which uses fragments. I've implemented this just as described here per the android documentation. And the drawer layout Adapter and View all work in that they show up, but the drawer extends "under" the fragment content. Here is a picture of what is happening:
Does anyone know why this might be? My code for the activity's xml, and the activity add fragment is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list); // for drawer navigation
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
fragmentTransaction.commit();
setUpDrawer(fragmentManager);
}
private void setUpDrawer(FragmentManager fm) {
String[] drawerItems = getResources().getStringArray(
R.array.drawer_array);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView drawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_nav_drawer, drawerItems));
// Set the list's click listener
drawerList.setOnItemClickListener(new
DrawerItemClickListener(drawer,drawerList,drawerItems,fm));
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ListView
android:id="#+id/alarm_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
<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>
Do I have to set a z-index somewhere? I can provide any further info needed I'd just really like to figure this out!
I don't what produces this issue , but to solve it u have to add the fragment to the content of your FrameLayout which has the id = content_frame.
So edit this line
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
to be
fragmentTransaction.replace(R.id.content_frame,
new AlarmListFragment());
You can move the drawer layout below your Fragment container in your XML. this will render the DrawerLayout in the forward view of the app. just be sure not to cover the fragment container with the parent container of the drawer so that your fragment will be accessible when you need it.
<RelativeLayout
android:id="#+id/my_parent_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="#+id/fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/my_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id=#+id/drawer_pane"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
<ListView xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="#+id/menu_list"
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
So i'm wanting to be able to place an logo image at the bottom of my drawer view separate from the list. I've seen people do similar things with other list view and things like that, but whenever I try implementing that i get a lot of java errors. any help with this would be greatly appreciated.
here's my layout 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">
<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="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#f4f8f9" />
</android.support.v4.widget.DrawerLayout>
I've seen in a few places to change the frame layout to a relative layout, but every time i do that i get an error.
here's my java code:
public class someActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mDrawerItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerItems = getResources().getStringArray(R.array.drawer_items);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ShelfArrayAdapter(this, mDrawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setSubtitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setSubtitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
/* Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mDrawerItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
// this changes fragments.
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Include a RelativeLayout inside your FrameLayout (child). Then you can put your logo at the bottom ! Hope that works!
Edit
Sorry, I thought it would be that simple, but I got it working !
Try this:
<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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<RelativeLayout
android:id="#+id/ratafeia"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start" >
<ListView
android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="240dip"
android:layout_alignParentBottom="true"
android:src="#drawable/rata" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
And the Java code:
Add at the global variables:
RelativeLayout ratafeia;
Before mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);:
ratafeia = (RelativeLayout) findViewById(R.id.ratafeia);
Replace: mDrawerLayout.isDrawerOpen(mDrawerList); with mDrawerLayout.isDrawerOpen(ratafeia);
mDrawerLayout.closeDrawer(mDrawerList)
with mDrawerLayout.closeDrawer(ratafeia);
Bottom line, replace every mDrawerList with your relativeLayout !
I've tested and it is working here !
Just giving you an idea, Try the below logic (Use a wrapper for ListView) , it should work
<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" />
<RelativeLayout
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#f4f8f9" />
<ImageView
// Load your image here
with align parent bottom
/>
</RelativeLayout>
See the layout I created myself to achieve a similar thing, here..
Also, at the time of closing the drawer call drawer.close(Gravity.START), don't pass listView object here, pass the relative layout instead.
On the image, a drawer layout is inside a ViewPager + Tab. I want to open the Drawer Menu but the when I click the Home button, does nothing. I just wanted to display the menu on this scenario.
On some point, when I swipe to the right the drawer to be opened, but instead the viewPager got triggered and transferred to the next page.
Could I possibly put a border like drawable instead, in the drawer menu so that by the time I click the border or drag it it will open the drawer? The blue line is just an added edit but not in the actual scenario.
switch(arg0){
/** Android tab is selected */
case 0:
DrawerLayoutFragment androidFragment = new DrawerLayoutFragment();
data.putInt("current_page", arg0+1);
androidFragment.setArguments(data);
return androidFragment;
DrawerLayoutFragment
public class DrawerLayoutFragment extends Fragment implements SimpleGestureListener{
private SimpleGestureFilter detector;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
detector = new SimpleGestureFilter(getActivity(),this);
mTitle = mDrawerTitle = getActivity().getTitle();
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.drawer_layout_fragment, container, false);
mPlanetTitles = rootView.getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) rootView.findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//enable ActionBar app icon to behave as action to toggle nav drawer
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActivity().getActionBar().setTitle(mTitle);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
Log.d("onDrawerClosed", "inside");
}
public void onDrawerOpened(View drawerView) {
getActivity().getActionBar().setTitle(mDrawerTitle);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
return rootView;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return getActivity().onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/u p action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
default:
Log.d("onOptionsItemSelected", "inside");
return super.onOptionsItemSelected(item);
}
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
Log.d("DrawerItemClickListener", "inside");
}
}
private void selectItem(int position) {
Log.d("selectItem", "inside");
// update the main content by replacing fragments
PlanetFragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setTitle(CharSequence title) {
mTitle = title;
getActivity().getActionBar().setTitle(mTitle);
}
public static class PlanetFragment extends SherlockFragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
//Simple Gesture
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return getActivity().dispatchTouchEvent(me);
}
#Override
public void onSwipe(int direction) {
String str = "";
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
mDrawerLayout.openDrawer(mDrawerList);
break;
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
mDrawerLayout.closeDrawer(mDrawerList);
break;
case SimpleGestureFilter.SWIPE_DOWN : str = "Swipe Down";
break;
case SimpleGestureFilter.SWIPE_UP : str = "Swipe Up";
break;
}
Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
}
#Override
public void onDoubleTap() {
Toast.makeText(getActivity(), "Double Tap", Toast.LENGTH_SHORT).show();
}
}
drawer_layout_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<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>
Sorry sir, iDroid for not updating this quick.
Update:
I also faced the same problem and then i tired SlidingLayer.
You can give it a try as well, that's exactly what you need.
Enjoy Coding...
=====================================================
It seems like you want to add another view in the DrawerLayout. If it is then you can do it like below:
Here is my code of xml layout having drawerlayout:
<?xml version="1.0" encoding="UTF-8"?>
<!--
A DrawerLayout is intended to
be used as the top-level content view using match_parent for both width and
height to consume the full space available.
-->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedResources"
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" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat this as
a sliding drawer on the left side for left-to-right languages and on the
right side for right-to-left languages. The drawer is given a fixed width
in dp and extends the full height of the container. A solid background is
used for contrast with the content view.
-->
<LinearLayout android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="horizontal">
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#drawable/list_background_gradient"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:background="#drawable/search_box"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:paddingLeft="13dp"
android:paddingRight="13dp"
android:layout_weight="1"
android:singleLine="true"
android:hint="SEARCH USER"
android:maxLength="30"
android:textColor="#FFFFFF"
android:background="#android:color/transparent"
android:textSize="15sp" />
<ImageView android:layout_height="20dp"
android:layout_width="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:src="#drawable/category_search"
/>
</LinearLayout>
<TextView android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="SELECT A LINE"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:gravity="center_horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginBottom="2dp"
android:background="#43BBED"
/>
<ListView
android:id="#+id/list_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_margin="2dp"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:scrollbars="none" />
</LinearLayout>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/ic_launcher"
android:scrollX="20dp"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
And it looks something like this: