How to call a Fragment Activity as a Fragment? - android

I have a Navigation Drawer in my Main Activity. Now i need to call all the fragments from onNavigationDrawerItemSelected method. But the odd part is that I don't have fragments.All I have activities. So i extended it with Fragment Activity.
MainActivity is the navigation Drawer Activity.
FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main Activity. How can i achieve this ?
I saw many questions Related to this. But i can't relate it with me.
Main Activity
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 1){
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.commit();
}
}
}
FeedListActivity
public class FeedListActivity extends FragmentActivity{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
private static final String TAG = "FeedListActivity";
private ListView NewsView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
ProgressBarCircularIndetermininate progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
NewsView = (ListView) findViewById(R.id.feed_list);
NewsView.setAdapter(listAdapter);
// making fresh volley request and getting json
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getimg();
}
}

FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main
Activity. How can i achieve this ?
A fragmentActivity is not a fragment. It's an activity with support for fragments, it was introduced to make older android builds backward compatible with fragments -- if you're targeting newer API's only you can ignore it. To make make FeedListActivity() a fragment...you must extend from fragment.
Fragment newFragment = new Fragment();
This line here should be creating a new instance of FeedList after you make it a fragment.

Related

Want to move from navigation drawer activity to another screen using fragments to show drawer across all screens

I'm trying for last two days to add fragment next to my drawer activity to get navigation drawer visible across the whole application. I have tried several ways from stackoverflow and many others but still no success. and after that i have to move to 2nd fragment from 1st fragment and so on till the need for navigation drawer.
I want to replace entire view except drawer when i move from from my activity to any fragment. Each fragment have its own layout.xml like an activity(Linear/Relative layouts as parent in them).
Drawer avtivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button btnfragOne = (Button) findViewById(R.id.btnfrag_one);
btnfragOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragOne fragment = new FragOne();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frag2, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
});
}
1st Fragment class:
public class FragOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_one, container, false);
}
// 2nd Fragment class:
public class FragTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_two, container, false);
}
Simply for this task you have to override onNavigationItemSelected method in your Activity, which return id of selected fragment on NavigationDrawer.
Try this,
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedFragment(item.getItemId());
return true;
}
Now displaySelectedFragment,
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.your_fragment_one_id:
fragment = new FragOne();
break;
case R.id.your_fragment_two_id:
fragment = new FragTwo();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_layout_id_which_is_to_be_replace, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.your_drawer_layout_id);
drawer.closeDrawer(GravityCompat.START);
}
Edit -- If you want navigate from FragOne to FragTwo. Try this,
Create a method in your Activity,
public void showFragTwo(){
FragmentManager manager = getSupportFragmentManager();
FragTwo frag = new FragTwo();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.your_layout_id_which_is_to_be_replace, frag);
transaction.commit();
}
Then in your FragOne when you want to start FragTwo, call startFragTwo method from Activity as,
((YourActivity) getActivity()).showFragTwo();

how to call an activity from a self created slide menu (one which found in default in navigation drawer activity) under Tabbed activity

I created a slide menu (one which found in default in navigation drawer activity) under a tabbed activity. I can call a fragment from the slide menu choices like shown in the code below. My question is , how can I call an activity instead of a fragment. Can anyone help me on this? Here is the code
public class MainActivity extends AppCompatActivity
{
DrawerLayout drawerLayout;
NavigationView navigationView;
android.support.v4.app.FragmentManager FM;
FragmentTransaction FT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationView = (NavigationView) findViewById(R.id.shitstuff);
FM = getSupportFragmentManager();
FT = FM.beginTransaction();
FT.replace(R.id.containerView, new TabFragment()).commit();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_history) {
FragmentTransaction fragmentTransaction = FM.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new HistoryFragment()).commit();
}
else if (id == R.id.nav_gallery) {
FragmentTransaction fragmentTransaction = FM.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new GalaryFragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
android.support.v7.widget.Toolbar toolbar =(Toolbar)findViewById(R.id.toolbar);
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
}
Simply call activity instead of a fragment.
If you are new to the Android, on below code "this" refers to a current activity, HistoryAcitvity is a new activity which is going to start. Before you write this code, create activity, and add that activity to manifest(if you do it in the android studio it does automatically).
if (id == R.id.nav_history) {
Intent historyActivity = new Intent(this ,HistoryAcitivity.class);
startActivity(historyActivity);
}

