0

I made a simple app with a bottom navigation bar. i want one of the button in the bar to display a fragment which has an alertdialog on it when it loads up with setCancelable(false) but i still can switch between other fragments. But when i use the alert dialog in the onCreate() of the fragment class, play the app and select the button with the alert dialog , the alertdialog covers the whole screen and i cant use the other buttons in the bottom navigation bar. So basically what i wanna do is display the alert dialog only in the frame layout (the container) but not on the whole screen so that i can still use the other buttons on the navigation bar while the alert dialog is still on the screen. lets suppouse that you have an app with two seperate fragment layout. i want the alert dialog to just display in only one layout.

activity_main.xml

    <LinearLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="abdul.com.myapp1.MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_dark">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.navigation_home:
                                selectedFragment = second.newInstance();
                                break;
                            case R.id.navigation_dashboard:
                                selectedFragment = Third.newInstance();
                                break;
                            case R.id.navigation_notifications:
                                selectedFragment = Fourth.newInstance();
                                break;
                            case R.id.navigation_fifth:
                                selectedFragment = Fifth.newInstance();
                                break;

                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, second.newInstance());
        transaction.commit();

        //Used to select an item programmatically
        //bottomNavigationView.getMenu().getItem(2).setChecked(true);
    }
}

Third.java (this is where i want to use the alertdialog)

    public class Third extends Fragment {
    public static Third newInstance() {
        Third fragment = new Third();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

 AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
                //LayoutInflater factory = LayoutInflater.from(v.getContext());
               View mView2 = getActivity().getLayoutInflater().inflate(popup_onpressed, null);
               mBuilder.setView(mView2);
               final AlertDialog dialog = mBuilder.create();
               dialog.show();
               dialog.setCancelable(false);
               dialog.setCanceledOnTouchOutside(false);

    }

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

I tried pasting it in the onCreateView() too but it didnt work either.

1 Answers1

0

first you shoud customize alartdialog then you can use this code to Show AlertDialog in any position of the screen

amin mahmoudi
  • 630
  • 7
  • 26
  • the position of the alert dialog doesn't matter. i just want the alert dialog not to cover the whole screen but only the area above the bottom navigation bar so that i can still access the other pages with the alert dialog on the screen – Javaid Hussain Aug 13 '17 at 10:48
  • i want to display the dialog just inside its parent which. – Javaid Hussain Aug 13 '17 at 10:49
  • i think alartdialog created to cover all views screen to get quick feedback then you can creat view xml looklike alart dialg white buttons and textview then you can Set the enabled state of this view by this cod:`View.setVisibility(View.GONE / View.VISIBLE / View.INVISIBLE)`. – amin mahmoudi Aug 15 '17 at 18:50