Adije
Adije

Reputation: 25

open fragment from activity and inflate the view

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" />

Upvotes: 1

Views: 429

Answers (2)

yennsarah
yennsarah

Reputation: 5517

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

Upvotes: 2

Dhruvi
Dhruvi

Reputation: 1981

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>

Upvotes: 0

Related Questions