Milan Gajera
Milan Gajera

Reputation: 970

How can i disable fragment UI when another fragment is open over one in android?

Explanation: I have two fragment where i replace the fragment one over another.My fragment name is FourFragment and OneFragment. Both have it's own layout.

Requirement:

My requirement is i set the textView over the toolbar and i have navigationView in my MainActivity. While my app is run the default fragment is loaded e.g. OneFragment it's correct. Now, when i click on the toolbar textview it's open the new fragment e.g. FourFragment. The Fourfragment cover some part of the screen and rest of the part of fourfragment is transparent. so that i can visible the previous fragment UI transparently e.g. OneFragment.I have done all the things.

But the problem while fourfragment is loaded over the Onefragment.But the UI of the onefragment is clickable when the fourfragment is loaded.

I want to my onefragment is visible while fourfragment is loaded over the onefragment but the UI of the onefargment is not clickable when fourfragment is open.

activity_main.xml

<?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.millu.navidemo.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:text="TextView"
                android:gravity="center"
                android:clickable="true"/>
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

    </FrameLayout>
    <FrameLayout
        android:id="@+id/dialog_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    TextView textView;
    boolean flag=false;
    FrameLayout dialog_frame;
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if(savedInstanceState==null){
            MenuItem item=navigationView.getMenu().getItem(0);
            onNavigationItemSelected(item);
        }
        textView=(TextView)findViewById(R.id.textView);
        dialog_frame=(FrameLayout)findViewById(R.id.dialog_frame);

        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                FourFragment ff=null;
                if(!flag) {
                    Toast.makeText(MainActivity.this, "Clicked!!!!", Toast.LENGTH_SHORT).show();
                    ff= new FourFragment();
                    ft.replace(R.id.dialog_frame,ff);
                    ft.addToBackStack(null);
                    ft.commit();
                    dialog_frame.setVisibility(View.VISIBLE);
                    flag=true;
                }
                else{
                    Toast.makeText(MainActivity.this, "else", Toast.LENGTH_SHORT).show();
                    dialog_frame.setVisibility(View.GONE);
                    ft.remove(ff);
                    ft.commit();
                    OneFragment oneFragment=new OneFragment();
                    ft.replace(R.id.container,oneFragment);
                    ft.commit();
                    flag=false;
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        Fragment fragment=null;

        switch (item.getItemId()){
            case R.id.nav_camera:
                fragment=new OneFragment();
                break;
        }
        if(fragment!=null){
            FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container,fragment);
            ft.commit();
        }
        else
        {
            Toast.makeText(MainActivity.this, "Fragment is null!!", Toast.LENGTH_SHORT).show();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

OneFragment.java

public class OneFragment extends Fragment {

    View rootView;

    TwoWayView twoWayView;
    List<Days> days_list;
    String[] str_arr={"A","B","C","D","E","F","G","H"};
    int[] img_arr={R.drawable.ic_menu_send,R.drawable.ic_menu_gallery,R.drawable.ic_drawer,R.drawable.ic_menu_camera,
    R.drawable.ic_menu_manage,R.drawable.ic_menu_share,R.drawable.ic_menu_slideshow};

    Button button;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_one, container, false);

        twoWayView=(TwoWayView)rootView.findViewById(R.id.twoWayView);

        button=(Button)rootView.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Button clicked!!1", Toast.LENGTH_SHORT).show();
            }
        });
        days_list=new ArrayList<>();
        for(int i=0;i<str_arr.length-1;i++){
            Days d=new Days(img_arr[i],str_arr[i]);
            days_list.add(d);
        }
        DayAdapter adapter=new DayAdapter(getContext(),days_list);
        twoWayView.setAdapter(adapter);

        twoWayView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(), "One:"+days_list.get(i).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

FourFragment.java

public class FourFragment extends Fragment {

    View rootView;
    TwoWayView twoWayView;
    List<Days> days_list;
    String[] name={"Milan","Sagar","Mithun","Brijesh","Ravi","Vishal","Nikhil"};
    public FourFragment(){}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_dialog, container, false);

        twoWayView=(TwoWayView)rootView.findViewById(R.id.twoWayView);
        days_list=new ArrayList<>();
        for(int i=0;i<name.length;i++){
            Days days=new Days();
            days.setName(name[i]);
            days_list.add(days);
        }

        DayAdapter dayAdapter=new DayAdapter(getContext(),days_list);
        twoWayView.setAdapter(dayAdapter);

        twoWayView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(), "One:"+days_list.get(i).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

Note: I want to visible the onefragment transparent when fourfragment is visible after click on textview which set on the toolbar.

Please, help me to solve out this problem. Thanx in advance

Upvotes: 1

Views: 991

Answers (1)

Milan Gajera
Milan Gajera

Reputation: 970

Set FourFragment main layout clickable="true" so that once fourfragment is loaded its parent layout is clickable so the hidden fragment widget not click until this main layout is clickable

FourFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:clickable="true">

Upvotes: 1

Related Questions