I want to create a navigation drawer that contains tabs in android, but I could not do anything. I want to add tabs (categories, favorites) to the navigation drawer.Is it posible?
(eg. ios)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer,
R.string.app_name, R.string.app_name ){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
// invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
//invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(getApplicationContext(), getSupportFragmentManager(), android.R.id.tabcontent );
tabHost.addTab(tabHost.newTabSpec("surec").setIndicator("Süreç listesi"), SurecListesiFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("bolum").setIndicator("Bölüm listesi"), PhotosFragment.class, null);
I cannot switch between tabs. And I cannot use any widgets on tab content. (listview, button, etc.. )
(EDIT : solution)
(linearlayout : slider menu layout)
linearLayout.bringToFront();
linearLayout.requestLayout();
I did it just yesterday.
This is my layout for main activity with drawer:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<!-- Main layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_background"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_location_spinners_campaigns" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_campaignslistview" />
</LinearLayout>
<!-- Slider menu -->
<LinearLayout
android:id="#+id/preferencesDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/app_background"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
The method to display the tabs:
FragmentTabHost tabhost;
void buildTabs() {
tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabhost.addTab(tabhost.newTabSpec("locations").setIndicator(getString(R.string.locations)), FragmentPreferencesLocations.class, null);
tabhost.addTab(tabhost.newTabSpec("categories").setIndicator(getString(R.string.categories)),FragmentPreferencesCategories.class, null);
}
Besides that you need code to display the drawer and customize your tabs...
EDIT:
I initialize drawer with this method (invoke it at onCreate):
ActionBarDrawerToggle drawerToggle;
LinearLayout preferencesDrawer;
FragmentTabHost tabhost;
...
void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name,
R.string.app_name) {
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
drawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.preferences));
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
// Invoked by action bar button
void clickFilterCategories() {
toggleDrawer(preferencesDrawer);
}
void toggleDrawer(View drawer) {
if (drawerLayout.isDrawerOpen(drawer)) {
closeDrawer(drawer);
} else {
openDrawer(drawer);
}
}
void openDrawer(View toOpen) {
drawerLayout.openDrawer(toOpen);
}
void closeDrawer(View toClose) {
drawerLayout.closeDrawer(toClose);
}
Related
I have been able to show the "hamburger" icon on my toolbar, but when I click on it, nothing is happening... The only way to bring my drawer is to slide from the left..
Here's my code:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrayerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrayerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, test));
mDrawerList.setOnItemClickListener(new DrawerListClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrayerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
getSupportActionBar().setTitle("Close");
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
public void onDrawerOpened(View drawerView){
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle("Open");
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
};
mDrawerToggle.syncState();
mDrayerLayout.setDrawerListener(mDrawerToggle);
}
Here's my xml file. I don't think it will help but who knows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/my_toolbar">
<!-- 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>
Thanks!
When using the four-parameter constructor for ActionBarDrawerToggle, you need to override the Activity's onOptionsItemSelected() method, and call the toggle's corresponding method to trigger the drawer opening and closing.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
return super.onOptionsItemSelected(item);
}
This is what my code looks like, my activity extends Activity. I am having trouble getting the Hamburger menu icon as i get back arrow menu icon. which i dont want.
code in mainactivity:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowCustomEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
menu_list = new ArrayAdapter<String>(this, R.layout.drawer_list,
getResources().getStringArray(R.array.menu_options));
mDrawerList.setAdapter(menu_list);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer ,R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
code in action_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#29ABE2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:textColor="#FFF"
android:textSize="25dp"
android:text="DataVision" />
</LinearLayout>
additional methods:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle
// If it returns true, then it has handled
// the nav drawer indicator touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
You need to wrap your existing LinearLayout in a DrawerLayout:
<?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">
then your LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#29ABE2" >
<TextView
android:id="#+id/textView1"
etc.
...
</LinearLayout>
then the 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>
see also: https://developer.android.com/training/implementing-navigation/nav-drawer.html
My action bar has navigation drawer and profile image icon.I want to decrease the space between navigation drawer icon and profile image icon but navigation drawer icon takes fixed space even if i replace it with small icon.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
ActionBar mActionBar = getSupportActionBar();
View mCustomView = mInflater.inflate(R.layout.actionbar_home, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
actionbar_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/tabbackground"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profileImageActionBar"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:visibility="visible"
app:border_width="1dp"
app:border_color="#FFFFFF"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
android:text="Home"
android:layout_toRightOf="#+id/profileImageActionBar"
android:layout_marginLeft="50dp"
android:textColor="#color/white"
android:textSize="22sp"/>
</RelativeLayout>
Could you add a screenshot of how it looks like for you?
I tried this using a simple ActionBar mActionBar = getActionBar() with a custom view like yours, and hiding icon like this mActionBar.setIcon(android.R.color.transparent), and the space between navigation drawer icon and custom view is quite ok.
I'm currently trying to implement a Navigation Drawer into my Application. For that i want the Drawer to be opened when the activity is started. This is my onCreate:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
isInLandscapeMode = getResources().getBoolean(R.bool.isInLandscape);
if (!isInLandscapeMode)
{
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.date, R.string.applicationName)
{
/**
* Called when a drawer has settled in a completely closed
* state.
*/
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
};
// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
drawerLayout.openDrawer(Gravity.START);
}
The Drawer is opening as it should but if i try to click one of my list entries it closes. If I open it manually afterwards everything is working perfectly. Do I have to set the focus on my drawerLayout somehow? I have tried several things but nothing seems to work.
UPDATE
Finally found my error:
I had the following code in my layout xml file:
<FrameLayout 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" >
<LinearLayout
android:id="#+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
<FrameLayout
android:id="#+id/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
The main content view(detail_container) must be the first child in the navigation drawer thought. So i changed it to this:
<FrameLayout 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/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
<LinearLayout
android:id="#+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
And it worked.
Use drawer.openDrawer(Gravity.LEFT);
After you find your drawer layout and Data(list or expandable list) then put
drawerLayout.openDrawer("Name of Data Contained By Drawer");
Android Studio 0.8.10
I am wondering is it possible to be a icon in the actionbar for the navigation draw when you are
using a Fragment to display the ActionBar. I know you can do this from the activity, but I want to do
it from my Fragment. I can display the navigation draw that comes out of the left. But I can't put the icon
in the actionbar.
Many thanks for any suggestions.
This is what I have for my navigation draw layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dlMenus"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ssd.fott.FragmentDraw">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundlist"
android:orientation="vertical">
<ImageView
android:id="#+id/ivDetailImage"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="4dp"
android:contentDescription="Picture of newsfeed"
android:scaleType="fitXY" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="30sp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:orientation="vertical"
android:gravity="top"
android:background="#drawable/background">
<ImageView
android:layout_width="150dp"
android:layout_height="100dp"
android:src="#drawable/logo_large"
android:scaleType="fitXY"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="#string/fott_feed"
android:textColor="#ffff"
android:layout_margin="8dp"
android:drawableLeft="#android:drawable/ic_dialog_map"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In my Fragment class I have the following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Log.d(TAG, "onCreateView()");
View rootView = inflater.inflate(R.layout.fragment_detailfott, container, false);
mTvDescription = (TextView)rootView.findViewById(R.id.tvDescription);
mIvDetailImage = (ImageView)rootView.findViewById(R.id.ivDetailImage);
mTvSubject = (TextView)rootView.findViewById(R.id.tvSubject);
Drawable drawable = new BitmapDrawable(getResources(), mNewsFeedDB.get(mId).getImage());
mIvDetailImage.setImageDrawable(drawable);
mTvDescription.setText(mNewsFeedDB.get(mId).getDescription());
mTvSubject.setText(mNewsFeedDB.get(mId).getSubject());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(),
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
return rootView;
}
Yes, it is. From inside your Fragment onCreateView() method, call:
getActivity().getActionBar().setIcon(R.drawable.yourDrawable);
Alternatively, you can give a Drawable object as parameter to the setIcon() method.
For the support version you can use getSupportActionBar() instead to access the Activity ActionBar.