toolbar not showing up in fragment with google maps - android

I have an android fragment in which I bring google maps into. I am trying to add a android toolbar to the top.
My xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/statsSpin"
android:spinnerMode="dropdown"/>
</android.support.v7.widget.Toolbar>
<!-- Lots of fancy layout -->
<RelativeLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</RelativeLayout>
When I run my app though, the android toolbar does not show up...
My fragment looks like this:
public class BreweryTappedMap extends Fragment {
public BreweryTappedMap(){}
String beerId = "";
GoogleMap mMap;
private SupportMapFragment fragment;
private GoogleMap map;
String userID;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_brewmap, container, false);
((ActionBarActivity)getActivity()).getSupportActionBar().setTitle("Your Maps");
Spinner spinner = (Spinner) rootView.findViewById(R.id.statsSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.mappage, R.layout.dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch (position) {
case 0:
break;
case 1:
// do whatever stuff you wanna do here
Fragment Fragment_one;
FragmentManager man= getActivity().getSupportFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BreweryMap();
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
userID = prefs.getString("userID", null);
//add url
//call async to get breweries to add to
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
#Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
String url = "myURL";
new GetVisitedBreweries(getActivity(), map).execute(url); }
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/statsSpin"
android:spinnerMode="dropdown"/>
</android.support.v7.widget.Toolbar>
<!-- Lots of fancy layout -->
<RelativeLayout
android:id="#+id/map"
android:layout_below="#+id/toolbar" <----- You need this
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</RelativeLayout>

You are using ToolBar widget, so you should set toolbar before use it:
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
if (toolbar != null) {
getActivity().setSupportActionBar(toolbar);
getActivity().getSupportActionBar().setTitle("Your Maps");
// ...
}

Related

AccelerateInterpolator sync state

im new in android developer world and need help, i need access state of pressed on toolbar from any button which will be seen after press on Toolbar, i want close it or hide open view on button press, Please advise
My code :
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment with the ProductGrid theme
View view = inflater.inflate(R.layout.product_grid_fragment, container, false);
// Set up the tool bar
setUpToolbar(view);
esas_btn = (Button)view.findViewById(R.id.esas_btn);
mq = (Button)view.findViewById(R.id.mq_btn);
sur = (Button)view.findViewById(R.id.sure_btn);
da = (Button)view.findViewById(R.id.dua_btn);
esas_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new EsasFragment());
}
}); mq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new MQMaterialFragment());
}
}); sur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new SurelerFragment());
}
}); da.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Begin the transaction
setFragment(new DualarFragment());
}
});
return view;
}
private void setFragment(Fragment fragment){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "visible_fragment");
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit(); toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.shr_branded_menu));
}
private void setUpToolbar(View view) {
toolbar = view.findViewById(R.id.app_bar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
activity.setSupportActionBar(toolbar);
}
toolbar.setNavigationOnClickListener(new NavigationIconClickListener(
getContext(),
view.findViewById(R.id.product_grid),
new AccelerateInterpolator(),
getContext().getResources().getDrawable(R.drawable.bran_menu),
getContext().getResources().getDrawable(R.drawable.close_menu))); // Menu close icon
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
tools:context=".SelectionGridFragment">
<LinearLayout
style="#style/Widget.Shrine.Backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingTop="88dp">
<include layout="#layout/shr_backdrop" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
style="#style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingStart="12dp"
android:paddingLeft="12dp"
android:paddingEnd="12dp"
android:paddingRight="12dp"
app:contentInsetStart="0dp"
app:navigationIcon="#drawable/shr_branded_menu"
app:title="#string/app_name" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="#+id/product_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:background="#color/productGridBackgroundColor"
android:elevation="8dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/content_frame"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Replace Fragment over Fragment that contains ViewPager

I have ONE activity that calls a FragmentParent. This contains a viewpager. The viewpager has 2 tabs. In each tab there is a fragment. In the first tab (TAB-A) there is a fragment that contains a Recyclerview. When I click over an item the app opens new fragment, but when I go back ALL the fragments inside the viewpager get empty.
I am using Fragments and support library v-13, I already tried with v-4 but I had the same issue.
This is my code:
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_test);
initWithFragments(savedInstanceState);
}
public void initWithFragments(Bundle savedInstanceState) {
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
MainFragment mainFragment = new MainFragment();
//Fragment transaction
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_container, mainFragment);
ft.commit();
}
}
MainFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_app, container, false);
//Enabled menu
setHasOptionsMenu(true);
// Initialize Toolbar and View Pager
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each section
SectionsPageAdapter mSectionsPagerAdapter = new SectionsPageAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.container_view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
switch (position) {
case 0:
toolbar.setTitle("Home");
break;
case 1:
toolbar.setTitle("Downloads");
break;
default:
break;
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
// Set up tab layout
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
return rootView;
}
fragment_app
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
This is the FragmentA inside the viewpager
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorGrayLight"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/container_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/video_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Also I have the PageAdapter
public class SectionsPageAdapter extends FragmentPagerAdapter {
private int NUM_PAGES = 2;
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return SearchFragment.newInstance();
case 1:
return DownloadFragment.newInstance();
default:
return new Fragment();
}
}
#Override
public int getCount() {
return NUM_PAGES;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Search";
case 1:
return "Downloads";
}
return null;
}
}
I am using FragmentTransaction to replace Fragments...
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,settingFragment);
ft.addToBackStack(null);
ft.commit();
If you guys can help me, I will really appreciate
Thanks
This is what I want to:
change FragmentA layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorGrayLight"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/container_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/video_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</FrameLayout>
Now when you replace fragment with this
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,settingFragment);
ft.addToBackStack(null);
ft.commit();
it will take R.id.fragment_container from FragmentA
I finally resolved using getChildFragments:
I used getChildFragmentManager instead of getFragmentManager. I passed to the constructor of the PageAdapter
SectionsPageAdapter mSectionsPagerAdapter = new SectionsPageAdapter(getChildFragmentManager());
In the view holder of the RecyclerView I created an Interface to communicate Nested Fragments with the Activity(main container). Using this way I could replace Fragments in the R.id.fragment_container
I found the solution in this link:
Adding child Fragment to Parent Fragment withing a ViewPager in Android

