0

I am trying to create a sort of a transparent tutorial which appears only the first time. This is the fragment i have created. How do I add this on top of an existing layout

here's the code for the fragment

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import in.webblue.nuclity.Activity.Logs.SaveLog;
import in.webblue.nuclity.R;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Akshay on 15-06-2017.
 */

public class TutorialFragment extends Fragment {
    private String Class_Name = "TutorialFragment";
    private  boolean  ranBefore;
    View topLevelLayout1;
    View topLevelLayout2;
    View myView;
    String methodName = "onCreateView";

    public static TutorialFragment newInstance() {
        TutorialFragment f = new TutorialFragment();
        return f;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = inflater.inflate(R.layout.tutorial_layout, container, 
   false);
        topLevelLayout1=myView.findViewById(R.id.tutorial1);
        topLevelLayout2=myView.findViewById(R.id.tutorial2);
        if (!isFirstTime()) {
            topLevelLayout1.setVisibility(View.INVISIBLE);
            topLevelLayout2.setVisibility(View.INVISIBLE);

        }
   return  myView;
}

    private boolean isFirstTime()
    {
        try {
            SharedPreferences preferences = 
         this.getActivity().getSharedPreferences("RanBefore", MODE_PRIVATE);
            boolean ranBefore = preferences.getBoolean("RanBefore", false);
            if (!ranBefore) {

                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("RanBefore", true);
                editor.commit();
                topLevelLayout1.setVisibility(View.VISIBLE);
                topLevelLayout2.setVisibility(View.INVISIBLE);
                topLevelLayout1.setOnTouchListener(new 
                View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        topLevelLayout1.setVisibility(View.INVISIBLE);
                        topLevelLayout2.setVisibility(View.VISIBLE);
                        return false;
                    }

                });
                topLevelLayout2.setOnTouchListener(new 
           View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        topLevelLayout2.setVisibility(View.INVISIBLE);
                        return false;
                    }

                });


            }
        }
        catch (Exception e){
            Log.e(getClass().getName(),"Method Name :"+methodName+ " "+ e.getStackTrace().toString());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

      SaveLog.saveLog(getContext(),Class_Name,methodName,e.toString());
            }
        }
        return ranBefore;

    }

}

I need to add this on top of an existing layout

2 Answers2

0

I think FrameLayout is the way to go here.

The secret of FrameLayout is how it layouts its children. Although normally designed to contain one item, it will happily stack up other element on top of each other. Thus FrameLayout is essentially a way to manipulate the Z-order of views on the screen

Here a thread about what a FrameLayout can do:
what does FrameLayout do?

So your Layout would look something like this:

<FrameLayout>
   <Fragment/>
   <LinearLayout>
   // here is your normal layout
   </LinearLayout>
</>
0

You could do it the following way. This is your activity on top of which you need to add the fragment.

<?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:orientation="vertical">


    <LinearLayout
         android:id="@+id/example_fragment_parent"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

        //YOUR LAYOUT

    </LinearLayout>

    <LinearLayout
         android:id="@+id/example_fragment_parent"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:onClick="EndFragment">

        /*THE FRAGMENT YOU WANT TO SHOW. THE LOGIC TO SHOW THIS FRAGMENT 
          ONLY ONCE WILL HAVE TO BE IN THE ACTIVITY ON TOP OF WHICH YOU ARE 
          SHOWING THIS FRAGMENT*/

        <fragment
            android:id="@+id/example_fragment"
            android:name="com.example.ExampleFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

</RelativeLayout>

Now what you should do is show this fragment only once. The onClick method to hide this fragment is as follows:

public void EndFragment(View view) {
    example_fragment_parent.setVisibility(View.GONE);
}

You need to find this fragment in the onCreate() of your activity like below:

LinearLayout example_fragment_parent =
(LinearLayout)findViewById(R.id.example_fragment_parent);