Android Studio "NavigationDrawer": How to implement a basic GoogleMap? - android

I have to learn, but for YOU it is simple:
I created a simple basic NavigationDrawer-app with Android Studio (the offered NavigationDrawer you can choose when creating a new app).
My question: How (or where) can I put my GoogleMap-fragment? Can I replace the "PlaceholderFragment" or do I create a new Fragment? And how to implement?
May I ask for a LITTLE and EASY example or hint? As I said, just an absolute simple running NavigationDrawer/GoogleMap app. I know a lot about GoogleMap, but I don't get the fusion.
Thanks for any little help and best regards from Germany.

you can replace the PlaceHolderFragment generated with your own Fragment, be it a regular fragment or a GoogleMap fragment.
The private PlaceHolderFragment class is created for your convenience, but you could just create a separate Fragment class and remove the PlaceHolderFragment class. You would open your map fragment instead of the PlaceHolderFragment inside the method onNavigationDrawerItemSelected in the Activity created with the project template.
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, MyMapFragment.newInstance(position + 1)) // instead of PHF
.commit();
}
Hope this helps.

Related

How to manage Fragment in FragmentAdapter?

I have a few fragments in PageAdapter which placed on other fragment and I need somehow to control their states, update some views, etc.
I have code which looks like below:
Fragment frg1;
Fragment frg2;
frg1.setHandleProtocol(this);
class MyAdapter extends FragmentPageAdapter {
public Fragment getItem(int pos) {
return frg1;
}
}
This is very simple snippet, but it should help me to explain. This approach works good by the moment when Activity will be killed and restored by Android. In that case PageAdapter re-creates new fragments by Object class and they won't have a old links and depends.
Is there any good way to handle this new fragments?
I have ugly workaround. I am just going through all fragments in FragmentManager and re-assigning their to the old links.

How to go from one activty to another fragment in android?

I have an Activity called Mytaskonclick extends Activity and a Fragment called SentTaskFragment extends Fragment. I want to go from Mytaskonclick to SentTaskFragment on click of a button. I tried using the code
Intent ii=new Intent(Mytaskonclick.this,SentTaskFragment.class);
startActivity(ii);
But this code doesn't work. Can anyone suggest me how to do it?
Fragments does not work like this.You need to put a place holder in your activity, say FrameLayout for example and, inside on click, you get the fragment and attach it inside this placeholder. I suggest you read the Fragment Tutorial Here. It contains all what you need to know and extremely useful for you.
When you want to switch to a Fragment them you have to do that through FragmentManager. Pass that thae Fragment object into beginTransaction() method of FragmentManager along with the container layout which will hold the Fragment as below...
SentTaskFragment fragment = new SentTaskFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment).commit();
You can see some tutorial from below link...
Android Developer-Fragment
Multi-pane development in Android with Fragments - Tutorial
Android Fragments
Android Fragments Example

Use a Listfragment as a Tab of the actionBar accessed by a ViewPager

I came upon this solution to use a Listfragment within a Tab of the Actionbar
I face the same problem as described there, but somehow this solution doesn't work for me.
I want to write an application for android versions greater than 4.0.
I took the Android Sample code for generating Swipe View with tabs, if you generate a new Project and select: Fixed Tabs+Swipe
one Fragment should be a ListFragment, so I came upon this solution but the problem is the MainActivity uses the: import android.support.v4 support.
Here is a part of my SectionsPagerAdapter the rest of the code is equal to the prelinked thread.
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ControlFragment();
case 1:
fragment = (Fragment)new WrapperListFragment();
}
return fragment;
}
I guess the code here useses the newer packages, so how to combine these two.
In general i want to use the actionbar with swipe feature, and one fragment should display a listView.
Thanks in advance.

Fragment was unable to replace with fragment in viewpager

I'm working with fragments with ViewPager concept. I'm creating a diary app in which I'm using only one fragment which gets all updates from database and show in it. Until this everything is fine...According to my requirement when i click on a button in fragment i need to show another fragment which allows the user to store the images.
My problem is.....
--If i use replace method in fragments it was not replacing properly in the sense if A is fragment which consists of viewpager and B is a fragment i want to replace.
--Then if i use replace B is visible but A also appears under the fragment B
FragmentManager m=getActivity().getSupportFragmentManager();
FragmentTransaction ft = getActivity().getSupportFragmentManager()
.beginTransaction();
Demobutton demobutton = new Demobutton();
ft.replace(R.id.lay, demobutton);
ft.commit();
Hope you guys understand my problem. If you feel my question is incomplete please let me know that.
I have two suggestions depending on how you use the DemoButton Fragment:
1) Maybe your issue is with nested fragments. You get the FragmentManager from the activity but if the Demobutton is already part of an fragment use getChildFragmentManager() of the outer fragment instead.
2) From my experience when using a ViewPager with Fragments the PagerAdapter of the ViewPager should do all the fragment transactions. You could extend and overwrite the class FragmentPagerAdapter from the support library in order to get the correct fragment in your ViewPager when you need it.
I've developed a small example app that achieves this without overwriting native classes.
The point is to use a fragment as a container.
In your ViewPagerAdapter:
#Override
public Fragment getItem(int position) {
/*
* IMPORTANT: This is the point. We create a RootFragment acting as
* a container for other fragments
*/
if (position == 0)
return new RootFragment();
else
return new StaticFragment();
}
RootFragment layout should look like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root_frame" >
</FrameLayout>
You could review my complete explanation here: https://stackoverflow.com/a/21453571/1631136

Attach a fragment to an activity in Android

My older version of an Android app was using 4 different activities (FirstActivity => FourthActivity, with the corresponding xml activity_first => activity_fourth), and the app can switch back and forth between those using Intent. Recently I wanted to change the user interface to use a ViewPagerIndicator. I have implemented 4 fragments like this:
public class FirstFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_first, null);
}
}
The question is, how can I migrate all the business code from FirstActivity to FirstFragment? Do I just need to find the equivalents of onCreate, onDestroy, onStart... and copy/paste the code (adding getActivity(), getView() where appropriate)? Is there any easy way to attach the fragment to an activity to avoid doing so?
The easiest way to do it is to migrate the code you had in your individual activities under onCreate() to onActivityCreated() in the new fragments. You then add the fragments to your activity using a fragment transaction in the onCreate() method of your supporting activity. The android docs give a pretty good walk through of how to do this here, complete with sample code. In case there's some confusion you're going to do the following in your underlying activity:
Get a new FragmentManager
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Create a new instance of your fragment
ExampleFragment fragment = new ExampleFragment();
Add that fragment to your FragmentManager
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
There are a number of different ways to use fragments - like in a ViewPager or ViewSwitcher that require a different implemenation but this sounds like it address what you're trying to do.

Categories

Resources