how to create the click event in sidebar navigation - android

I have created the sidebar navigation in my app. Now I want to create a click event on that.
I have searched for it and I got the documentation but I'm unable to understand the same.
so please anyone can suggest a better source for it will be helpful. tutorial on youtube will be helpful.
My navigation_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_account"
android:icon="#mipmap/ic_person_black_24dp"
android:title="My Account"/>
<item android:id="#+id/nav_setting"
android:icon="#mipmap/ic_settings_black_24dp"
android:title="Settings"/>
<item android:id="#+id/nav_logout"
android:icon="#mipmap/ic_logout_black_24dp"
android:title="Logout"/>
</menu>
My activity_main.xml
<?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"
tools:context="com.example.lenovo.jdstudio.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/navigation_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Main Layout"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

Simply implement NavigationView.OnNavigationItemSelectedListener interface and then override the method onNavigationItemSelected(MenuItem menuItem) like the following:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// declaring the NavigationView
navigationView = (NavigationView) findViewById(R.id.navigationView);
// assigning the listener to the NavigationView
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.nav_account:
// do you click actions for the first selection
break;
case R.id.nav_setting:
// do you click actions for the second selection
break;
case R.id.nav_logout:
// do you click actions for the third selection
break;
}
return true;
}
}
And for sure don't forget to give id to the NavigationView in your xml-layout:
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</android.support.design.widget.NavigationView>

Related

drawer menu not doing anything on Click event

Iam Implementing navigation drawer in my application but its menu not working on click. I dont know whats wrong but on click event is not doing anything or toasting a message.Click anything from drawer menu dont do anything.Firstly other button were also not working but adding them in a seprate layout solved the issue.but drawer menu are still not working
Here is my xml :
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here in java Iam using it :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Solution for this is in the XML file bring navigation view below the include tag and it will work
Change this
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
To this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
You don't initialize actionBarDrawerToggle. You have to do it.
When you are using AndroidX try to use the methods related to it.
Add the host fragment when you want to implement the fragments in that particular activity:
xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="#+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
Now use the navigation graph where you need to implement the fragments you are about to use in the activity :
navigation_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="#id/profile">
<fragment
android:id="#+id/profile"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/features"
android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="#string/features"
tools:layout="#layout/fragment_features" />
<fragment
android:id="#+id/signout"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/signout"
tools:layout="#layout/fragment_signout" />
</navigation>
In your java class add the implement the Navigation component code :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
//Add this
NavController mNavController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
//add this
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);
NavigationUI.setupWithNavController(navigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout",
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
You can test your app now. let me know after you implement this.

DrawerLayout inside fragment.xml not showing on home icon click

Background
I'm trying to implement a Drawer Navigation inside my fragment. I would prefer to have it there rather than in my main activity because I'm trying to handle toolbars per fragment and having the Drawerlayout coupled would make it easier to setup.
Problem
When I click on the hamburger home button, no drawer layout is displayed. How do I resolve this issue while keeping the drawerlayout inside my fragment.xml file instead of moving it to activity?
Things I've Tried
Followed the Android Navigation Drawer Guide
Made sure my xml layout hierarchy matches the requirements
fragment.xml
<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_article_topics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/articles_swipe_to_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/articles"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/navigation_article_topic" />
</android.support.v4.widget.DrawerLayout>
activity.xml
<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=".ui.main.MainActivity">
<LinearLayout
android:id="#+id/main_content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Fragment.java
#BindView(R.id.drawer_article_topics)
DrawerLayout articleTopicDrawerLayout;
// Other code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_articles_refresh:
articleListingViewModel.startRefresh();
refresh();
return true;
case android.R.id.home:
articleTopicDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
setSupportActionBar(toolbar);
setDrawerIcon();
setupRecyclerView();
setupSwipeRefreshLayout();
}
private void setDrawerIcon() {
if (isAdded()) {
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
}
}
The issue seemed to lie with DrawerLayout.openDrawer() and the Android Material Components library. Because I was inheriting Theme.MaterialComponents.Light.NoActionBar in my AppTheme, the Navigation Drawer did not open. Until they add a fix in, the workaround is to add this to your App Theme:
<item name="navigationViewStyle">#style/Widget.Design.NavigationView</item>
More information here:
https://github.com/material-components/material-components-android/issues/133

My Navigator Drawer is not triggering the Item Select Listener