Android View Displaying twice

This is what I mean by displaying twice. Both of them are able to scroll depending on where you place your finger.
I have a basic adapter that doesn't do anything other than load the view (which has some placeholder data seen above).
Here is where the view is referenced in the adapter:
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item_forecast, parent, false);
return view;
}
And the Adapter is call in a fragment here:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);
ListView forecastListView = (ListView) rootView.findViewById(R.id.listView_forecast);
forecastListView.setAdapter(mForecastAdapter);
forecastListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView adapterView, View view, int position, long l) {
// CursorAdapter returns a cursor at the correct position for getItem(), or null
// if it cannot seek to that position.
Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
if (cursor != null) {
String locationSetting = Utility.getPreferredLocation(getActivity());
Intent intent = new Intent(getActivity(), DetailActivity.class)
.setData(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
locationSetting, cursor.getLong(COL_WEATHER_DATE)
));
startActivity(intent);
}
}
});
return rootView;
}
I also have a cursor loader in that fragment that swaps the cursor onLoadFinished and onLoaderReset.
My layout.xml file, just in case it's relevant.
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for weather forecast list item for future day (not today) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="#+id/list_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/list_item_date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tomorrow"/>
<TextView
android:id="#+id/list_item_forecast_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/list_item_high_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="81"/>
<TextView
android:id="#+id/list_item_low_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="68"/>
</LinearLayout>
</LinearLayout>
Asked for my MainActivity.java:
public class MainActivity extends AppCompatActivity {
String LOG_TAG = MainActivity.class.getSimpleName();
private final String FORECASTFRAGMENT_TAG = "FFTAG";
String mLocation;
private void showMap(Uri zipCode){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(zipCode);
if(intent.resolveActivity(this.getPackageManager()) != null){
startActivity(intent);
}else{
View view = getCurrentFocus();
Snackbar.make(view, "No Map application found", Snackbar.LENGTH_SHORT).setAction("", null).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//
// mLocation = Utility.getPreferredLocation(this);
//
// Log.v(LOG_TAG, "onCreate()");
mLocation = Utility.getPreferredLocation(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
.commit();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.v(LOG_TAG, "onDestroy()");
}
#Override
protected void onStart() {
super.onStart();
Log.v(LOG_TAG, "onStart()");
}
#Override
protected void onResume() {
super.onResume();
if(mLocation != Utility.getPreferredLocation(this)){
mLocation = Utility.getPreferredLocation(this);
ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentByTag(FORECASTFRAGMENT_TAG);
ff.onLocationChange();
}
Log.v(LOG_TAG, "onResume()");
}
#Override
protected void onStop() {
super.onStop();
Log.v(LOG_TAG, "onStop()");
}
#Override
protected void onPause() {
super.onPause();
Log.v(LOG_TAG, "onPause()");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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.
Context context = getApplicationContext();
// int duration = Toast.LENGTH_SHORT;
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// Toast toast = Toast.makeText(context, "Settings Pressed", duration);
// toast.show();
Intent intent = new Intent(context, SettingsActivity.class);
startActivity(intent);
return true;
}else if(id == R.id.action_map_view){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplication());
String zipCode = preferences.getString(getString(
R.string.pref_location_key),
getString(R.string.pref_location_default));
Uri geolocation = Uri.parse("geo:0,0?").buildUpon()
.appendQueryParameter("q", zipCode)
.build();
showMap(geolocation);
}
return super.onOptionsItemSelected(item);
}
}
This is the main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.sunshine.app.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<!--<android.support.design.widget.FloatingActionButton-->
<!--android:id="#+id/fab"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom|end"-->
<!--android:layout_margin="#dimen/fab_margin"-->
<!--android:src="#android:drawable/ic_dialog_email" />-->
</android.support.design.widget.CoordinatorLayout>
And this is the content_main.xml that is referenced:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.example.android.sunshine.app.ForecastFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_main" />
I have seen this sort of behavior before with fragments. It indicates that you have more than one fragment that has been attached to the activity. Each with its own functioning listview. I am unsure how this is happening as of yet in your code.
EDIT
Two fragments are being attached in the following places.
Once in your xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.example.android.sunshine.app.ForecastFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_main" />
And once in your Activity:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
.commit();
To fix this, remove
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
.commit();
in your onCreate() of your activity and you'll now have just one list displaying.
You don't need to add the fragment in the onCreate since you have declared it in the xml:
Remove this from your onCreate:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment(),FORECASTFRAGMENT_TAG)
.commit();
}
And make it this:
#Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mLocation = Utility.getPreferredLocation(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Changing fragments

