Quick watch on SwipeRefreshLayout (Tutorial)

7:02 PM 1 Comments


After getting update in android support v4, I thought lets check it out. And believe me it is very light and very simple to implement. First of all update Android Support Library from SDK manager. The SwipeRefreshLayout works with Vertical swipe gesture. Lets go through tutorial. Please dont go through coding standards because this is not deep dive ;). 




Create xml with SwipeRefreshLayout.

<android.support.v4.widget.SwipeRefreshLayout 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" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

Create MainActivity.java

public class MainActivity extends ActionBarActivity {

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

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 /**
  * A place holder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  private SwipeRefreshLayout layout;
  String[] values = new String[] { "Android", "iPhone", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  ArrayList<String> list = new ArrayList<String>();

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   
   layout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_main, container, false);
   final ListView listView = (ListView) layout.findViewById(R.id.listview);
   
   for (int i = 0; i < values.length; i++) {
    list.add(values[i]);
   }
   final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
   listView.setAdapter(adapter);
   
   layout.setColorScheme(android.R.color.holo_green_dark, android.R.color.holo_orange_dark, android.R.color.holo_blue_dark,
     android.R.color.holo_red_dark);
   layout.setOnRefreshListener(new OnRefreshListener() {

    @Override
    public void onRefresh() {
     Log.e(getClass().getSimpleName(), "refresh");
     for (int i = 0; i < values.length; i++) {
      list.add(values[i]);
     }
     adapter.notifyDataSetInvalidated();
     new GetLinks().execute();
    }
   });
   return layout;
  }

  public class GetLinks extends AsyncTask<Void, Void, Void> {

   @Override
   protected void onPreExecute() {
    super.onPreExecute();

   }

   @Override
   protected Void doInBackground(Void... params) {
    try {
     Thread.sleep(5000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    return null;
   }

   @Override
   protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    layout.setRefreshing(false);
   }
  }

 }

}

Note: This component should be parent view and support only one child.

SourceCode : SwipeRefreshLayout

 

 Reference : SwipeRefreshLayout on devloper site


1 comments:

Mastering ProGuard with ProGuard GUI - Part 1 (ProGuard with JAR)

8:39 PM 3 Comments





ProGuard is a nice tool that is used to shrink, obfuscate, and optimize Java code. It is part of Android SDK and very useful when applied correctly. ProGuard provides four main features:
  1. Shrinking
  2. Obfuscation
  3. Repackaging
  4. Optimization
First of all, Download ProGuard from this link : ProGuard. It contains proguardgui.jar in lib folder. Run that jar and ProGuard GUI popup. Follow below steps to apply ProGuard to JAR.

  1. Go to Input/Output:Add JAR file using Add input and specify output folder using Add output.
    New JAR will be placed in Output folder. ProGuard uses it to reconstruct the class dependencies that are necessary for proper processing. The library JAR itself always remain unchanged.

  2. Go to Shrinking:It is necessary to reduce the size of APK. It can be more advantageous because large binaries are less likely to be installed in poor network conditions.
    Shrink collects all unused code and it is possible to list all that unused code to file by providing file path in "Print Usage" section.
  3. Go to Obfuscation:Today, there are plenty of tools available to extract the contents of APKs. It is important to obfuscate to protect the code base. ProGuard generates a mapping file that allows to map the stack traces of obfuscated code to actual code. To get mapping file tick "Print mapping" and give path where file will be dumped."Apply Mapping" is useful to reuse the given name mapping that was printed out in a previous obfuscation run of ProGuard. Classes and class members that are listed in the mapping file receive the names specified along with them.

    "Keep package names" is used to keep the given package name from being obfuscated. Give multiple package name with coma separation.

    "Use mixed-case class names" is useful to generate mixed-case class names while obfuscating.

    "Keep additional class names and class members names" is very important to keep classes from being obfuscated,classes like models, interfaces etc. To keep methods out of Obfuscation, add method in added class with valid access modifiers. For example, if you specify public methods, Public methods would not obfuscate by ProGuard.If method name, return type or argument type is not specify, ProGuard will keep all public methods from obfuscation. 



    "Repackage classes" allows ProGuard to take externals  jar and class files and move them to a single package location. For building libraries, it is very helpful to choose simple interface to third party developers when maintaining well structured project in the repository.

  4. Go to Optimization: It is not recommended to use it because of some dalvik version issues and by default Android tool has optimization turned off in ProGuard.

  5. Go to Information: set target to java version used. Tick verbose to write out more information during process. If execution terminated with exception,  this option will print out the entire stack trace instead of just exception message."Print configuration" is useful to write out the entire configuration that has been parsed, with included files and replaced variables. It will generate file in output folder.

  6. Go to Process: It has an output console for displaying the configuration and the messages while processing. Click on Process and it executes ProGuard with the current configuration. New JAR will be placed in folder which is specified at Input/Output section.

Reference : http://proguard.sourceforge.net/

3 comments: