Add Navigation Drawer to Exsisting Andrdoid Google Map - android

I'm developing Android Google Map project.
Here is what i wanted.
Default screen is like this and when drawer page must like this
I already did the google map part and I created a new Navigation Drawer Activity. How Am I suppose to connect these 2? I mean, how can I set the screen under the navigation drawer to my map page?
Already tried,
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, new Fragment1());
tx.commit();
}
Did not work.
Please help. This may be a silly question. but, I'm new to Andrdoid.
Thanks
-Edit -
Here is the activity_maps.xml
<fragment 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:id="#+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
Here is the activity_navigation.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity_maps"/>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer"/>
Here is the drawer_header.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="192dp"
android:background="?attr/colorPrimaryDark"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Abc"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
Here is the drawer.xml (menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
</group>
<item android:title="Sub items">
<menu>
</menu>
</item>
</menu>
I can not find what happened. But, I'm still getting the same error with this code.

Follow these steps.
Right click on your Android Studio project
Select New-> Activity -> Navigation Drawer Activity
Give a name and create the Navigation Drawer activity
open activity_drawer.xml
Add following code <include layout="#layout/activity_maps"/>
You'll get your answer
Hope this will help. :)

Related

Android. Toolbar menu doesn't show items

Toolbar menu doesn't show items and I can't understand why. When I start VDA-emulator it's shows menu icon, but it's blank inside. Can't add screenshot, coz Stackoverflow has some promblems with servers, as it say.
Menu screen in VDA-emulator
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:menu="#menu/menu_toolbar"
app:navigationIcon="#drawable/ic_arrow_back"
app:title="#string/toolbar_name" />
</LinearLayout>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="#string/toolbar_menu_1"/>
<item android:title="#string/toolbar_menu_2"
android:icon="#drawable/ic_comment"/>
</menu>
Current appcompact version 1.5.1

navigation drawer is always inflated when the app starts

