How to get Button fields on google map - android

I am working on an application in which i am getting a map in full screen and also action bar on the top. In action bar i have also included Navigation drawer. Everything is working fine but the Button is not getting displayed onto the MapView.
If I remove the navigation drawer functionality, the buttons are getting displayed. But my requirement is like i should be able to display the button on the bar as well as the Navigation bar.
The snippets are:
<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:context="com.mobile.android.ui.MapActivity"
tools:ignore="MergeRootFrame" >
<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">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:layout_gravity="left|top" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.mobile.android.ui.NavigationDrawerFragment" />
</android.support.v4.widget.DrawerLayout>
</FrameLayout>

My project supports similar features.. But I'm using the ViewPager.
So my location_framgment.xml (see down below) is able show the map and I will display it by the ViewPager's default behaviour.
The xml file looks as follow:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
And here my LocationFragment, which extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (sView != null) {
ViewGroup parent = (ViewGroup) sView.getParent();
if (parent != null) {
parent.removeView(sView);
}
}
try {
sView = inflater.inflate(R.layout.location_fragment, container, false);
sMap = ((MapFragment) MainActivity.sFragmentManager.findFragmentById(R.id.location_map)).getMap();
} catch (InflateException e) {
Log.d(TAG, "InflateExc.: "+e);
}
return sView;
}

Related

Custom NavHostFragment replace framelayout with viewgorup

There are some views and one FrameLayout in the layout. I want to custom NavHostFragment replace FrameLayout with RelativeLayout, it works but the views hierarchy is wrong. The ImageView should be top, but it is in the bottom.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
and my custom NavHostFragment:
public abstract class BaseModuleFragment extends NavHostFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = null;
if (getContentViewId() > 0) {
view = inflater.inflate(getContentViewId(), container, false);
View frameLayout = view.findViewById(R.id.lb_nav_host_container);
if (!(frameLayout instanceof FrameLayout)) {
throw new RuntimeException("frame layout must exist");
}
frameLayout.setId(getId());
} else {
view = super.onCreateView(inflater, container, savedInstanceState);
}
return view;
}
}
Was the method wrong, or how to solve?
Change your layout to this one. It should be ok
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_below="#+id/ivTop"
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
These pictures show:
Wrong:
FrameLayout cover ImageView, it is wrong
Right
ImageView should be top, this is right
FrameLayout top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ImageView top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>

Google's BottomSheet appears at top of screen

I'm using android's new BottomSheet inside Design Library.
Problem is that I'm using it inside a Fragment and It cause that it appear at top of screen instead of appearing at bottom.
This is my Activity xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="ir.aidinsoft.quicktaxi.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/tlbr_acMain"
android:elevation="5dp"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.CoordinatorLayout
android:id="#+id/crdl_acMain"
android:layout_below="#+id/tlbr_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nscv_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This Layout Is Replacement Layout For Fragment -->
<RelativeLayout
android:id="#+id/frml_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
And This Is My Fragment xml:
<android.support.design.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="ir.aidinsoft.quicktaxi.MyFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.codetail.widget.RevealFrameLayout
android:id="#+id/rvfl_frTaxi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<fragment
android:id="#+id/frag_frTaxi_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</io.codetail.widget.RevealFrameLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/facb_frTaxi_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- My Bottom Sheet Layout -->
<FrameLayout
android:id="#+id/frml_frTaxi_bottomSheet"
android:layout_width="match_parent"
android:layout_height="360dp"
android:background="#color/colorPrimaryLight"
app:behavior_hideable="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />
</android.support.design.widget.CoordinatorLayout>
So after Replacing Fragment in Layout with this code:
getSupportFragmentManager().beginTransaction().replace(R.id.frml_acMain, taxiFragment).commit();
I adde this code to reveal BottomSheet on FAB clicked state.
Java Code for Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
fab = (FloatingActionButton) view.findViewById(R.id.facb_frTaxi_request);
bottomSheet = view.findViewById(R.id.frml_frTaxi_bottomSheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
return view;
}
#Override
public void onResume() {
super.onResume();
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
}
What is the problem? what I'm doing wrong?
TnQ
If you are using animateLayoutChanges inside your layout, removing it can fix your problem.

Navigating through a fragment

