Fragment
● Tablet and phone code comes together
● One ring to rule them all!!!
Fragment
Define in XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
Set Content in code
setContentView(R.layout.fragment_layout);
It works just like activity
public static class TitlesFragment extends ListFragment {
Create Fragment
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
Fire the fragment
// Instantiate a new fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
Get the parameters
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
mLabel = args.getCharSequence("label", mLabel);
}
}
ViewPagerIndicator
@Java
//Set the pager with an adapter
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(new TestAdapter(getSupportFragmentManager()));
//Bind the title indicator to the adapter
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles);
titleIndicator.setViewPager(pager);
titleIndicator.setOnPageChangeListener(mPageChangeListener);
Send Parameter
● Bundle
Send
Intent data = new Intent("my.action");
data.putExtra("point", point);
data.putExtra("noqs", noqs);
Get
Bundle b = getIntent().getExtras();
noqs = b.getInt("noqs");
point = b.getInt("point");
Send Parameter
Parcelable
protected static class Foo implements Parcelable {
public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
public Foo createFromParcel(Parcel source) {
final Foo f = new Foo();
f.str = (String) source.readValue(Foo.class.getClassLoader());
return f;
}
public Foo[] newArray(int size) {
throw new UnsupportedOperationException();
}
};
Do not use px
● Use dp and sp
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clickme"
android:layout_marginTop="20dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
AsyncTask
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
new DownloadFilesTask().execute(url1, url2, url3);
new DownloadFilesTask().execute(url1, url2, url3);
HTTP Libraries
You can use Apache commons
Or I found these libraries well
http://loopj.com/android-async-http/
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
});
This works as well
http://code.google.com/p/android-query/
WTF is DI
● Before DI ● After DI
class AndroidWay extends Activity { class RoboWay extends RoboActivity {
@InjectView(R.id.name) TextView
TextView name;
name;
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle
super.onCreate(savedInstanceState);
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(R.layout.main);
name = (TextView) findViewById(R.id.name); name.setText( "Hello, " + myName );
} }
} }
Image Caching
● Google I/O 2012 app has a great image
caching library
http://code.google.com/p/iosched/
Open Source apps
● Google I/O 2012
http://code.google.com/p/iosched/
● Github app
https://github.com/github/android
ListView
Most important widget
Fast ListView Tips
● Inflate once
● ViewHolder
● Use Tag
● Don't write a heavy code like network operation in getView
method
http://yekmer.posterous.com/fast-listview-scrolling-in-android