I'm following the Developer Android Guide and already search and found questions like this, this and this.
I just wanna something that should be simple, open another activity (or make any action) when a item inside the menu of the navigation drawer is selected.
The menu open in the right behaviour using the FAB.
But when I click on my emulator it close but don't get inside the OnNavigationItemSelectedListener/onNavigationItemSelected, I debugged and the declaration is working, but when I click it won't get inside the listener.
I already tried to implements the listener in the activity and split the method, but didn't work.
Maybe something that can help, in the menu I hint my icons with red, but they still grey.
I already tried to clean, rebuild, open/close Android Studio all this basics that some times Android Studio don't handle.
XML Activity
<?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"
tools:context=".screens.intro.MainActivity"
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/main_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/main_drawer_view"
app:headerLayout="#layout/main_drawer_header" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/main_user_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:srcCompat="#drawable/ic_menu_black_24dp"
android:tint="#color/icon_red"
app:backgroundTint="#color/button_white"
app:fabSize="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//My layout/interface
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
XML Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/main_drawer_home"
android:icon="#drawable/ic_home_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/home_screen" />
<item
android:id="#+id/main_drawer_heal"
android:icon="#drawable/ic_healing_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/healing" />
<item
android:id="#+id/main_drawer_chance"
android:icon="#drawable/ic_warning_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/chance_of_accident" />
<item
android:id="#+id/main_drawer_avoid"
android:icon="#drawable/ic_play_circle_outline_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/avoid_accident" />
<item
android:id="#+id/main_drawer_chart"
android:icon="#drawable/ic_pie_chart_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/accident_char" />
<item
android:id="#+id/main_drawer_about"
android:icon="#drawable/ic_info_outline_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/about" />
<item
android:id="#+id/main_drawer_sign_out"
android:icon="#drawable/ic_logout_black_24dp"
android:iconTint="#color/icon_red"
android:title="#string/sign_out" />
</group>
</menu>
Code Java
public class MainActivity extends BaseActivity {
//In Layout
//navigator drawer
private DrawerLayout mainDrawerLayout;
private NavigationView mainNavigationView;
private FloatingActionButton mainUserMenu;
#Override
protected void assignViews() {
mainDrawerLayout = findViewById(R.id.main_drawer_layout);
mainNavigationView = findViewById(R.id.main_navigation_view);
mainUserMenu = findViewById(R.id.main_user_menu);
}
#Override
protected void prepareViews() {
mainUserMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Utils.isDoubleClick()) return;
mainDrawerLayout.openDrawer(GravityCompat.START);
}
});
mainNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mainDrawerLayout.closeDrawers();
//Do something
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
return true;
}
});
You need to change the position between NavigationView with FrameLayout.
Your xml activity needs to be like this.
<?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"
tools:context=".screens.intro.MainActivity"
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/main_user_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:srcCompat="#drawable/ic_menu_black_24dp"
android:tint="#color/icon_red"
app:backgroundTint="#color/button_white"
app:fabSize="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//My layout/interface
</android.support.constraint.ConstraintLayout>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/main_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/main_drawer_view"
app:headerLayout="#layout/main_drawer_header" />
</android.support.v4.widget.DrawerLayout>

menuInflater to create a menu in the onCreateOptionsMenu not working

I have used a menuInflater to create a menu in the onCreateOptionsMenu method of your activity. I have used this to display arrows on the toolbar for a calendar so that the user can go to previous or next month. But for some reason the arrows are not getting displayed
Pls can someone help.
MonthGridActivity:
private MonthGridFragment monthGridFragment;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calendar_grid, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
monthGridFragment.loadLastMonth();
return true;
case R.id.action_next:
monthGridFragment.loadNextMonth();
return true;
case R.id.all_events:
monthGridFragment.showAllEvents();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_grid);
monthGridFragment = new MonthGridFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_calendar_grid_container, monthGridFragment)
.commit();
}
menu_calendar_grid.xml
<item
android:id="#+id/action_previous"
android:title="#string/prev_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_previous" />
<item
android:id="#+id/action_next"
android:title="#string/next_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_next" />
<item
android:id="#+id/all_events"
android:title="#string/view_all_events"
android:orderInCategory="101"
app:showAsAction="never"
android:icon="#android:drawable/ic_menu_view" />
MonthGridFragment.java
public void loadNextMonth() {
calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
refresh();
}
public void loadLastMonth() {
calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
refresh();
}
activity_calendar_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame">
<android.support.v7.widget.Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_calendar_grid.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:orientation="vertical"
android:id="#+id/calendar_grid_layout">
<include layout="#layout/calendar_grid_header" />
<GridView
android:paddingTop="1dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/calendar_grid_view"
android:numColumns="7"
android:stretchMode="columnWidth"
android:background="#color/white_gray"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp" />
in onCreate method create a toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.myActivity_toolbar);
setActionBar(toolbar);
so in the layout add the view:
<Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
UPD:
You have to use either Toolbar from the support library or the standard one. If MonthGridActivity extends from Activity, use Toolbar (not android.support.v7.widget.Toolbar) in the layout. Otherwise MonthGridActivity must be extended from AppCompatActivity

list view not shown over main fragment having map

i am showing map in main content view which is a fragment. i want to show a listView over the main content view having map on a button click in an action bar or swipe the screen from left. i have tried alot but all in vain. my problem is how can i show the listview over the main content fragment such that the list view comes over the map.
upto now the listview appears when i swipe but it appears below the map in the fragment
i hav'nt add the functionality for clicking the button in actionbar. i am just swiping the list view from left. thanks in advance
my code is
DrawerClass.java
public class DrawerClass extends Activity {
DrawerLayout drawerLayout;
View drawerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawarclass_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerView = (View) findViewById(R.id.drawer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.actionbaricons_layout, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
drawerclass_layout.xml
<LinearLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/mapid"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/drawer"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_light"
android:orientation="vertical"
android:padding="5dp" >
<fragment
android:id="#+id/fragment1"
android:name="open.way.iscope.MyListFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
actionbaricons_layout.xml
<item
android:id="#+id/category"
android:gravity="center"
android:icon="#drawable/ic_action_select_all"
android:showAsAction="always"
android:title="#string/categories"/>
<item
android:id="#+id/previous"
android:icon="#drawable/ic_action_previous_item"
android:showAsAction="always"
android:title="#string/previous"/>
<item
android:id="#+id/next"
android:icon="#drawable/ic_action_next_item"
android:showAsAction="always"
android:title="#string/next"/>
<item
android:id="#+id/save"
android:icon="#drawable/ic_action_save"
android:showAsAction="always"
android:title="#string/save"/>
This is happening because the drawer element is not the root element in your drawerclass_layout.xml. Make Drawer element as your root element and the map fragment as the child in the layout file and you will be able to see your slider over the top of the main fragment that has map. Here is the code snippet.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- google map -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Hope this would help!!

Categories

Resources