I have a fragment inside my main activity. I want to update the contents of the fragment dynamically a run time. Specifically, I want to populate the contents of fragment with new data on button back press. Please help me out in the same and suggest me how to achieve such a functionality.
//main_activity.xml
<?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:background="#android:color/white"
android:orientation="vertical"
android:id="#+id/container">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<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">
<FrameLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_recyclerview"
android:name="com.example.RecyclerViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout="#layout/fragment_recyclerview" />
</FrameLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.example.NavigationDrawerFragment"
android:layout_width="#dimen/drawer_dimen"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
The first fragment is the one that needs to be updated
#Override
public void onBackPressed() {
//Find if fragment by tag
Fragment fr = fragmentManager.findFragmentByTag("home");
if (fr.isVisible()) {
//do ur stuff
} else {
fragmentManager.beginTransaction().show(fr).commit();
}
}
Ok! I will give you an example of what I did in one of my projects...
My main activity xml contained a simple linear layout among other things:
<LinearLayout
android:id="#+id/layout_images_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:orientation="horizontal" >
</LinearLayout>
In my activity OnCreate, I added the following:
FragmentImages fragmentImages = new FragmentImages();
getFragmentManager().beginTransaction().add(R.id.layout_images_fragment, fragmentImages).commit();
where FragmentImages is a class that extends Fragment
public class FragmentImages extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_images, container, false);
//and here is where I wrote my codes for this fragment
return v;
}
}
Hope this will help !!

Issue with appcompat v21 Material Design: Fragment overlay content and toolbar

I followed http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html to use the toolbar, without fragment, it works perfectly, but when I use fragment, the fragment always overlays the content, and I cannot see toolbar when displaying fragment.
screen:
and when I starts another activity from the current one, the icon of the toolbar in the new activity doesn't show up. screen:
I use Android Support Library 21.0.3.
and the code in the MainActivity class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_list_item_1, data);
mLeftDrawerList.setAdapter(mNavigationDrawerAdapter);
if (mToolbar != null) {
mToolbar.setTitle("Home");
setSupportActionBar(mToolbar);
}
initDrawer();
final FragmentManager fragementMgr = getSupportFragmentManager();
FragmentTransaction transaction = fragementMgr.beginTransaction();
transaction.add(R.id.fragment_container, new MyFragment());
transaction.commit();
}
the layout xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
code in MyFragment:
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(getActivity(), DetailActivity.class);
startActivity(intent);
}
});
// Inflate the layout for this fragment
return view;
}
}
layout xml for Fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_fragment"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="top"
android:textSize="18sp"
android:id="#+id/text"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/text"
android:text="#string/open"
android:id="#+id/button"/>
</RelativeLayout>
The code in DetailActivity class:
public class DetailActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Detail");
setSupportActionBar(toolbar);
}
}
layout for the detail activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".DetailActivity">
<include layout="#layout/toolbar" />
<TextView
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Detail Activity Content"
android:layout_height="wrap_content" />
</LinearLayout>
can anyone help me out? thanks a lot!
Update: I updated the code and layout of the main activity according to reply from m iav.
new screen is blank now with the changes:
toolbar is a view in your layout , you can see it like textview ,now it is a textview overlay your fragment ,you use attrbibu to below it
so finally after checking the sample: MaterialEverywhere, I came with the solution with updating the layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_below="#id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
the trick is the parent layout of the toolbar and the content frame. Instead of LinearLayout, I use RelativeLayout. and it works now as expected.
anyway, I found such change from the SDK is over complex, with so many changes, and each can cause issues. It will be much better for the developers to have clean APIs with less all these tweaks.
This should be your main layout.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
And this should be your call to the new fragment:
transaction.add(R.id.fragment_container, new MyFragment())
transaction.commit();
Regarding your second question, a navigation drawer can't be shared among different activities. You have to use a new Fragment and keep the same Activity you're working in now.

DrawerLayout with input widgets

I work on my android application and I would like to create a view with sliding sub-view. The sub-view will contains some widgets such as EditText. Here is my layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#0000ff">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
I can't switch between EditText widgets. When I click some widget the sliding layout begins to collapse. I thought that is the cause my problem, but I use the following code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
DrawerLayout drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
return view;
}
Left side layout is locked but I still cannot switch between widgets. I tried to change clickable and focusable attribute too.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
DrawerLayout drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
drawerLayout.setClickable(true);
drawerLayout.setFocusable(true);
return view;
}
Any idea?
I can't switch between EditText widgets. When I click some widget the
sliding layout begins to collapse.
because your widgets is on the main layout and those are not on the sliding menu. so when you click on them it means you click outside of your drawerlayout, your layout is incorrect 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">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</FrameLayout>
<LinearLayout android:layout_width="250dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#0000ff">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Categories

Resources