13 CHAPTER
13 CHAPTER 2
3
4
5
6
7
06 android:background="#ffffffff"> 07 <TextView 08 android:layout_width="fill_parent" 09 android:layout_height="fill_parent" 10 android:text="fragment #1" 11 android:textcolor="#ff000000" 12 android:gravity="center" /> 09 public class FragmentOne extends Fragment { 10 @Override 11 public View oncreateview(layoutinflater inflater, 12 ViewGroup container, Bundle savedinstancestate) { 13 return inflater.inflate(r.layout.fragment1, container, false); 14 } 15 } 8
05 <fragment 06 class="com.corea.fragment1.fragmentone" 07 android:id="@+id/fragment1" 08 android:layout_width="fill_parent" 09 android:layout_height="fill_parent" /> 9
10
13 public class Fragment2 extends Activity { ~~~ ~~~ 20 FragmentManager fm = getfragmentmanager(); 21 FragmentTransaction ft = fm.begintransaction(); 22 23 FragmentOne f1 = new FragmentOne(); 24 FragmentTwo f2 = new FragmentTwo(); 25 26 if(getwindowmanager().getdefaultdisplay() 27.getRotation()==Surface.ROTATION_0){ 28 ft.replace(android.r.id.content, f1); 29 } else { 11
30 ft.replace(android.r.id.content, f2); 31 } 32 33 ft.commit(); 34 } 35 36 public static class FragmentOne extends Fragment { 37 @Override 38 public View oncreateview(layoutinflater inflater, 39 ViewGroup container, Bundle savedinstancestate) { 40 return inflater.inflate(r.layout.fragment1, container, false); 41 } 42 } 43 44 public static class FragmentTwo extends Fragment { 45 @Override 46 public View oncreateview(layoutinflater inflater, 47 ViewGroup container, Bundle savedinstancestate) { 48 return inflater.inflate(r.layout.fragment1, container, false); 49 } 50 } 51 } 12
13
05 android:orientation="horizontal" > 06 <fragment 07 android:id="@id/android:list" 08 android:layout_width="0dip" 09 android:layout_weight="1" 10 android:layout_height="match_parent" 11 class="com.corea.fragment3.listfrag" /> 12 <fragment 13 android:id="@+id/detail" 14 android:layout_width="0dip" 15 android:layout_weight="5" 16 android:layout_height="match_parent" 17 class="com.corea.fragment3.detailfrag" /> 14
05 android:background="#ffffffff" 06 android:orientation="vertical" > 07 <TextView 08 android:id="@+id/tv" 09 android:layout_width="wrap_content" 10 android:layout_height="match_parent" 11 android:layout_gravity="center_horizontal center_vertical" 12 android:layout_margintop="20dip" 13 android:text="details" 14 android:textcolor="#00000000" 15 android:textappearance="?android:attr/textappearancelarge" /> 15
10 public class DetailFrag extends Fragment { 11 @Override 12 public View oncreateview(layoutinflater inflater, 13 ViewGroup container, Bundle savedinstancestate) { 14 return inflater.inflate(r.layout.detail, container, false); 15 } 16 17 public void settext(string item) { 18 TextView v = (TextView) getview().findviewbyid(r.id.tv); 19 v.settext(item); 20 } 21 } 16
3 public interface OnListItemSelectedListener { 4 public void onlistitemselected(int position); 5 } 10 public class ListFrag extends ListFragment { 11 OnListItemSelectedListener listener; 12 @Override 13 public void onattach(activity activity) { 14 super.onattach(activity); 15 try { 16 listener = (OnListItemSelectedListener)activity; 17 } catch (ClassCastException e) { 18 throw new ClassCastException(activity.toString() 19 + " must implement OnListItemSelectedListener interface"); 20 } 21 } 22 23 @Override 24 public void onactivitycreated(bundle savedinstancestate) { 25 super.onactivitycreated(savedinstancestate); 26 setlistadapter(new ArrayAdapter<String>(getActivity(), 27 android.r.layout.simple_list_item_1, 28 new String[] {"1", "2", "3", "4", "5"})); 29 } 30 17
31 @Override 32 public void onlistitemclick(listview l, View v, 33 int position, long id) { 34 listener.onlistitemselected(position); 35 } 36 } 06 public class Fragment3 extends Activity 07 implements OnListItemSelectedListener { 08 String[] numbers = 09 new String[] { "One", "Two", "Three", "Four", "Five"}; ~~~ ~~~ 18
16 public void onlistitemselected(int position) { 17 DetailFrag df = (DetailFrag) getfragmentmanager() 18.findFragmentById(R.id.detail); 19 if (df!= null && df.isinlayout()) { 20 df.settext(numbers[position]); 21 } 22 } 23 } 19
06 <fragment 07 android:id="@id/android:list" 08 android:layout_width="0dip" 09 android:layout_weight="1" 10 android:layout_height="match_parent" 11 class="com.corea.fragment4.listfrag"/> 12 <FrameLayout 13 android:id="@+id/detail" 20
14 android:layout_width="0dip" 15 android:layout_height="fill_parent" 16 android:layout_weight="5" 17 android:background="?android:attr/detailselementbackground" /> 10 public class DetailFrag extends Fragment { 11 @Override 12 public View oncreateview(layoutinflater inflater, 13 ViewGroup container, Bundle savedinstancestate) { 14 View v = inflater.inflate(r.layout.detail, container, false); 15 TextView tv = (TextView) v.findviewbyid(r.id.tv); 16 17 String[] numbers = getarguments().getstringarray("numbers"); 18 tv.settext(numbers[getarguments().getint("position", 0)]); 19 return v; 20 } 21 } 21
07 public class Fragment4 extends Activity 08 implements OnListItemSelectedListener { 09 String[] numbers = new String[] { "One", "Two", "Three", "Four", "Five"}; ~~~ ~~~ 16 public void onlistitemselected(int position) { 17 DetailFrag df = (DetailFrag) getfragmentmanager() 18.findFragmentById(R.id.detail); 19 if (df == null df.getarguments().getint("position", 0)!= position) { 20 df = new DetailFrag(); 21 Bundle b = new Bundle(); 22 b.putint("position", position); 23 b.putstringarray("numbers", numbers); 24 df.setarguments(b); 25 FragmentTransaction ft = getfragmentmanager().begintransaction(); 26 ft.replace(r.id.detail, df).addtobackstack(null).commit(); 27 } 22
28 } 29 } 23
05 <fragment 06 android:id="@id/android:list" 07 android:layout_width="match_parent" 08 android:layout_height="match_parent" 09 class="com.corea.fragment5.listfrag" /> 24
06 public class DetailActivity extends Activity { 07 @Override 08 protected void oncreate(bundle savedinstancestate) { 09 super.oncreate(savedinstancestate); 10 DetailFrag df = new DetailFrag(); 11 df.setarguments(getintent().getextras()); 12 getfragmentmanager().begintransaction() 13.add(android.R.id.content, df).commit(); 14 } 15 } 17 public void onlistitemselected(int position) { 18 Intent i = new Intent(this, DetailActivity.class); 19 Bundle b = new Bundle(); 20 b.putint("position", position); 21 b.putstringarray("numbers", numbers); 22 i.putextras(b); 23 startactivity(i); 24 } 25
26
27
getactionbar().setdisplayoptions( ); 28
<activity android:theme="@android:style/theme.holo.noactionbar"> getactionbar().hide(); getactionbar().show(); getactionbar().settitle(" ); 29
02 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 03 <item 04 android:id="@+id/add" 05 android:title=" " 06 android:icon="@android:drawable/ic_menu_add" 07 android:showasaction="ifroom" /> 30
08 <item 09 android:id="@+id/refresh" 10 android:title=" " 11 android:showasaction="always withtext" /> 12 <item 13 android:id="@+id/search" 14 android:title=" " 15 android:icon="@android:drawable/ic_menu_search" 16 android:showasaction="always" /> 17 <item 18 android:id="@+id/share" 19 android:title=" " 20 android:icon="@android:drawable/ic_menu_share" 21 android:showasaction="never" /> 22 <item 23 android:id="@+id/etc" 24 android:title=" "/> 25 </menu> 31
06 <Button 07 android:id="@+id/showtitle" 08 android:layout_width="fill_parent" 09 android:layout_height="wrap_content" 10 android:text=" " 11 android:onclick="onbutton"/> 12 <Button 13 android:id="@+id/notitle" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text=" " 17 android:onclick="onbutton"/> 11 public class ActionBar1 extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void oncreate(bundle savedinstancestate) { 15 super.oncreate(savedinstancestate); 16 setcontentview(r.layout.main); 17 getactionbar().sethomebuttonenabled(true); 18 } 19 20 @Override 32
21 public boolean oncreateoptionsmenu(menu menu) { 22 getmenuinflater().inflate(r.menu.menu, menu); 23 return true; 24 } 25 26 @Override 27 public boolean onoptionsitemselected(menuitem item) { 28 String text = null; 29 30 switch(item.getitemid()){ 31 case android.r.id.home: 32 text = "Home"; break; 33 case R.id.add: 34 text = "ifroom"; break; 35 case R.id.refresh: 36 text = "always withtext"; break; 37 case R.id.search: 38 text = "always"; break; 39 case R.id.share: 40 text = "never"; break; 41 case R.id.etc: 42 text = "default"; break; 43 default: 44 return false; 45 } 46 Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 47 48 return true; 49 } 50 51 public void onbutton(view v) { 52 switch(v.getid()){ 53 case R.id.showtitle: 54 getactionbar().setdisplayoptions( 55 ActionBar.DISPLAY_SHOW_HOME 56 ActionBar.DISPLAY_SHOW_TITLE); 57 break; 33
58 case R.id.notitle: 59 getactionbar().setdisplayoptions( 60 ActionBar.DISPLAY_SHOW_HOME); 61 } 62 } 63 } 34
35
06 <Button 07 android:layout_width="fill_parent" 08 android:layout_height="wrap_content" 09 android:id="@+id/white" 10 android:text=" " 11 android:onclick="onbutton" /> 12 <Button 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:id="@+id/black" 16 android:text=" " 17 android:onclick="onbutton" /> 36
13 public void oncreate(bundle savedinstancestate) { ~~~ ~~~ 17 View v = getlayoutinflater().inflate(r.layout.button, null); 18 19 ActionBar ab = getactionbar(); 20 ab.setcustomview(v); 21 ab.setdisplayoptions(actionbar.display_show_home 22 ActionBar.DISPLAY_SHOW_TITLE 23 ActionBar.DISPLAY_SHOW_CUSTOM); 24 } 25 26 public void onbutton(view v) { 27 LinearLayout ll = (LinearLayout)findViewById(R.id.body); 28 switch(v.getid()) { 29 case R.id.white: 30 ll.setbackgroundcolor(color.white); 31 break; 32 case R.id.black: 33 ll.setbackgroundcolor(color.black); 34 } 35 } 36 } 37
38
2 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 3 <item android:id="@+id/action_search" 4 android:title="search" 5 android:icon="@android:drawable/ic_menu_search" 6 android:showasaction="always" 7 android:actionviewclass="android.widget.searchview" /> 8 </menu> 15 public class ActionBar3 extends Activity implements OnQueryTextListener { 16 private SearchView sv; ~~~ ~~~ 25 public boolean oncreateoptionsmenu(menu menu) { 26 super.oncreateoptionsmenu(menu); 27 getmenuinflater().inflate(r.layout.searchview, menu); 28 MenuItem mi = menu.finditem(r.id.action_search); 29 sv = (SearchView) mi.getactionview(); 30 39
31 mi.setshowasactionflags(menuitem.show_as_action_if_room 32 MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 33 SearchManager sm 34 = (SearchManager)getSystemService(Context.SEARCH_SERVICE); 35 if (sm!= null) { 36 List<SearchableInfo> searchables 37 = sm.getsearchablesinglobalsearch(); 38 SearchableInfo info 39 = sm.getsearchableinfo(getcomponentname()); 40 for (SearchableInfo inf : searchables) { 41 if (inf.getsuggestauthority()!= null 42 && inf.getsuggestauthority().startswith("applications")) { 43 info = inf; 44 } 45 } 46 sv.setsearchableinfo(info); 47 } 48 sv.setonquerytextlistener(this); 49 sv.setqueryhint(" "); 50 return true; 51 } 52 53 public boolean onquerytextchange(string newtext) { 54 return false; 55 } 56 57 public boolean onquerytextsubmit(string query) { 58 return false; 59 } 60 } 40
41
06 android:background="#ffffffff"> 07 <TextView 08 android:layout_width="fill_parent" 09 android:layout_height="fill_parent" 10 android:text="text Fragment" 11 android:textcolor="#ff000000" 12 android:gravity="center" /> 06 android:background="#ffffffff"> 07 <ImageView 08 android:layout_width="fill_parent" 09 android:layout_height="fill_parent" 10 android:src="@drawable/ic_launcher" 11 android:gravity="center" /> 42
5 <LinearLayout 6 android:id="@+id/fragment" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" /> 12 public class ActionBar4 extends Activity implements TabListener { 13 Fragment frag1, frag2; 14 /** Called when the activity is first created. */ 15 @Override 16 public void oncreate(bundle savedinstancestate) { 17 super.oncreate(savedinstancestate); 18 setcontentview(r.layout.main); 19 20 ActionBar ab = getactionbar(); 21 ab.setnavigationmode(actionbar.navigation_mode_tabs); 22 frag1 = new FragmentOne(); 23 frag2 = new FragmentTwo(); 24 ab.addtab(ab.newtab().settext("text Fragment") 25.setTabListener(this)); 26 ab.addtab(ab.newtab().settext("image Fragment") 27.setTabListener(this)); 28 } 29 30 public void ontabreselected(tab tab, FragmentTransaction ft) { 43
31 Toast.makeText(this, "Reselected!", Toast.LENGTH_LONG).show(); 32 } 33 34 public void ontabselected(tab tab, FragmentTransaction ft) { 35 ft.replace(r.id.fragment, tab.getposition() == 0? frag1 : frag2); 36 } 37 38 public void ontabunselected(tab tab, FragmentTransaction ft) { 39 ft.remove(tab.getposition() == 0? frag1 : frag2); 40 } 41 } 44
07 <string-array name="list"> 08 <item>text Fragment</item> 09 <item>image Fragment</item> 10 </string-array> 45
11 public class ActionBar5 extends Activity implements OnNavigationListener { 12 /** Called when the activity is first created. */ 13 @Override 14 public void oncreate(bundle savedinstancestate) { 15 super.oncreate(savedinstancestate); 16 setcontentview(r.layout.main); 17 18 ActionBar ab = getactionbar(); 19 ab.setnavigationmode(actionbar.navigation_mode_list); 20 SpinnerAdapter sa = ArrayAdapter.createFromResource(this, 21 R.array.list, android.r.layout.simple_spinner_dropdown_item); 22 23 ab.setlistnavigationcallbacks(sa, this); 24 } 25 26 public boolean onnavigationitemselected(int itemposition, long itemid) { 27 Fragment frag1 = new FragmentOne(); 28 Fragment frag2 = new FragmentTwo(); 29 30 getfragmentmanager() 31.beginTransaction() 32.replace(R.id.fragment, itemposition == 0? frag1 : frag2) 33.commit(); 34 return true; 35 } 36 } 46
47
1 2 3 4 5 6 7 48