Fragments 1
Fragments 1
Introduction
Fragment Lifecycle
Methods
onViewCreated()
DialogFragment
ListFragment
PreferenceFragment
Responsive Design
Tap on a row and see detailed information about that item. The detailed fragment can
be either in the same activity (on a sufficiently wide screen), or in a different activity.
activity_item_twopane.xml
Creating A Fragment
1.Make a new Fragment (File >> New File >> Fragment >> Blank)
2.Override onCreateView() to create the Fragment's View
3.Edit the layout xml as needed (we'll just add a Button)
Aside: Android provides 5 Fragment subclasses which we will explore
later. Here the goal is to make our own subclass.
Overriding onCreateView()
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container, false);
Button btn = (Button) view.findViewById(R.id.clickMeBTN);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){ /* .... */ }
});
return view;
}
}
Adding a Fragment to an
Activity Using XML
An Example
res/layout-large/news_articles.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Specify fragments
in the activity's
layout file
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
An Example, Enlarged
HeadlinesFragment
ArticleFragment
Placing Fragments
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/topFRG"
android:name="org.mprogers.fragmento.TopFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="24dp"/>
<fragment
android:id="@+id/bottomFRG"
android:name="org.mprogers.fragmento.BottomFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fragment1"
android:layout_below="@+id/fragment1"
android:layout_marginTop="31dp"
tools:layout="@layout/bottom_fragment" />
</RelativeLayout>
Adding a Fragment at
Runtime
An Example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
/res/layout/news_articles.xml
When the layout isn't so large ...
<FrameLayout xmlns:android="http://.../res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
An Example, Continued
}
}
where to add
what to add
Replacing Fragments
public void onArticleSelected(int position) {
// Create fragment and give it an argument specifying the article it should show
ArticleFragment articleFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
articleFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, articleFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Preferred technique:
interface1
interface2
interface1
interface2
An Example
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;// this will be the Activity
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity; // gotcha!
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
Fragment Constructors
Arguments @ Work
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
Log.d("TAG", "Greetings from newInstance()");
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
Log.d("TAG", String.format("Greetings from onCreate() where
params are %s & %s", mParam1, mParam2));
}
ListFragment
Exercises
Exercises
by using a FragmentManager().
Write the code necessary to instantiate a Fragment called FrangoMints and display
it in an activity's FrameLayout (assume the same is as in question 7)
Exercises
Can you have 2 files, both named my_layout.xml, defined in both /res/layout-port
and /res/layout-land? If so, when would each file be utilized?
Name the key methods of the WebViewFragment class What does loadUrl() do?
What does setWebClient() do?
Resources
http://developer.android.com/training/basics/fragments/index.html
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/guide/practices/screens_support.html
http://stackoverflow.com/questions/6677136/member-variables-vssetarguments-in-fragments
http://stackoverflow.com/questions/6091194/how-to-handle-button-clicksusing-the-xml-onclick-within-fragments
http://stackoverflow.com/questions/19844244/fragmentbasics-gives-nullpointer-exception
http://stackoverflow.com/questions/26805863/android-studio-directory-creation