I am trying to add navigation drawer to my main activity.
in design view of activity_main.xml it should be visible at left side of the activity layout as a shadowed animation(i don't know what terminology i should use here for that :| ),which on drag/swipe to right should be visible.
but in my case it is covering up the whole activity by default and is not displaying the actual contents of the activity ...which it ought to be...
i have gone through the solutionhere,but it is not working for me.
my layout code for activity_main.xml is as following:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mts3_.p1_d3_app_bar.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/tv"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/drawer_menu"
/>
</android.support.v4.widget.DrawerLayout>
and drawer_menu.xml(layout file) is as following:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:title="#string/img1"
android:id="#+id/img11"
android:icon="#drawable/pic_1"></item>
<item android:title="#string/img2"
android:id="#+id/img22"
android:icon="#drawable/pic_2"></item>
<item android:title="#string/img3"
android:id="#+id/img33"
android:icon="#drawable/pic_3"></item>
<item android:title="#string/img4"
android:id="#+id/img44"
android:icon="#drawable/pic_4"></item>
</group>
<item android:title="#string/social">
<menu>
<item android:title="#string/add_to_group"
android:id="#+id/add_to_group"
android:icon="#drawable/pic_5"></item>
<item android:title="#string/share"
android:id="#+id/share"
android:icon="#drawable/pic_6"></item>
<item android:title="#string/group"
android:id="#+id/group"
android:icon="#drawable/pic_7"></item>
</menu>
</item>
</menu>
First,change your height and width android:layout_width="wrap_content" and android:layout_height="match_parent",
Second,set gravity android:layout_gravity="left" in your NavigationView.
Try this .
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
app:menu="#menu/drawer_menu"></android.support.design.widget.NavigationView>
Edit
If you use android:layout_gravity="left" in your xml code , you will start NavigationView on the left .
And android:layout_gravity="left" is the same of `android:layout_gravity="start``.
If you use android:layout_gravity="right" in your xml code , you will start NavigationView on the right.
And use tools:openDrawer="start" in your code ,it will display in the preview in the android studio .it did not effect your display in your device .
Put below Code in Drawerlayout tag
tools:openDrawer="start"
Like Following Code
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
After that implement that code in navigationView Tag
android:layout_gravity="start"
Hope it work fine.

How Can I trigger Google PlaceAutocompleteFragment from a menu item?

I am new to android and trying to create a list-detail app. I am trying to add the PlaceAutocompleteFragment for searching and adding places to my list.
Now I wanted to add the PlaceAutocompleteFragment to my app and trigger it in such a way that the search dialog gets invoked if I click a plus ("+") sign on my actionbar's menu item.
Instead I am only able to manage to add the PlaceAutocompleteFragment to my list activity's layout.
city_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<fragment
android:id="#+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<android.support.v7.widget.RecyclerView 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/city_list"
android:name="com.apoorv.android.weatherapp.CityListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.apoorv.android.weatherapp.CityListActivity"
tools:listitem="#layout/city_list_content" />
</LinearLayout>
enter code here
How it appears now
Whereas, I tried adding it to a Menu for my activity but it crashes the app with a NullPointer exception if I add the fragment to my menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add_city_button"
android:icon="#android:drawable/ic_input_add"
android:title="Add City"
app:showAsAction="ifRoom" />
<fragment
android:id="#+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</menu>
I want to invoke the autocomplete dialog on the click of the + sign of this menu.
Plus Sign

Android right to left NavigationDrawer menu items aren't RTL

I need to align navigation menu items to the right.
I read many articles and questions and answers but i couldn't find what's wrong in my project.
This is my 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"
android:fitsSystemWindows="true"
tools:openDrawer="right">
<include
layout="#layout/content_activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_activity_home"
app:menu="#menu/activity_home_drawer" />
This is activity_home_drawer.xml:
<?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/menu_account"
android:icon="#drawable/ic_profile"
android:title="حساب کاربری" />
<item
android:id="#+id/menu_logout"
android:icon="#drawable/ic_sign_out"
android:title="خروج از حساب" />
</group>
I can make drawer itself to open from right but elements of NavigationView still remain left to right. You can see the result here:
As you see menu items aren't right to left. How to make them rtl?
try adding android:layoutDirection="rtl"
<android.support.design.widget.NavigationView
android:layoutDirection="rtl"
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
This works for api level 17 or above. For older devices there is a trick.
In application level of android manifest set supportRtl="false" and in layouts set layout_gravity="right". This works properly.
First delete app:menu="#menu/activity_main_drawer" and in
"#layout/nav_header_main" layout you can set custom layout.in the below you can see content of nav_header_main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/banner"
android:gravity="right">
</LinearLayout>
</LinearLayout>

Navigation View: checkableBehavior="none" not working on latest version

Im using the latest Android Design Support Library and have two groups for my Navigation View, one for fragments where I use android:checkableBehavior="single" and one for Activities like Settings and About where I use android:checkableBehavior="none".
This worked fine up to version 23.0.1 of the library, now when I just use compile 'com.android.support:design:+' The buttons get checked when selecting.
Is this just a bug in the library or do I have to change something for the new version?
I can't find anything in the changelog.
menu:
<group
android:id="#+id/main"
android:checkableBehavior="single">
<item
android:id="#+id/navigation_fragment_1"
android:icon="#drawable/icon"
android:title="#string/fragment_1" />
</group>
<group
android:id="#+id/sub"
android:checkableBehavior="none">
<item
android:id="#+id/navigation_sub_settings"
android:title="#string/settings" />
</group>
</menu>
layout:
<android.support.v4.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">
<RelativeLayout
android:id="#+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
I had a menuItem.setChecked(true); in my code (which caused no problems with the older version) but I guess they changed something so now you can manually check the menuItems even tho they are set to android:checkableBehavior="none".

Categories

Resources