Poonam
Poonam

Reputation: 31

Add Fragment overlay on activity's view

I am new bie in android.

Below is my senerio what i have and what i actually need.

I have one activity where i have declare one custom view which contains a set of button which click event display respective fragments.

This custom view will be appear on top of each fragment.

Now suppose on first button of custom view i am display fragment a fragment which display list-view.. on click on list-view it show another fragment i.e detail fragment

In detail fragment.. i have one button where i need to show an fragment overlay on main activity ... as on full screen.. how can i achive this?

Upvotes: 3

Views: 7729

Answers (3)

tiny sunlight
tiny sunlight

Reputation: 6251

like this :

<LinearLayout id = "@+id/container">
<ToolBar id ="@+id/toolbar>
</ToolBar>
<FrameLayout id ="@+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();

use container as the container of fragment;

Upvotes: 0

Artur Szymański
Artur Szymański

Reputation: 1648

In my opinion easiest way lead to this behavior is use Activity Overlay or DialogFragment. Example:

Activity Overlay


MainActivity.java

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.buttonShow);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

MainActivity.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"
    android:id="@+id/mainLayout"
    tools:context=".MainActivity"
    android:background="@android:color/holo_orange_dark">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Activity"
        android:textSize="30dp"
        android:layout_centerInParent="true"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="show"
        android:id="@+id/buttonShow"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

SecondActivity.java

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

SecondActivity.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"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_light">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second Activity"
            android:textSize="30dp"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
</RelativeLayout>

Styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

    <style name="TransparentFloatingActivity" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.arturszymanski.test" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:theme="@style/TransparentFloatingActivity"
            android:name=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>
</manifest>

DialogFragment


MyDialogFragment.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyDialogFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FullScreen Fragment"
        android:textSize="30dp"
        android:layout_centerInParent="true"/>
</RelativeLayout>

MyDialogFragment.java

public class MyDialogFragment extends DialogFragment
{
    private int margin;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        margin = 10;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_dialog, container, false);
    }

    @Override
    public void onResume() {
        int dialogHeight = MainActivity.displayMetrics.heightPixels - (margin * 2) - MainActivity.StatusBarHeight;
        int dialogWidth = MainActivity.displayMetrics.widthPixels - (margin * 2);
        getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
        super.onResume();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    public static DisplayMetrics displayMetrics;
    public static int StatusBarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        displayMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        StatusBarHeight = getStatusBarHeight();

        Button button = (Button) findViewById(R.id.buttonShow);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                MyDialogFragment dialogFragment = new MyDialogFragment();
                dialogFragment.show(getFragmentManager(), "Dialog");
            }
        });
    }
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

Other Files

Other files are the same as in example above.

Upvotes: 2

Sourabh Bans
Sourabh Bans

Reputation: 3134

If i follow your question than, You should use a RelativeLayout as overlay in main activity's layout. and set visibility according to your need.

layout_activity_main.xml

<RelativeLayout
   android:width="match_parent"
   android:height="match_parent">

<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here

</LinearLayout>

<RelativeLayout
   android:id="@+id/overlay"
   android:width="match_parent"
   android:height="match_parent">
</RelativeLayout>

</RelativeLayout>

Upvotes: 0

Related Questions