160 likes | 389 Views
Android Application Step by Step Eleonora Todorova Boyan Iliev. Mobile Devices Today. Android Evolution. Android in a Glance Android SDK Dalvik Eclipse vs. Android Studio ADT Debug & Testing Tools DDMS Open Source Libraries . Android Step by Step. Android Step by Step
E N D
Android Application Step by Step EleonoraTodorova BoyanIliev
Android in a Glance • Android SDK • Dalvik • Eclipse vs. Android Studio • ADT • Debug & Testing Tools • DDMS • Open Source Libraries
Android Step by Step
Android Step by Step • Create an Android Project
<?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> Android Step by Step
public class LabsAppextendsApplication{ private static GsonsGson; private static RequestQueuesRequestQueue; private static ImageLoadersImageLoader; privateImageLoader.ImageCachemImageCache; privateImageLoader.ImageCachemDiskCache; @Override public void onCreate() { super.onCreate(); // init everything sRequestQueue= Volley.newRequestQueue(getApplicationContext()); sGson= new Gson(); // super simple image cache mImageCache = newImageLoader.ImageCache() { private final LruCache<String, Bitmap> mCache= newLruCache<String, Bitmap>(10); @Override public void putBitmap(String url, Bitmap bitmap) {..} @Override publicBitmap getBitmap(String url) {...} }; mDiskCache = newImageLoader.ImageCache() { private final DiskBasedCachemCache = newiskBasedCache(getCacheDir(), Constants.DEFAULT_MAX_CACHE_LIMIT); @Override public void putBitmap(String url, Bitmap bitmap) {...} @Override publicBitmap getBitmap(String url) {... returngetBitmapFromBytes(entry.data); } // can't be simpler than this sImageLoader = newImageLoader(sRequestQueue, Constants.USE_DISK_CACHE ? mDiskCache : mImageCache); } public static ImageLoadergetImageLoader() {...} public static RequestQueuegetRequestQueue() {...} public static GsongetGsonInstance() {...} } Android Step by Step • Application class & App start point
publicclassMainActivityextendsActivity { privateListViewmListView; protectedGalleryAdaptermAdapter; privateRequestQueuemQueue; privateMenuItemrefreshItem; @Override protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_volley); mQueue = LabsApp.getRequestQueue(); mListView = (ListView) findViewById(android.R.id.list); refreshData(); } privatevoidrefreshData() {...} publicvoidonStop() { super.onStop(); mQueue.cancelAll(Constants.TAG_IMAGE_REQUEST); } @Override publicbooleanonCreateOptionsMenu(Menumenu) { getMenuInflater().inflate(R.menu.volley, menu); returntrue; } @Override publicbooleanonOptionsItemSelected(MenuItemitem) { switch (item.getItemId()) { caseR.id.action_refresh: refreshItem = item; refresh(); break; default: break; } returntrue; }} Android Step by Step
public class ListAdapter extends BaseAdapter { private ContextmContext; 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; ViewHolderviewHolder; if(v == null){ LayoutInflaterinflater = (LayoutInflater) getContext(). getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.list_item_photo_gallery, null, false); viewHolder = newViewHolder(); 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(newOnClickListener() {…}); return v; } public void requestImage(finalImageViewniv, final String imgUrl) {...} private booleanshouldLoadData(long position) {...} } Android Step by Step
<?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 */ LayoutInflaterinflater = (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); } } Android Step by Step • Refresh Button Animation
v.setOnClickListener(new OnClickListener() { @Override public 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 DetailsActivityextendsActivity { privateNetworkImageViewivImage; privateProgressBarmLoadingBar; @Override protected 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()); } } Android Step by Step • Navigate to the Second screen
Android Step by Step • Add Progress loader <?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns: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>