0

I am new to android and I have implemented android studios drawerlayout. I designed the main page of my app in content_main. But now I want to switch pages by using the DrawerLayout to connect me to my CurrentSongsFragment that I created. Here is the code that is in my MainActivity.java class:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

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


        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();

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

    @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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }

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

        if (id == R.id.nav_camera) {
            setContentView(R.layout.fragment_current_song);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Here is what's in my CurrentSongsFragment.java class:

public class CurrentSongFragment extends android.app.Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.activity_main, container, false);
    }
}

I am really struggling on how to switch the screens to show different content when I click on the fragment in the DrawerLayout. Help on this would be excellent(Let me know if I need to provide other code to be able to solve the solution) :D

1 Answers1

0

Put this layout in your activity_main

<RelativeLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

and to add different fragment

public void addYourFragment(){
                YourFragment myFragment = new YourFragment();
                FragmentManager fragmentManager = this.getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, myFragment, tagToUniqlyIdentifiedFramgent);
                fragmentTransaction.addToBackStack(tagToUniqlyIdentifiedFramgent);
                fragmentTransaction.commit();                       
}




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

    Intent nextIntent;
    switch (id){
        case R.id.item1:
            addYourFragment();
            break;
        case R.id.item2:           
            addYourFragmentTwo()
            break;
        case R.id.item3:
            addYourFragmentThree()
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
} 
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55