Question
its in mobile programing
Design a Navigation Drawer Activity which has one menu item called “Cinemas. When user clicks the Cinemas menu item. The a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.create a android project

2.

Create a menu/drawer_view.xml file:


android:id="@+id/cinemas"
android:icon="@drawable/ic_one"
android:title="Cinemas" />

3.

Now, let's setup the drawer in our activity. We can also setup the menu icon too.

public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;

private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
  
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}

return super.onOptionsItemSelected(item);
}
}

3.

Navigating between Menu Items
Setup a handler to respond to click events on the navigation elements and swap out the fragment. This can be put into the activity directly:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
nvDrawer = (NavigationView) findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
}

private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}

public void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.cinemas:
fragmentClass = FirstFragment.class;
break;
}

try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}

}

4.

public class ColorsFragment extends Fragment {

public ColorsFragment() {}

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

View rootView = inflater.inflate(R.layout.fragment_colors, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.list);

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

ItemData itemsData[] = {
new ItemData("Taj cinima", R.drawable.circle),
new ItemData("Grand cinima", R.drawable.color_ic_launcher),
new ItemData("prime cinima", R.drawable.indigo),
};


MyAdapter mAdapter = new MyAdapter(itemsData);
recyclerView.setAdapter(mAdapter);

recyclerView.setItemAnimator(new DefaultItemAnimator());

return rootView;
}
}

Add a comment
Know the answer?
Add Answer to:
its in mobile programing Design a Navigation Drawer Activity which has one menu item called “Cinemas"....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT