1

I want to open a Fragment and want to inflate the view the fragment is located in. Is this possible? I searched these Questions:

  1. error inflating class fragment fragment did not create a view;
  2. calling fragment from activity;
  3. how to open specific fragment from other activity onclick;
  4. open fragment from activity;
  5. how do i prevent overlapping in android;

I couldn't find my answer or I have overlooked it. Is it possible that when my fragment is opened after the onclick that my layout pushes the button (beetInfosButton) below my fragment,(which is encapsulated in a scrollView) so my fragment is not overlapping? Do I have to use another layout instead of RelativeLayout? Or is this just not possible. Hopefully someone can understand what I want. Thanks in advance
This is the Activity code.

public class InfoSeite extends AppCompatActivity implements BodenSeite.OnFragmentInteractionListener {

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info_seite);

public void buttonBodenInfos(View view){
    getFragmentManager().beginTransaction().add(R.id.fragment_container,new BodenSeite()).commit();
    }

And the Activity XML-File

<?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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#2fb215"
        android:id="@+id/infoSeite">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bodenInfosString"
            android:id="@+id/bodenInfosButton"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="79dp"
            android:onClick="buttonBodenInfos"/>


            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/scrollView2"
                android:layout_toEndOf="@+id/bodenInfosButton"
                android:layout_below="@+id/bodenInfosButton"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" >

            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_height="wrap_content"
                android:layout_width="match_parent" />
            </ScrollView>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/beetInfosString"
            android:id="@+id/beetInfosButton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:onClick="buttonBeetInfos" />

    </RelativeLayout>

And a sample of the Fragment XML.

<GridLayout 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:background="#2fb215"
    android:columnOrderPreserved="true"
    android:tag="BodenFragment"
    android:id="@+id/bodenFragment">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bodenArtenString"
        android:id="@+id/bodenSeiteUeberschrift"
        android:layout_row="0"
        android:layout_column="0"
        android:textSize="40dp"
        android:textAlignment="center" />
Community
  • 1
  • 1
Adije
  • 25
  • 5
  • If I understood your problem correctly: Since your `ScrollView` is defined to be below your `Button`, it will always be on top of the `ScrollView` (and thus your `FrameLayout`). You can change your `activity_main.xml` in a way that you `Button` will "disappear" , or you can handle the `Visibility` of your `Button` yourself, so in the `buttonBodenInfos` method, call `view.setVisibility(View.GONE)`. You need to set this to `View.VISIBLE` whenever you close your `Fragment`. – yennsarah Jul 05 '16 at 08:55
  • Thanks. But is there not another way? By shifting the buttons further below or something? Do I have to make them invisible? I will try this, if there is no other possiblity. – Adije Jul 05 '16 at 09:14
  • I did not read your question correctly. ^^' I'll add an answer. – yennsarah Jul 05 '16 at 09:16

2 Answers2

2

Since you are using a RelativeLayout, you have the option to "stick" Views relative to each other - or their parent.

Pseudo code:

<RelativeLayout>
    <Button />

    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

    <Button />

<RelativeLayout>

And in you onClick:

scrollView.setVisibility(View.GONE);  //View.VISIBLE, when you close the fragment
yennsarah
  • 5,467
  • 2
  • 27
  • 48
  • Ah, yes. I got this answer above with just the code and I already tried these options with Layout_above and below. But this did not have the effect I had in mind. The Layouts or buttons still remain in the same place. – Adije Jul 05 '16 at 09:26
  • I assume you are german - maybe explain your problem here in a comment in german, so I might understand you ;) – yennsarah Jul 05 '16 at 09:30
  • :D Ich möchte, dass nach dem Drücken des Buttons buttonBodenInfos(), welches das Fragment öffnet, sich die Buttons die sich in der View derzeit unter dem buttonBodenInfos() befinden, Platz machen für das sich öffnende Fragment. – Adije Jul 05 '16 at 09:34
  • For everyone else: The `Buttons` should disappear when the fragment opens itself. For @Adije: As far as I know, it would be a better practice to use two `Fragments` - one with the `Buttons`, one with the `ScrollView`. If you want to keep your layouts, you can try to set `width` and ´height` to `match_parent` (of your `ScrollView`) , and its `Visibility` from `View.GONE` / `View.VISIBLE`. – yennsarah Jul 05 '16 at 09:41
  • :D I think I have problems to correctly state my problem or my desired effect. I do not want the **Buttons** to disappear. I want them to still be shown but if my **Fragment** is opened they have to be further below, so that they do not overlap. But I think this is not possible because all my answers come back to hide other layouts/buttons. I will state this answer as a correct one, because I will go with the solution to hide my **Layouts** or **Buttons** or use two **Fragments**. Depending on which will suit me more. Thanks – Adije Jul 05 '16 at 09:51
  • Yes. Maybe an image/screenshot would have helped ;) – yennsarah Jul 05 '16 at 09:52
0

Try below code:

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#2fb215"
    android:id="@+id/infoSeite">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bodenInfosString"
        android:id="@+id/bodenInfosButton"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="79dp"
        android:onClick="buttonBodenInfos"/>


        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView2"
            android:layout_toEndOf="@+id/bodenInfosButton"
            android:layout_below="@+id/bodenInfosButton"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" >

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />
        </ScrollView>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/beetInfosString"
        android:id="@+id/beetInfosButton"
        android:layout_below="@+id/scrollView2"
        android:onClick="buttonBeetInfos" />

</RelativeLayout>
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
  • Thanks for your input. I tried your code and it did not work. But I learned the alignment with layout_above and layout_below through try and error. But this does not help with my problem. – Adije Jul 05 '16 at 09:11