I want show a preview of my fragment inside FrameLayout.
In fact I want display it only in android studio, And I do not want to render it.
My solution:
In Xml I load home_fragment preview using include tag:
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/nav_view"
android:layout_below="#id/my_toolbar">
<include layout="#layout/home_fragment" />
</FrameLayout>
In onCreate method i remove all child view of FrameLayout:
FrameLayout frameLayout = findViewById(R.id.main_frame);
frameLayout.removeAllViews(); // remove static preview in activity_main.xml
Everything works right now.
But I think there's a better way.
You probably want the tools:showIn attribute.
So if you have your main_frame.xml that <include>s your home_fragment.xml, in home_fragment.xml just point to the container layout:
<FrameLayout
tools:showIn="#layout/main_frame"
android:id="#+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other stuff -->
</FrameLayout>
Hope that helps!
Related
It is very common to add a Fragment to an Activity.
Let's say I have the following Activity's layout and Fragment's layout.
activity.xml
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout />
<FrameLayout
android:id="#+id/content_from_activity" />
</android.support.design.widget.CoordinatorLayout>
fragment.xml
<LinearLayout
android:id="#+id/linear_layout_from_fragment" />
If I add the fragment to activity
// Java code in Activity. To add Fragment to Activity.
this.webViewFragment = WebViewFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content_from_activity, this.webViewFragment).commitAllowingStateLoss();
The resultant layout (I'm using Tools -> Layout Inspector in Android Studio) would look as following.
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout />
<FrameLayout
android:id="#+id/content_from_activity" >
<LinearLayout
android:id="#+id/linear_layout_from_fragment" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
I was wondering, is it possible to eliminate the redundant content_from_activity , and have the following outcome when adding Fragment to Activity?
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout />
<LinearLayout
android:id="#+id/linear_layout_from_fragment" />
</android.support.design.widget.CoordinatorLayout>
It seems that it is not possible to use <merge> for `Fragment.
Can you use the merge tag with fragments?
I was wondering, do you have any technique, to optimized the resultant layout when adding a Fragment to an Activity?
I have created a Google Maps activity called 'PechhulpActivity' and
i want to place a button over it. But if i want to edit the activity it gives me the error about fragment tags:
A <fragment> tag allows a layout file to dynamically include different
layouts at runtime. At layout editing time the specific layout to be
used is not known. You can choose which layout you would like previewed
while editing the layout.
Here is a screenshot of my layout folder structure, first i thought i was missing a file like content_pechhulp.xml. If so, how can i create the correct one? I want to customize the Google maps activity like the info window and i want a button inside of it etc. I hope you guys can help me out.
Edit: (activity_pechhulp.xml)
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doppler.stackingcoder.pechhulp.PechhulpActivity" />
Try using RelativeLayout as Parent and then add the child with fragment and another view like TextView or Button. It will placed above the map.
use a Relativelayout or FrameLayout as parrent for this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doppler.stackingcoder.pechhulp.PechhulpActivity"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_string_value"/>
</RelativeLayout>
Background
In my app I have a lot of fragments that share the same static overlay. Each fragment can be resized dynamically (i.e. its weight may change) so I want the size of my overlay to be in sync with it. Therefore it makes sense to either make them siblings or, as it occurred to me, put one inside another. The first approach works fine but it implies introducing one extra ViewGroup which seems redundant (or does it not?). The latter is the one I'm having problems with.
Problem
Consider these two layouts.
Container R.id.container is where I put my fragment in runtime with FragmentManager.
Fragment R.id.overlay is my overlay.
The difference is which one is the parent.
Layout A.
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Layout B.
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</fragment>
In both cases my overlay ends up below container (by z-axis). That is, container keeps overlapping it regardless of its role in hierarchy.
What are the rules for defining views's z-order in situations like this? Is it just the order of their initialization? I couldn't google it out.
Thanks.
From documentation:
When the system creates this activity layout, it instantiates each
fragment specified in the layout and calls the onCreateView() method
for each one, to retrieve each fragment's layout. The system inserts
the View returned by the fragment directly in place of the
element.
So I think you should use something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
My app consists of multiple activities, they all have a common layout in the sense that they are drawer layout, have a tool bar, but they have different main content. Specifically, their complete layouts are as follows
<?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">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/action_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
.......main content, this is different for each activity..........
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Since the shared components are defined at "parent level", I'm not sure how to structure these layouts so that the generic layout is only defined once. Is it possible to achieve that? Thanks.
Two options:
1. ViewStub
Define the main layout with a ViewStub where you will have different sub-layouts.
A ViewStub is an invisible, zero-sized View that can be used to lazily
inflate layout resources at runtime. When a ViewStub is made visible,
or when inflate() is invoked, the layout resource is inflated. The
ViewStub then replaces itself in its parent with the inflated View or
Views.
In each Activity you set the same layout and then inflate the specific sub-layout onto the ViewStub.
2. Fragments
If you want to avoid having massive quantities of code on a single Activity you might want to think about using Fragments.
Some other topics to guide you:
How to use View Stub in android
Loading Views On Demand
Yes it is possible by using Fragment. For this structure first you need to define a FrameLayout in your activity_main.xml . This framelayout will be replaced by the contents of the fragments over the time.
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.safallwa.zahan.oreader.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Then you can create your layouts as you want and classes as those extends Fragment . Set these new layouts as contentView of Fragment classes. Now these fragments can replace that FrameLayout of activity_main. Suppose we have a Framgent class name First_Fragment.Java then we can show that fragment by replacing activity_main framelayout by below code
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);//container_body is the id of the framelayout
fragmentTransaction.commit();
For full featured example with navigation drawer also we can follow below link
http://www.codedisect.com/?p=134
I have a Toolbar and some fragments in FrameLayout. However in such a case I am asked to make an ImageView in a fragment which its half will be on the Toolbar. I couldn't figure it out. I considered to cut the image and make it an Options menu item and rest of it will be on the top of the fragment, but I think this won't be very effective for different devices. Can you please help me?
I don't sure what exactly u need.
But if I get it. Here is something u can do.
U just need to make your fragment on whole screen and draw toolbar over fragment.
Here is the snippet. I don't tested it:
your_activity.xml
<?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">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Toolbar //name possibly incorrect(change to correct one)
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"/>
</RelativeLayout>
Then inside fragment layout:
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView //or any other container for your stuff
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/toolbar_height"
android:clipToPadding="false"/>
<ImageView //your imageView that will draw halfover toolbar
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/half_toolbar_height"/>
</FrameLayout>
The snippet above possibly won't work correctly. But anyway it can be start point to solve your problem. Good luck!