I am trying to show different fragments depending on whether GPS is enabled or not. But I am getting this error :-
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1
Please advise me how to do this:
Here is my MainActivity class:
public class MainActivity extends Activity {
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
fr = new FragmentTwo();
getFragment();
} else {
fr = new FragmentOne();
getFragment();
}
}
public void getFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne:-
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
FragmentTwo:-
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
My activity_main:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
And the fragment_one.xml and fragment_two.xml are same just the different text:-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GPS IS ENABLE"/>
</RelativeLayout>
Thanks in advance.
Replace
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
With
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
As static Fragment can not be replace at run time. But you can add or replace fragment in FrameLayout.
You can remove this line
android:name="com.test.FragmentTwo"
<fragment
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

MapFragment takes up whole screen when I'm trying to use a ViewPager

My app's MainActivity has two Fragments within a ViewPager, a MapFragment and a ListFragment. I'm trying to follow Arshu's guide on adding the MapFragment into a ViewPager but without much luck. If you want to see what I mean, here's a screenshot from my app:
Here's a condensed form of the MainActivity:
#Bind(R.id.activity_main_tabs)
PagerSlidingTabStrip mainTabs;
#Bind(R.id.activity_main_pager)
ViewPager mainPager;
#Bind(R.id.activity_main_toolbar)
Toolbar activityToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(activityToolbar);
ButterKnife.bind(this);
fragManager = getSupportFragmentManager();
MainTabPagerAdapter adapter = new MainTabPagerAdapter(fragManager, aircraftArrayList);
mainPager.setAdapter(adapter);
mainTabs.setViewPager(mainPager);
}
My ViewPager's TabPagerAdapter:
public class MainTabPagerAdapter extends FragmentStatePagerAdapter {
private final String[] TAB_TITLES = {"Map", "List of planes"};
public final String AIR_KEY = "aircraftKey";
public static FragmentManager fragMgr;
public Bundle bundle;
ArrayList<Aircraft> aircraftArrayList;
public MainTabPagerAdapter(FragmentManager fm, ArrayList<Aircraft> aircraftArrayList) {
super(fm);
fragMgr = fm;
this.aircraftArrayList = aircraftArrayList;
}
#Override
public CharSequence getPageTitle(int position){
return TAB_TITLES[position];
}
#Override
public int getCount() {
return TAB_TITLES.length;
}
#Override
public Fragment getItem(int position){
switch(position){
case 0:
return MainMapFragment.newInstance(aircraftArrayList);
case 1:
return AircraftListFragment.newInstance(1, aircraftArrayList);
}
return null;
}
}
My MainActivity's layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main"
android:fitsSystemWindows="true"
tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#eeeeee">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/activity_main_tabs"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_gravity="top"
android:background="#eeeeee"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:pstsIndicatorColor="#33ccee"
app:pstsIndicatorHeight="4dp"
app:pstsUnderlineColor="#33ccee"
app:pstsUnderlineHeight="2dp"
app:pstsShouldExpand="true"/>
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
And, if it helps, my MapFragment:
public class MainMapFragment extends Fragment implements OnMapReadyCallback {
static View view;
SupportMapFragment mainMapFrag;
private GoogleMap googleMap;
private CameraPosition camPos;
public static MainMapFragment newInstance(ArrayList<Aircraft> aircraftArrayList) {
MainMapFragment fragment = new MainMapFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(AIR_KEY, aircraftArrayList);
fragment.setArguments(bundle);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(container == null)
return null;
view = inflater.inflate(R.layout.fragment_main_map, container, false);
ButterKnife.bind(this, view);
mainMapFrag = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.main_map);
mainMapFrag.getMapAsync(this);
setUpMapIfNeeded();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
if(googleMap != null)
setUpMap(googleMap);
else { //It's null anyway
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)).getMapAsync(this);
mainMapFrag.getMapAsync(this);
if (googleMap != null)
setUpMap(googleMap);
}
}
#Override
public void onPause() {
super.onPause();
if(googleMap != null)
camPos = googleMap.getCameraPosition();
googleMap = null;
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
//Wait until googleMap is re-initialised
if(camPos != null & googleMap != null) {
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));
camPos = null;
}
}
//Sets up the map if it hasn't been set up already
//Somehow it works just fine if I get rid of "static"
protected void setUpMapIfNeeded() {
if (googleMap == null){
Log.d(TAG, "Map was null");
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)) //TODO: FIX THIS
.getMapAsync(this);
//Check if the map was obtained successfully
if (googleMap != null)
setUpMap(googleMap);
}
}
/** This is where markers, lines and listeners are added, and where the camera is moved.
* #param googleMap The GoogleMap object to be set up.
*/
private void setUpMap(GoogleMap googleMap) {
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//googleMap.setMyLocationEnabled(true); //TODO: Maybe wait until I have the pointing function worked out
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)))
.setTitle("My server is here");
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(53.5, -6.35), 8.0f));
}
#Override
public void onMapReady(final GoogleMap gMap) {
googleMap = gMap;
setUpMap(googleMap);
}
Its layout file:
<RelativeLayout 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"
tools:context="com.example.se415017.maynoothskyradar.fragments.MainMapFragment">
<fragment
android:id="#+id/main_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Can someone please tell me what I'm doing wrong?
It turns out I messed up the layout file for the MainActivity by putting the PagerSlidingTabStrip within the ViewPager element. Here's my fixed file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main"
android:fitsSystemWindows="true"
tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:visibility="visible" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#eeeeee"
android:clickable="false"/>
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/activity_main_tabs"
android:layout_below="#id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#eeeeee"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:pstsIndicatorColor="#33ccee"
app:pstsIndicatorHeight="4dp"
app:pstsUnderlineColor="#33ccee"
app:pstsUnderlineHeight="2dp"
app:pstsShouldExpand="true"
android:visibility="visible"
android:measureAllChildren="false"
android:longClickable="true"
android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>

Categories

Resources