Questions tagged [android-fragments]

Fragments represent reusable behaviors or portions of the user interface in an Android app.

A Fragment represents a behavior or a portion of user interface in an Activity. and reuse a fragment in multiple activities.

Fragments were first introduced in Android 3.0 (API 11). However, the Support V4 Library back-ported fragments to every version of Android from 1.6 (API 4) onwards.

Here are the important things to understand about fragments:

  • A Fragment is a combination of an XML layout file and a java class much like an Activity.
  • Using the support library, fragments are supported back to all relevant Android versions.
  • Fragments encapsulate views and logic so that it is easier to reuse within activities.
  • Fragments are standalone components that can contain views, events and logic.
  • You can combine multiple fragments in a single activity to build a multi-pane UI.

Usage of Fragments in different environments

  • You can add fragments to your app directly with XML or through the FragmentManager in Java.
  • The FragmentManager is responsible for all runtime management of fragments including adding, removing, hiding, showing, or otherwise navigating between fragments. The fragment manager is also responsible for finding fragments within an activity.

The ApiDemos sample application present in the SDK provides runnable fragment examples and source code.

You can find more information in:

Tag Usage:

45668 questions
11
votes
6 answers

View in the view pager overlap between previous and next view

I am using ViewPager for sliding effect in my app. I have pager.setPageMargin() to make next and previous views visibility, unfortunately my next view is over lapping with current view. Below is my code pager = (ViewPager)…
11
votes
2 answers

Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference

I'm trying to implement sliding tab layout using android material design. But it gives me NullPointerException. Here is my code so far: MainActivity.java public class MainActivity extends ActionBarActivity { private Toolbar toolbar; private…
11
votes
7 answers

Tabs of TabLayout not showing

I have a main activity, which hosts a fragment, which in turn hosts a TabLayout (with a ViewPager). The tab bar is shown, baut the tabs themselves are not shown. Here is my code in the main activity for displaying the host fragment: Fragment…
mrd
  • 4,561
  • 10
  • 54
  • 92
11
votes
1 answer

Collapsing Toolbar and Fragment

I have an app that is laid out like this, the mainactivity hosts 2 tabs with accompanied fragments, the first fragment has a recycler view that works, I'm trying to add the parallax effect as the person scrolls up or down, wasn't sure if I should…
the_islander
  • 207
  • 1
  • 4
  • 19
11
votes
4 answers

The android:name attribute in the

Please explain ArticleListFragment and ArticleReaderFragmet as they are in this code:
hamid_c
  • 849
  • 3
  • 11
  • 29
11
votes
2 answers

Floating Action Button in an Activity - how to anchor to a recyclerview in a fragment?

I had the same problem in this question Floating Action Button not showing fully inside a fragment I have a tablayout in my activity_main and 2 tabs (with different fragments, one of them contains a recyclerView). When I put the FAB in the…
11
votes
3 answers

Cordova webview inside Android Fragment on Cordova 4.0

I just upgraded to cordova 4.0 for android. I used the following post to load a cordova webview inside a fragment.. https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment This code no longer…
TWilly
  • 4,863
  • 3
  • 43
  • 73
11
votes
1 answer

How to achieve smooth fragment transaction animations on Android

I've mostly worked with iOS and have become accustomed to very smooth and fluent screen change animations. I am now working on an Android app and can't for the life of me get a fragment transaction to smoothly add/replace a fragment. My set up is…
kev
  • 2,306
  • 3
  • 26
  • 31
11
votes
1 answer

What is the best way to save state of RecyclerView?

I have fragment contains recyclerview in main activity Fragment.java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view =…
Ilnar Karimov
  • 339
  • 2
  • 6
  • 19
11
votes
2 answers

How to create a dropdown within a Navigation Drawer (in Android)?

I would like to be a able to create a Navigation Drawer that has some expandable, selectable items, and some non-expandable items. The consensus on StackOverflow for questions similar to mine point to solutions by ExpandableListView (which may not…
11
votes
3 answers

DialogFragment dismiss() does not pop backstack

I have a simple DialogFragment that calls dismiss when exits, according to the documentation: public void dismiss() Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this…
Monster Hunter
  • 846
  • 1
  • 12
  • 24
11
votes
3 answers

Adding actionbar to FragmentActivity

I am making an app that is using google maps for some specific reason. So I created an Google Maps app in Android Studio and I got an activity that extends FragmentActivity. Everything works perfectly but now I want to add the action bar at top of…
11
votes
6 answers

onActivityResult not call in the Fragment

The structure of the app is like this: tabHost (in Activity) -> contains -> TabFragment(extend base container fragment) 1. The code in Activity: tabHost.addTab( tabHost.newTabSpec("home").setIndicator("", …
11
votes
2 answers

SwitchCompat in actionbar, with fragment

I get an error when trying to set SwitchCompat to action bar. Here is my abswitch.xml
11
votes
2 answers

Dismissed dialog fragment reappears again when app is resumed

Hi I am developing an app that uses maps. I am using Fragment Activity and a fragment named Fragment-A. In Fragment-A there is a button, on clicking that button a Dialog Fragment pops up, displays a map with some location received previously from…