Android Application Step by Step Eleonora Todorova Boyan Iliev
description
Transcript of Android Application Step by Step Eleonora Todorova Boyan Iliev
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 2
Android ApplicationStep by StepEleonora Todorova
Boyan Iliev
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 3
Mobile Devices Today
80%
14%
4% 2%
AndroidiOSWindows Phone BlackBerryOther
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 4
Android Evolution
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 5
Android in a Glance• Android SDK
• Dalvik
• Eclipse vs. Android Studio
• ADT
• Debug & Testing Tools
• DDMS
• Open Source Libraries
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 6
Android Step by Step
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 7
Android Step by Step• Create an Android Project
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 8
Android Step by StepProject Archite
cture
Manifest File
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mentormate.java2days" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mentormate.java2days.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN”/>
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 10
Android Step by Step• Application class
&
App start point
Application class
public class LabsApp extends Application { private static Gson sGson; private static RequestQueue sRequestQueue; private static ImageLoader sImageLoader; private ImageLoader.ImageCache mImageCache; private ImageLoader.ImageCache mDiskCache;
@Override public void onCreate() { super.onCreate(); // init everything sRequestQueue = Volley.newRequestQueue(getApplicationContext()); sGson = new Gson();
// super simple image cache mImageCache = new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10); @Override public void putBitmap(String url, Bitmap bitmap) {..} @Override public Bitmap getBitmap(String url) {...} };
mDiskCache = new ImageLoader.ImageCache() { private final DiskBasedCache mCache = new iskBasedCache(getCacheDir(), Constants.DEFAULT_MAX_CACHE_LIMIT);
@Override public void putBitmap(String url, Bitmap bitmap) {...} @Override public Bitmap getBitmap(String url) {... return getBitmapFromBytes(entry.data); }
// can't be simpler than this sImageLoader = new ImageLoader(sRequestQueue, Constants.USE_DISK_CACHE ? mDiskCache : mImageCache); }
public static ImageLoader getImageLoader() {...}
public static RequestQueue getRequestQueue() {...}
public static Gson getGsonInstance() {...}}
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 12
Android Step by StepList
View MainAc
tivity public class MainActivity extends Activity {
private ListView mListView;protected GalleryAdapter mAdapter;private RequestQueue mQueue;private MenuItem refreshItem;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_volley);
mQueue = LabsApp.getRequestQueue();mListView = (ListView) findViewById(android.R.id.list);
refreshData();
}
private void refreshData() {...}
public void onStop() {super.onStop();mQueue.cancelAll(Constants.TAG_IMAGE_REQUEST);
}
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.volley, menu);return true;
}
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {case R.id.action_refresh:
refreshItem = item;refresh();break;
default:break;
}return true;
} }
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 13
Android Step by StepList
View ListAda
pter public class ListAdapter extends BaseAdapter {
private Context mContext; private List<Result> mOriginalLis;...t public ListAdapter(Context context, List<Result> list) {...} public void loadMoreData() {...}
@Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder viewHolder; if(v == null){
LayoutInflater inflater = (LayoutInflater) getContext(). getSystemService(Context.LAYOUT_INFLATER_SERVICE);v = inflater.inflate(R.layout.list_item_photo_gallery, null, false);viewHolder = new ViewHolder();
if (v != null) { viewHolder.thumbnail = (NetworkImageView) v.findViewById(R.id.thumbnail); viewHolder.description = (TextView) v.findViewById(R.id.description); v.setTag(viewHolder); } } else {
viewHolder = (ViewHolder) v.getTag(); } final Result result = mOriginalList.get(position); viewHolder.thumbnail.setDefaultImageResId(android.R.drawable.ic_menu_gallery); viewHolder.thumbnail.setErrorImageResId(android.R.drawable.ic_menu_delete); if (result != null) { if (Constants.USER_NETWORK_IMAGE_VIEWS) { viewHolder.thumbnail.setImageUrl(result.getTbUrl(), LabsApp.getImageLoader()); } else { requestImage(viewHolder.thumbnail, result.getTbUrl()); } } viewHolder.description.setText(result.getTitleNoFormatting()); if(closeToEnd(position)) { loadMoreData(); } v.setOnClickListener(new OnClickListener() {…}); return v; } public void requestImage(final ImageView niv, final String imgUrl) {...} private boolean shouldLoadData(long position) {...}}
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 14
Android Step by Step• Refresh Button
Animation
Animation <?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" />
private void startAnimation() {/* Attach a rotating ImageView to the refresh item as an ActionView */LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);ImageView iv = (ImageView) inflater
.inflate(layout.action_refresh, null);
Animation rotation = AnimationUtils.loadAnimation(this,anim.refresh_rotate);
rotation.setRepeatCount(Animation.INFINITE);iv.startAnimation(rotation);
if(refreshItem != null && iv != null)refreshItem.setActionView(iv);
}
private void stopAnimation() {if (refreshItem != null && refreshItem.getActionView() != null) {
refreshItem.getActionView().clearAnimation();refreshItem.setActionView(null);
}}
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 15
Android Step by Step• Navigate to the
Second screen
DetailsActivity
v.setOnClickListener(new OnClickListener() {
@Overridepublic void onClick(View v) {
Intent intent = new Intent(mContext, DetailsActivity.class);intent.putExtra("image_url", result.getUrl());mContext.startActivity(intent);
}});
/** * * @author MentorMate LLC. 2013 * */public class DetailsActivity extends Activity {
private NetworkImageView ivImage;private ProgressBar mLoadingBar;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_photo_details);
mLoadingBar = (ProgressBar) findViewById(R.id.loading);
ivImage = (NetworkImageView) findViewById(R.id.image);ivImage.setImageUrl(getIntent().getExtras().getString("image_url"),
LabsApp.getImageLoader());}
}
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 16
Android Step by Step• Add
Progress loader
Details layout
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<ProgressBar android:id="@+id/loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" />
<com.android.volley.toolbox.NetworkImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:adjustViewBounds="true" android:scaleType="fitCenter" />
</RelativeLayout>
2 0 1 3 M E N T O R M A T E , L L C . A L L R I G H T S R E S E R V E D . 18
QUESTIONS
Thank you!