Search This Blog

Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Wednesday, February 22, 2012

Porting Activity to Fragment

Oldman : "Now for Honeycomb and above phones or tablets, The Activity design method has been replace with Fragment. I spent many days learned how to porting the old Activity method source code to new Fragment. Hope this can be useful for the others... "

1.Replace main menu Activity with FragmentActivity (This is mean that I'm using Android "support libraries" instead of real Fragment api. That because I need to back porting this to running on the old devices also)

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  
   
}

 Yes, there is not much things inside MainActivity class but just calling setContentView method to set view for R.layout.main which is ...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <fragment class="com.myapp.TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</FrameLayout>
At res/layout/main.xml and ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment class="com.myapp.TitlesFragment"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <FrameLayout android:id="@+id/details"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
 </LinearLayout>
At res/layout-land/main.xml

2.Create TitlesFragment class (just like they described here)

3.Change others Activity type to Fragment type. Here is somethings you need to change ...
   - onBackPressed() method must be remove.
   - Add onCreateView() method. Move all code in onCreate() method to onCreateView() method.
   - Change 'this.' to 'this.getActivity().'
   - To access view components, use 'View view = inflater.inflate(R.layout.find_scanner_view, container, false);'
   - Be careful about this.getActivity().finish(); because in this case it means go finish the application.
   - They said to using onSaveInstanceState() method to saving UI state but it seems not working probably what I expected it will. So I using SharedPreferences to save UI state and restoring them at onCreateView() method. 
   - Add xxxFragmentActivity inside the Fragment class...

public class BFragment extends Fragment implements OnClickListener,Runnable{
   ....

  public static class BFragmentActivity extends FragmentActivity {
     ....
  }

}
4. Connect to converted Fragment classes through FragmentManager (learn from the example)
5. To sending result between Fragment classes and TitlesFragment class, just override setTargetFragment() method and calling it to passing caller Fragment reference.

     static Fragment targetFragment;
     static int targetRequestCode;
     @Override
     public void setTargetFragment(Fragment fragment, int requestCode){
         super.setTargetFragment(fragment, requestCode);
         targetFragment=fragment;
         targetRequestCode=requestCode;
     }

OK, This is just the point of view. In details, May be you need more homework to do. For my opinion, I think to making new app with Fragment should be a better choice than trying to porting old app to it ... it was painful job ...

Cheers,

Thursday, January 5, 2012

Add more languages support directly into .apk file

Oldman : "Recently my boss wanted me to add more languages support directly into my Android application .apk file without using my sourcecode. I don't know why he need that but I found the way to do this."

1.Go and install android-apktool
http://code.google.com/p/android-apktool/

2.Using apktool to decode the orignal .apk file (the file must not be sign yet or it will broke and can not be install later)

# apktool if xxxx.apk
# apktool d xxxx.apk

3.then xxxx folder was created. inside them place res/values-zz/strings.xml file where zz is your addition language (ex: ja for japanese, it for italy)

4. rebuild the resource.

# apktool b ./xxxx/ xxxx-new.apk

5. then open xxxx-new.apk and xxxx.apk with zip manager program(or something like that) drag resource.arse file from xxxx-new.apk and drop into xxxx.apk 

6. sign xxxx.apk with jarsigner and your key. Then your xxxx.apk file can be install and should be supporting for new languages....
http://developer.android.com/guide/publishing/app-signing.html

Note: I did this on my Debian x86 machine.

Thursday, August 4, 2011

How to make app show full-screen on Galaxy Tab

Old man : "Hi all, Recently I just started to learning about how to development Android app on my Galaxy Tab. I learned a lot of thing and found one thing interesting . If you have source code that doest not support for big screen (just like tablet). May be we can make it to showing in full-screen on your device ..."

      original app just displays like small-size screen

1. At AndroidManifest.xml file, add these two line
<uses-sdk android:minSdkVersion="8" />
    <supports-screens android:largeScreens="true" android:anyDensity="true"/>

2. Clean you project and rebuild it. Then download .apk on your device.

3. If nothing wrong. This time your app should display with full-screen function on the device.
 Wow !!
Ref: I using Min3D for an example...
http://code.google.com/p/min3d/