Feb 18, 2014

fragment



fragment is for use  many kinds of view in one activity.

detail...

This is a useful for wide display device similar tablet.

So I make a simple code.

Left of display is icon for move to website.

Right of display is web browser.

If you touched icon, browser is moved.




activity_main.xml

  1.      <fragment
  2.         android:id="@+id/fragment1"
  3.         android:name="com.answerofgod.fragment.Fragment1"
  4.         android:layout_width="wrap_content"
  5.         android:layout_height="match_parent"
  6.         android:layout_weight="1" />
  7.     <fragment
  8.        android:id="@+id/fragment2"
  9.        android:name="com.answerofgod.fragment.Fragment2"
  10.        android:layout_width="wrap_content"
  11.        android:layout_height="match_parent"
  12.        android:layout_weight="20" />
I added 'fragment' tag.

Number of 'fragment' tag is number of view.

Name means Java file below.

Layout_weight is range of view.

Fragment1.java
  1.  public class Fragment1 extends Fragment {
  2.    
  3.     @Override
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  5.             Bundle savedInstanceState) {
  6.        
  7.         return inflater.inflate(R.layout.frag1,container,false);
  8.     }
  9. }
This class inflate frag1.xml file.

'Fragment ' java file just to define of xml file(layout).

frag1.xml
  1.     <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.  xmlns:android="http://schemas.android.com/apk/res/android"
  4.  android:orientation="vertical"
  5.  android:layout_width="match_parent"
  6.  android:layout_height="match_parent">
  7.    
  8.   <Button
  9.     android:id="@+id/daum"
  10.     android:background="@drawable/daum"
  11.     android:gravity="center"
  12.     android:layout_width="50dp"
  13.     android:layout_height="50dp"/>

  14. ......
  15. </LinearLayout>
This is left of display for icon.

Fragment2.java

  1. public class Fragment2 extends Fragment {
  2.    
  3.     @Override
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  5.             Bundle savedInstanceState) {
  6.        
  7.         return inflater.inflate(R.layout.frag2,container,false);
  8.     }
  9. }
This file inflate frag2.xml file.

frag2.xml
  1. <LinearLayout
  2.  xmlns:android="http://schemas.android.com/apk/res/android"
  3.  android:orientation="vertical"
  4.  android:background="#ff00ff00"
  5.  android:layout_width="match_parent"
  6.  android:layout_height="match_parent">
  7.   <WebView
  8.     android:layout_width="match_parent"
  9.     android:layout_height="match_parent"
  10.     android:gravity="center"
  11.     android:id="@+id/webview"/>
  12. </LinearLayout>
This is left of display for web browser.

MainActivity.java

  1. public class MainActivity extends Activity implements OnClickListener{
  2.     WebView webview;
  3.     Button daum;
  4.     Button naver;
  5.     Button facebook;
  6.     Button twit;
  7.     String url="http://m.facebook.com";
  8.   
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.        
  14.         webview=(WebView)findViewById(R.id.webview);
  15.         daum=(Button)findViewById(R.id.daum);


  16. ..........       
  17.     }
  18.    
  19.     @Override
  20.     public void onClick(View v) {
  21.         // TODO Auto-generated method stub
  22.         switch(v.getId()){
  23.         case R.id.daum:
  24.             url="http://m.daum.net";
  25.             webview.loadUrl(url);
  26.             break;

  27.         .........
  28.   .........
This file is  to define all for app.

Function, define, casting.... all in this file.









No comments:

Post a Comment