I have an Activity container for several different fragments. All fragments should have the same margin except one. This should has no margin. But how can I do that?
This is my activity_container.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" />
All fragments within this container have a margin of 15dp. But there is one fragment, that should have 0 dp. The fragment have to be in the container. So creating a container only for this fragment is no option. Do somebody has an idea to solve that problem?
The fragments cannot be displayed outside of their container, just as with any other view.
Your only option is to remove the margin from your relative layout and add it back as padding onto the individual fragment layouts
Related
I have an activity with a bunch of elements already on it, and I want to open a fragment over the activity.
I want to use a fragment instead of an activity so I can pass a few objects that can't be serializable or parselable.
So far I manage to do this by doing something like this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<RelativeLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
.
.
.
In my activity I just change my fragment_container to VISIBLE, and I do a fragment transaction to add the fragment there, while also setting main_container that has all the views of my activity to GONE.
I know this is not a very nice solution, I've been thinking of moving all views inside of my main_container to a new fragment, so I can replace the container for one or another.
What would be the best practice to open a fragment on top of an activity?
A general rule of thumb that i use:
If you use fragments, then your activities should be extremely dumb.
Meaning that they should not contain any views themselves, and only contain logic to keep track of your fragments (and perhaps logic that only Activities can manage).
I'd take all the views and logic that's currently in your Activity, and move it into a separate Fragment.
Then you could simply replace the "standard" fragment with the new one. 2 fragments, clean and simple.
I have a collapsing toolbar within an appbar layout. It contains a view pager as it's collapsing content and a pinned action toolbar. The activity also has a nested scroll view below the collapsing toolbar. The nested scroll view contains a google map fragment.
On start of the activity, the collapsing toolbar collapses automatically in some of the cases without programmatically collapsing it.
I have tried a combination of different scroll flags for my view with scroll|exitUntilCollapsed|enterAlways.
I have also tried not showing the google map fragment but I can still reproduce it.
How can I stop it from collapsing automatically? Any help would be appreciated!
I know its late but still..
The reason collapsing toolbar automatically collapses on actvity start is because there may a view in <include layout="#layout/content_scrolling" /> like edittext which gain its focus when activity is started..
To overcome this issue you can add the tag
android:focusableInTouchMode="true"
to or
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
xmlns:android="http://schemas.android.com/apk/res/android">
Hope it helps..:)
Happy Coding..:)
On one button I called a layout like so:
setContentView(R.layout.my_form);
On the other I wanna destroy that layout. How can I do it ?
U can use rootView.removeAllViews(); to remove all views from layout, for example
Your layout is
<LinearLayout
android:id="#+id/llRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
.......
</LinearLayout>
Then you can call llRootView.removeAllViews(); to remove all views from this linear layout
EDIT
To Temporary remove views from layout you can change its visibility using
llRootView.setVisibility(View.GONE);
To make it visible again use
llRootView.setVisibility(View.VISIBLE);
This is the activity.xml and content_my.xml is a RelativeLayout and menu.xml is LinearLayout. Content layout(RelativeLayout)in the top and menu layout(LinearLayout) under the I want to move the Relative layout(content layout) left of screen and access the menu layout under it. content layout is in the top of menu layout after animation it menu cannot be accessed. In contentlayout I have add ListView. Please help me how to move the RelativeLayout left permanently after animation.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:context="com.bwd.demo.mybrand.My">
<include layout="#layout/menu" />
<include layout="#layout/content_my" />
</android.support.design.widget.CoordinatorLayout>
In your content_my.xml you have to assign an ID to your RelativeLayout :
android:id="#+id/rl_content"
Then you cast the xml in Java code in your Activity so you can use it :
RelativeLayout rl_content = (RelativeLayout) findViewById(R.id.rl_content);
Now there are multiple options to animate it.
Since you want to access something below the layout, I would recommend
using ViewPropertyAnimator, then you can do it with one line.
A minimal implementation would look like this :
rl_content.animate().translationX(0); // -> animates **TO** 0 on the X axis
would move the left border of the layout to the left side of the screen. If you want it further then you need a negative value.
or you can use the translationXBy() method f.e. :
rl_content.animate().translationXBy(-50); // -> animates **BY** -50 on the X axis
//(negative values are to the left)
Also interesting :
TranslateAnimation -> not recommended in your case, because it does not really move the layout, only makes it look like its moving, so you would have trouble accessing the menu under the RelativeLayout.
ObjectAnimator
I'm making an app for Android with Java in eclipse, and it will have multiple screens. One screen will contain a canvas with 2D graphics, and some buttons below the canvas. But I'm a newbie at Android so it's still a little messy to me. Seems like a new Paint() in a class which extends View always is fullscreen (?) so I tried to solve it with Fragments where I put the canvas/newPaint() in one Fragment and the buttons in another one. But my app crashes when I try to add the Fragments in the xml file, and I don't know why.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/game_fragment"
android:name="com.example.tictactoe.GameFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#FF0000"
tools:layout="#layout/game_view" />
<fragment
android:id="#+id/game_fragment2"
android:name="com.example.tictactoe.GameFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFF00"
tools:layout="#layout/game_view" />
Is there any other way of getting a 2D graphics canvas together with some buttons on a screen,
or can someone help me out with the fragment solution?
Thanks in advance!
Your stacktrace gives what the error is - GameFragment is not a class inheriting from Fragment. You need to ensure that you are placing Fragment classes appropriately in your layout.
Also from looking at your layout, you are placing GameFragment layout twice in the same root view. This looks wrong on some level. I mean if you have your buttons in a different layout, shouldn't you be using that layout? Also, as a tip, inflate the layout for the fragment by overriding the GamrFragment's onCreateView() method (you should see this method once you have inherited from the Fragment class appropriately).
And last but not the least, when using Fragments, ensure that you use the right version uniformly (if you use the fragments from the support package, stick to using it uniformly).