Mobile Computing: Fragments
Mobile Computing: Fragments
Mobile Computing: Fragments
Lecture #04-B
Fragments
Previous Lecture
Android Activities
• Each time a new activity start, previous activity preserves in a stack,
and when required, popped from the stack and resumes.
• By Default, all activities are subclasses of the AppCompatActivity class
and AppCompatActivity class is a subclass of Activity.
• The first task for your activity subclass is to implement the standard
activity lifecycle callback methods (such as OnCreate()) to handle the
state changes for your activity.
• onCreate() method calls setContentView() to create the primary
layout for the activity. This pass the path of XML file.
Previous Lecture
When you create a new activity in your Project, three changes appears
• An XML file containing the layout for the new activity.
• A Java file for the new activity with a skeleton class definition and
onCreate() method.
• An additional <activity> element in the Android manifest that
specifies the new activity
<activity android:name=".newActivity"></activity>
Previous Lecture
Intent Class
Intent is used to start Android Component such as Activity, Content Provider, Broadcast
Receiver.
Intent in = new Intent(getApplicationContext(), secondActivity.class);
startActivity(in);
Bundle Class
Bundle is a utility class that let you store a set of name-vlaue pairs. Into a Bundle object,
you can put integer, long, string, arrays etc. along with the keys to identified them.
Bundle b = new Bundle();
b.putString(“myname” anystring);
String s = b.getString(“myname”);
Previous Lecture
inten.putExtras(bnd); txt1.setText(bn.getString("mystring"));
startActivity(inten);
Today’s Lecture
Fragments
Fragment’s Lifecycle
Fragment Example
Fragment Subclasses
Fragment
An Activity is an application component or fundamental building block
that provides a screen, with which users can interact in order to do
something.
To call a fragment
Fragment frag = new Fragment1(); // Fragment1() is newly created Fragment
fT.replace(android.R.id.content, frag);
fT.commit();
Dynamic Fragment Example
Leave the fragments.java and fragment.xml files without changes.
Write the following code in activity_main.xml and MainActivity.java
<?xml version="1.0" encoding="utf-8"?> public class MainActivity extends AppCompatActivity implements View.OnClickListener {
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" Button press1, press2;
.
android:layout_width="match_parent"
android:layout_height="match_parent"
@Override
protected void onCreate(Bundle savedInstanceState) {
android:orientation="vertical" super.onCreate(savedInstanceState);
>
setContentView(R.layout.activity_main);
press1 = findViewById(R.id.press1);
<Button press2 = findViewById(R.id.press2);
android:id="@+id/press1" press1.setOnClick(this);
android:layout_width="match_parent" press2.setOnClick(this);
android:layout_height="wrap_content" }
android:text="fragment 1"/> @Override
<Button public void onClick(View v) {
android:id="@+id/press2" FragmentManager fm = getFragmentManager();
android:layout_width="match_parent" FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
android:layout_height="wrap_content" Fragment frag;
android:text="fragment 2"/>
if(press1.getId() == v.getId()){
frag = new fragment1();
<LinearLayout ft.replace(R.id.placeholder, frag);
android:id="@+id/placeholder"
ft.commit();
android:layout_width="match_parent"
}
android:layout_height="400dp"
if(press2.getId() == v.getId()){
android:orientation="vertical"
>
frag = new fragment2();
</LinearLayout> ft.replace(R.id.placeholder, frag);
ft.commit();
</LinearLayout> }
}
}
Another Example
Adding
Fragments
in Projects
root New
Fragment (Blank)
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
book_list.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Fragment1.java
public class Fragment1 extends ListFragment implements AdapterView.OnItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg,
Bundle bundle){
return inflater.inflate(R.layout.books_list, vg, false);
}
public void onActivityCreated(Bundle bundle){
super.onActivityCreated(bundle);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.books, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,long id){
Fragment2 fragment = (Fragment2)
getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment2);
if(fragment==null)
Toast.makeText(getActivity(), "Null object ",Toast.LENGTH_LONG).show();
else
fragment.showData(position);
}
}
book_details.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/book_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" <TextView
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:id="@+id/publisher_name"
android:text="@string/book_title" /> android:layout_width="wrap_content"
<TextView android:layout_height="wrap_content"
android:id="@+id/author_name" android:textSize="18dp"
android:layout_width="wrap_content" android:layout_below="@id/author_name"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="@string/publisher" />
android:layout_below="@id/book_title" <ImageView
android:text="@string/author_name" /> android:id="@+id/front_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/publisher_name"
android:src="@drawable/book_img1" />
</RelativeLayout>
book_details.xml
Fragment2.java
public class Fragment2 extends Fragment {
private String []books;
private String []authors;
private String []publishers;
private int []drawables={R.drawable.book_img1, R.drawable.book_img2,
R.drawable.book_img3, R.drawable.book_img4, R.drawable.book_img5,
R.drawable.book_img6};
TextView title, author, publisher;
ImageView image;
public View onCreateView(LayoutInflater inf, ViewGroup vg, Bundle bundle){
return inf.inflate(R.layout.book_details, vg, false);
}
Fragment2.java ….
public void onViewCreated(View view, Bundle bundle){
super.onViewCreated(view, bundle);
books = getActivity().getResources().getStringArray(R.array.books);
authors = getActivity().getResources().getStringArray(R.array.authors);
publishers =
getActivity().getResources().getStringArray(R.array.publishers);
title = view.findViewById(R.id.book_title);
author = view.findViewById(R.id.author_name);
publisher = view.findViewById(R.id.publisher_name);
image = view.findViewById(R.id.front_page);
}
public void showData(int id){
title.setText(books[id]);
author.setText(authors[id]);
publisher.setText(publishers[id]);
image.setImageResource(drawables[id]);
}
}
strings.xml
<resources>
<string name="app_name">Fragment Application</string>
<string name="book_title">Introduction to Android</string>
<string name="author_name">James Graham</string>
<string name="publisher">Cohen and Cohen</string>
<string-array name="books">
<item>Introduction to Algorithms</item>
<item>Android App Development for Dummies</item>
<item>Android Programming Cookbook</item>
<item>Algorithmics, the Spirit of Computing</item>
<item>Design and Analysis of Algorithms</item>
<item>Data Structures and Algorithms in Java</item>
</string-array>
<string-array name="authors">
<item>Thomas H. Cormen</item>
<item>Michael Burton</item>
<item>Google Inc.</item>
<item>David Harel</item>
<item>Sohail Aslam</item>
<item>Michael T. Goodrich</item>
</string-array>
strings.xml
<string-array name="publishers">
<item>MIT Press</item>
<item>John Wiley and Sons</item>
<item>Google Inc.</item>
<item>Addison Wesley</item>
<item>VU Pakistan</item>
<item>John Wiley and Sons</item>
</string-array>
</resources>
Fragment Subclasses
1. DialogFragment
A fragment meant to be shown as a dialog box that pops up on top of the current
activity.
2. ListFragment
A fragment that shows a list of items as its main content.
3. PreferenceFragment
A fragment whose main content is meant to allow the user to change settings for
the app.