Open screen inside mainactivity

Asked

Viewed 386 times

1

I’m making a mechanical calculator, but I wanted to make several calculation options in the navigation drawer and when you click the specific calculator click on the screen (the intention is to make a activity for each type of calculation), I am using the code to open the activity of a calculation when clicking on the first option of navigation drawer

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.calcRPM) {
        startActivity(new Intent(getBaseContext(),calculorpm.class));
    } else if (id == R.id.nav_gallery) {

inserir a descrição da imagem aqui

But as you can see in the images it opens a new screen without the drawer and the menu up there. how could you make this upload as part of the app "main"

  • 1

    Search on Fragments, Voce will have only one activity and several Fragments that exchange the internal content of the activity. Your question is too wide to offer an answer code..

  • I’m gonna go check it out, thanks

  • I wouldn’t use a fragment because it limits the application too much. You can do a Basectivity, the other activities that need the navigation Drawer should extend it. At the beginning of a little work, but then everything becomes easier. Take a look at this example: http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/ I was having the same difficulty as you and that’s where I found the path.

1 answer

4


To do this, you should not start a new Activity when you click on an item in the Drawer navigation. What you should do is work with fragments in Mainactivity and replace this fragment when an item is selected instead of starting a new Activity.

In your activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:theme="@style/NavigationDrawerStyle"
    app:menu="@menu/drawer">

</android.support.design.widget.NavigationView>

In your Mainactivity

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {


            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            drawerLayout.closeDrawers();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {

                //Replacing the main content with right fragment;
                case R.id.start:
                    if (startfragment == null) {
                        startfragment = new StartFragment();
                    }
                    fragmentTransaction.replace(R.id.frame, startfragment);
                    fragmentTransaction.commit();
                    return true;

                case R.id.second:
                    if (secondfragment == null) {
                        secondfragment = new Secondfragment();
                    }
                    fragmentTransaction.replace(R.id.frame, secondfragment);
                    fragmentTransaction.commit();
                    return true;

                default:
                    if (startfragment == null) {
                        startfragment = new StartFragment();
                    }
                    fragmentTransaction.replace(R.id.frame, startfragment);
                    fragmentTransaction.commit();
                    return true;


            }
         }
    });

And the model of a fragment

public class Startfragment extends Fragment { //Ui Elements

//Atributtes
private View view;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_start, container, false);
    return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}}
  • You will need to exchange Activity only if you are going to use a Collapsing Toolbar, something like that. If not, do as requested above .

  • I created a new project and created 2 fragments ( one with a textview written calculation rpm ), I understood the part of putting it in the menu, when I click it opens the fragment, but like this: http://imgur.com/a/yE9TF with the contents of the fragment on the app bar and on the rest of the app, how to leave it part of the app ?

  • I got it, I just had to change the frame of the main content

Browser other questions tagged

You are not signed in. Login or sign up in order to post.