Fragment does not display when select from navigation drawer

I have three fragments in my activity. I am using Recyclerview to display them. I wanted to make groupings of my fragment, like when A is selected in navigation drawer fragment 1 and 2 will display, using viewpager. And this works fine.
But when I select fragment B it does not display. And fragment B is not in the grouping.
Please help me out.
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{
Toolbar toolbar;
SharedPreferences savedPreferences;
private static Context mContext;
private FragmentDrawer drawerFragment;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setTitle("");
toolbar.setSubtitle("");
setSupportActionBar(toolbar);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new TabFragment();
break;
case 1:
fragment = new MoviesFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
}}
In this case MoviesFragment is not displaying. When i select MoviesFragment from navigation drawer, only a blank white screen appears.
Thanks!
Solved it!
The problem was not on MainActivity, it was the MoviesFragment itself. I just added an empty constructor and it worked.

How to set NavigationDrawer item click listener?

I have two classes.One is an abstract classBaseActivity.java which extends AppcompactActivity and another one is DrawerFragment.java which extends Fragment! How do I make BaseActivity class implement NavigationItemSelectedListener and handle navigation item click events from this activity! I have copy pasted the related code below:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = BaseActivity.class.getSimpleName();
protected Toolbar mToolbar;
protected DrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setElevation(0);
// Set navigation drawer
mNavigationDrawerFragment = (DrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setNavig((DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
Drawer Fragment.java
public class DrawerFragment extends Fragment{
private NavigationView navigView;
private Activity activity;
private static final String TAG = "NavigationDrawer";
private DrawerLayout drawerLayout;
private TextView fullNameTextView;
private ImageView profileImage;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.navigation_drawer_fragment, container, false);
navigView=(NavigationView)view.findViewById(R.id.main_drawer);
// Inflating nav_header layout on top of navigation drawer layout
View temp=getActivity().getLayoutInflater().inflate(R.layout.nav_header,navigView,false);
navigView.addHeaderView(temp);
profileImage= (ImageView) temp.findViewById(R.id.profile_image);
fullNameTextView= (TextView) temp.findViewById(R.id.fullNameTextView);
return view;
}
public void setNavig(DrawerLayout drawerLayout, Toolbar toolbar) {
ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(activity, drawerLayout,toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
this.drawerLayout=drawerLayout;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity=getActivity();
}
}
I know how to implement item click listener on DrawerFragment.java class and handle events there.But its not what I am looking for.I intend to do the same from BaseActivity.java class
//MAKE A PUBLIC METHOD IN THE NAVIGATION VIEW THAT RETURNS NAVIGATION VIEW
//in DrawerFragment
public NavigationView getNav(){
return navView;
}
//use that public method from baseactivity.java and set listener on the obtained object
getNav().setNavigationItemSelectedListener(this);
but the best way is to implement listener on the navigation drawer
fragment itself. because it makes navigation drawer complete and decoupled with BaseActivity(or any other class). OR You can do is like the one below Using Interface Mechanism as below:
//INSIDE FragmentDrawer
//Let FragmentDrawer Implement OnItemSelectedListener
//then you need to override onItemSelected() method
#Override
public void OnItemSelected(int pos){
//Call The method implemented on your Activity using interface
communicator.displayItem(pos);
//where communicator has reference to your activity and dispayItem() is implemented method
}

Android navigation drawer always open on tablet

I have seen many examples around, but I could not understand how to have the navigation drawer, always open in the tablet. I'm using this navigation drawer https://github.com/kanytu/android-material-drawer-template. I will have to create another xml file? and in OnCreate () what kind of code I need to add?
public class MainActivity extends AppCompatActivity implements NavigationDrawerCallbacks {
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
..
..
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void onResume() {
super.onResume();
}
}
I do not have the precise knowledge but a workaround could be to make a different xml file for large devices without the DrawerLayout so when it runs on a tablet it will return null and no other boolean will be neede in the code and this will make the coding part very simple :-) kudos !

Categories

Resources