Hey everyone, having some trouble trying to call upon a fragment to add a new activity to my nav drawer layout. I'm trying to set up my nav drawer so when an item gets clicked in the menu, it opens a new fragment for that specified activity inside of my home layout. My code I currently have is attached, and any help would be appreciated. I am a beginner/intermediate programmer and would like any help and advice I can get.
-- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/8ab3537c-b671-4f5a-b7c1-5ff5b46f2f9a%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
package com.weebly.httpmykyrutech.the_echo; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class arts_and_entertainment extends Nav_Drawer { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private OnFragmentInteractionListener mListener; private Fragment FragmentTransaction; public arts_and_entertainment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_arts_and_entertainment, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { FragmentTransaction.onDetach(); mListener = null; } public Fragment getArguments() { return FragmentTransaction; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p/> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } }
package com.weebly.httpmykyrutech.the_echo; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; public abstract class Nav_Drawer extends home implements NavigationView.OnNavigationItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nav_header_home); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_home_nav_drawer); 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.activity_home_nav_drawer); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_home_nav_drawer); 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.activity_home_nav_drawer_drawer, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } 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.Home) { Intent i = new Intent(this, home.class); startActivity(i); } else if (id == R.id.Locker_Room) { Intent i = new Intent(this, Locker_Room.class); startActivity(i); return true; } else if (id == R.id.calendar) { Intent i = new Intent(this, calendar.class); startActivity(i); return true; } else if (id == R.id.gossip) { Intent i = new Intent(this, gossip.class); startActivity(i); return true; } else if (id == R.id.birthday) { Intent i = new Intent(this, birthday.class); startActivity(i); return true; } else if (id == R.id.arts_and_entertainment) { Intent i = new Intent(this, arts_and_entertainment.class); startActivity(i); return true; } else if (id == R.id.content_current) { Intent i = new Intent(this, current_events.class); startActivity(i); return true; } else if (id == R.id.spotlight) { Intent i = new Intent(this, spotlight.class); startActivity(i); return true; } else if (id == R.id.KHS) { Intent i = new Intent(this, KHS_Exclusive.class); startActivity(i); return true; } else if (id == R.id.send) { return true; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_home_nav_drawer); drawer.closeDrawer(GravityCompat.START); return true; } public abstract View onCreateView(LayoutInflater inflater, ViewGroup container); abstract void onDetach(); }
package com.weebly.httpmykyrutech.the_echo; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.View; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; import static android.support.design.widget.NavigationView.OnNavigationItemSelectedListener; public abstract class home extends AppCompatActivity implements OnNavigationItemSelectedListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ public GoogleApiClient client; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.app_bar_home); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent Email = new Intent(Intent.ACTION_SEND); Email.setType("text/email"); Email.putExtra(Intent.EXTRA_EMAIL, new String[]{"mykyru.w...@gmail.com"}); Email.putExtra(Intent.EXTRA_SUBJECT, "add your subject"); Email.putExtra(Intent.EXTRA_TEXT, "Dear Cross Stitch," + ""); startActivity(Intent.createChooser(Email, "send Feedback:")); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_home_nav_drawer); 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.activity_home_nav_drawer); navigationView.setNavigationItemSelectedListener(this); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_home_nav_drawer); 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.home, menu); return true; } void onAttach(Context context) { } @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "home Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches this app activity's content, Uri.parse("http://host/path"), Uri.parse("android-app://com.weebly.httpmykyrutech.the_echo/http/host/path") ); AppIndex.AppIndexApi.start(client, viewAction); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "home Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), Uri.parse("android-app://com.weebly.httpmykyrutech.the_echo/http/host/path") ); AppIndex.AppIndexApi.end(client, viewAction); client.disconnect(); } }