User Tools

Site Tools


android-labs-s14:class-05

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
android-labs-s14:class-05 [2014/02/17 12:49]
prakhar created
android-labs-s14:class-05 [2014/02/19 03:00] (current)
prakhar
Line 1: Line 1:
 ====== Class 05 ====== ====== Class 05 ======
  
-In this class, we'll be learning about+In this class, we'll be learning about **Fragments**.
-  ​* Fragments +
-  ​Canvas, Bitmap, Paint, Path +
-  ​Context Menu+
  
 ---- ----
Line 18: Line 15:
   * You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. ​   * You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. ​
 {{ :​android-labs-s14:​04_fragments.png?​direct |}} {{ :​android-labs-s14:​04_fragments.png?​direct |}}
 +(Image source: developer.android.com)
   * You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity"​ that you can reuse in different activities).   * You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity"​ that you can reuse in different activities).
 {{ :​android-labs-s14:​04_fragment_lifecycle.png?​direct |}} {{ :​android-labs-s14:​04_fragment_lifecycle.png?​direct |}}
 +(Image source: developer.android.com)
  
 ==== Exercise ==== ==== Exercise ====
Line 316: Line 315:
 ==== Source Code ==== ==== Source Code ====
 Available at [[https://​github.com/​panwaria/​LearningFragments|Github]]. Available at [[https://​github.com/​panwaria/​LearningFragments|Github]].
- 
----- 
- 
-===== Demo-02 ===== 
-We'll see a small demo of the app we're going to build next. 
- 
-===== Adding a drawing board to the app ===== 
- 
-==== Exercise ==== 
- 
-=== Step 01: Create a place for the Canvas in FragmentTwo === 
-If user clicks on '​Create New' Button on the Main Screen, then fragment_02 should look something like this: 
-{{ :​android-labs-s14:​05_board_create_new_tablet.png?​direct&​600 |}} 
- 
-As you can see, we have a textbox to put in a drawing title, with a button '​Save'​ next to it. We can club this ''​EditText''​ and ''​Button''​ in a ''​RelativeLayout''​. Also, we need to provide some space to put the canvas (we'll name it ''​DrawingView''​) below, which we'll be creating programatically. To do this we can have following layout in ''​fragment_02.xml'':​ 
-<code xml fragment_02.xml>​ 
-<?xml version="​1.0"​ encoding="​utf-8"?>​ 
- 
-<​LinearLayout xmlns:​android="​http://​schemas.android.com/​apk/​res/​android"​ 
-    android:​layout_width="​match_parent"​ 
-    android:​layout_height="​match_parent"​ 
-    android:​orientation="​vertical"​ > 
- 
-    <​FrameLayout 
-        android:​layout_width="​match_parent"​ 
-        android:​layout_height="​match_parent"​ > 
- 
-        <!-- Welcome Title --> 
-        <​TextView 
-            android:​id="​@+id/​textview_welcome"​ 
-            android:​layout_width="​match_parent"​ 
-            android:​layout_height="​match_parent"​ 
-            android:​gravity="​center"​ 
-            android:​text="​@string/​two_welcome"​ 
-            android:​textSize="​24sp"​ 
-            android:​textStyle="​bold"​ /> 
- 
-        <!-- Outer Layout for Canvas --> 
-        <​LinearLayout 
-            android:​id="​@+id/​layout_drawing"​ 
-            android:​layout_width="​fill_parent"​ 
-            android:​layout_height="​wrap_content"​ 
-            android:​layout_marginBottom="​5dip"​ 
-            android:​orientation="​vertical"​ 
-            android:​visibility="​gone"​ > 
- 
-            <​RelativeLayout 
-                android:​layout_width="​fill_parent"​ 
-                android:​layout_height="​wrap_content"​ 
-                android:​layout_marginLeft="​@dimen/​activity_horizontal_margin"​ 
-                android:​layout_marginRight="​@dimen/​activity_horizontal_margin"​ > 
- 
-                <​EditText 
-                    android:​id="​@+id/​edittext_drawing"​ 
-                    android:​layout_width="​wrap_content"​ 
-                    android:​layout_height="​wrap_content"​ 
-                    android:​layout_alignParentLeft="​true"​ 
-                    android:​layout_toLeftOf="​@+id/​btn_two_save"​ 
-                    android:​hint="​@string/​two_drawing"​ /> 
- 
-                <Button 
-                    android:​id="​@id/​btn_two_save"​ 
-                    android:​layout_width="​100dp"​ 
-                    android:​layout_height="​wrap_content"​ 
-                    android:​layout_alignParentRight="​true"​ 
-                    android:​onClick="​onButtonClick"​ 
-                    android:​text="​Save"​ /> 
-            </​RelativeLayout>​ 
- 
-            <!-- We will PROGOMATICALLY insert Canvas here --> 
- 
-        </​LinearLayout>​ 
-    </​FrameLayout>​ 
- 
-</​LinearLayout>​ 
-</​code>​ 
- 
-=== Step 02: Adding **DrawingView** === 
- 
-Before we implement the click callback of '​Create New' or 'Open Existing'​ buttons, let's create a class which defines the DrawingView,​ on which our app user can draw as on a painting board. For that, add a class named ''​DrawingView.java''​ (extending ''​View''​) inside ''​src/​com.example.board''​ package name with the following methods implemented. 
-<code java DrawingView.java>​ 
-/** 
- * Class representing a View on which one can draw like a paint board. 
- */ 
-public class DrawingView extends View  
-{ 
- private Bitmap mBitmap; // To hold the pixels 
- private Canvas mCanvas; // To host '​draw'​ calls 
- private Path mPath; // A drawing primitive used in this case 
- private Paint mPaint; // To define colors and styles of drawing 
- private ArrayList<​Float>​ mPartsDrawingList;​ //​ To store individual lines 
- public ArrayList<​ArrayList<​Float>>​ mOverallDrawingList;​ //​ To store overall drawing ​ 
- private float mX, mY; // Current location 
- 
- public DrawingView(Context c)  
- { 
- super(c); 
-  
- // Set Drawing Paint Attributes 
- setPaint();​ 
-  
- mBitmap = Bitmap.createBitmap(400,​ 580, Bitmap.Config.ARGB_8888);​ // '​ARGB_8888'​ => Each pixel is stored on 4 bytes. 
- mCanvas = new Canvas(mBitmap);​ 
- mPath = new Path(); 
- 
- mOverallDrawingList = new ArrayList<​ArrayList<​Float>>​();​  
- } 
-  
- /** 
- * Method to set Paint attributes 
- */ 
- private void setPaint() ​ 
- { 
- mPaint = new Paint(); 
-  
- // Setting different properties of Paint object. Feel free to play with these. 
- mPaint.setAntiAlias(true);​ 
- mPaint.setDither(true);​ 
- mPaint.setColor(0xFFFFFFFF);​ // White 
- mPaint.setStyle(Paint.Style.STROKE);​ 
- mPaint.setStrokeJoin(Paint.Join.ROUND);​ 
- mPaint.setStrokeCap(Paint.Cap.ROUND);​ 
- mPaint.setStrokeWidth(8);​ 
- } 
-  
- /** 
- * Method to set overall drawing list to a given list. 
- * @param drawingList Given drawing list 
- */ 
- public void setOverallDrawingList(ArrayList<​ArrayList<​Float>>​ drawingList) 
- { 
- mOverallDrawingList = drawingList;​ 
- } 
-  
- @Override 
- protected void onDraw(Canvas canvas) ​ 
- { 
- canvas.drawBitmap(mBitmap,​ 0, 0, null); 
- canvas.drawPath(mPath,​ mPaint); 
- }  
-} 
-</​code>​ 
- 
-Let's learn a little more about Canvas. 
-== Canvas == 
-A ''​Canvas''​ works for you as a pretense, or interface, to the actual surface upon which your graphics will be drawn — it holds all of your "​draw"​ calls. Via the Canvas, your drawing is actually performed upon an underlying ''​Bitmap'',​ which is placed into the window. In the event that you're drawing within the onDraw() callback method, the Canvas is provided for you and you need only place your drawing calls upon it.  If you need to create a new Canvas, then you must define the Bitmap upon which drawing will actually be performed. The Bitmap is always required for a Canvas. 
- 
-So, to draw something, you need 4 basic components: ​ 
-  - a Bitmap to hold the pixels 
-  - a Canvas to host the draw calls (writing into the bitmap) 
-  - a drawing primitive (e.g. Rect, Path, text, Bitmap) 
-  - a Paint to describe the colors and styles for the drawing. 
- 
-Cool! So, now is the time to add a touch element to the ''​DrawingView''​. ​ 
- 
-To handle touch screen motion events for any ''​View'',​ we can implement ''​onTouchEvent()''​ method. We can detect different kind of motions using the value of ''​MotionEvent''​ object, which is passed as the argument value in the ''​onTouchEvent()''​ method whenever any touch gesture takes place over that View. To handle touch gestures on ''​DrawingView'',​ we can implement following methods in this class: 
-<code java DrawingView.java>​ 
- @Override 
- public boolean onTouchEvent(MotionEvent event) 
- { 
- float x = event.getX();​ 
- float y = event.getY();​ 
- 
- switch (event.getAction()) ​ 
- { 
- case MotionEvent.ACTION_DOWN:​ 
- 
- touch_start(x,​ y); 
- invalidate();​ 
- break; 
-  
- case MotionEvent.ACTION_MOVE:​ 
-  
- touch_move(x,​ y); 
- invalidate();​ 
- break; 
-  
- case MotionEvent.ACTION_UP:​ 
-  
- touch_up();​ 
- invalidate();​ 
- break; 
- } 
- return true; 
- } 
-  
- /** 
- * Callback for the case when pressed gesture has started. ​ 
- 
- * @param x Inital Starting loc X 
- * @param y Inital Starting loc Y 
- */ 
- private void touch_start(float x, float y) 
- {  
- mPath.reset();​ 
- mPath.moveTo(x,​ y); 
- mX = x; 
- mY = y; 
- 
- mPartsDrawingList = new ArrayList<​Float>​();​ 
- mPartsDrawingList.add(mX);​ 
- mPartsDrawingList.add(mY);​ 
- } 
- 
- private static final float TOUCH_TOLERANCE = 0; 
- 
- /** 
- * Callback for the case when a change happens during a press gesture 
- 
- * @param x Most recent point X 
- * @param y Most recent point Y 
- */ 
- private void touch_move(float x, float y)  
- { 
- float dx = Math.abs(x - mX); 
- float dy = Math.abs(y - mY); 
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
- mPath.quadTo(mX,​ mY, (x + mX) / 2, (y + mY) / 2); 
- mX = x; 
- mY = y; 
- } 
- 
- mPartsDrawingList.add(mX);​ 
- mPartsDrawingList.add(mY);​ 
- } 
- 
- /** 
- * Callback for the case when pressed gesture is finished. 
- */ 
- private void touch_up() ​ 
- { 
- mPath.lineTo(mX,​ mY); 
-  
- // Commit the path to our offscreen 
- mCanvas.drawPath(mPath,​ mPaint); 
-  
- // Reset path so we don't double draw 
- mPath.reset();​ 
- 
- mOverallDrawingList.add(mPartsDrawingList);​ 
- } 
- 
-</​code>​ 
- 
-There may be a case when ''​Visibility''​ or the ''​Size''​ of the ''​DrawingView''​ changes, may be because of the change in device orientation or change in the visibility of an ancestor of the ''​DrawingView''​. To handle such cases, let's implement following fallback methods in the class as well: 
-<code java DrawingView.java>​ 
- @Override 
- protected void onSizeChanged(int w, int h, int oldw, int oldh) 
- { 
- super.onSizeChanged(w,​ h, oldw, oldh); 
- 
- mBitmap = Bitmap.createBitmap(w,​ h, Bitmap.Config.ARGB_8888);​ 
- mCanvas = new Canvas(mBitmap);​ 
- redrawPath(mCanvas);​ 
- } 
- 
- @Override 
- protected void onVisibilityChanged(View changedView,​ int visibility) 
- { 
- super.onVisibilityChanged(changedView,​ visibility);​ 
- 
- if (visibility == View.VISIBLE) 
- { 
- if (mBitmap != null && mCanvas != null) 
- { 
- redrawPath(mCanvas);​ 
- invalidate();​ 
- } 
- } 
- } 
- 
- /* 
- * Helper method to redraw the entire path again on Canvas. 
- */ 
- private void redrawPath(Canvas canvas) 
- { 
- int numLines = mOverallDrawingList.size();​ 
- for (int i = 0; i < numLines; i++) 
- { 
- // Un-flatten the drawing points 
- ArrayList<​Float>​ partDrawingList = mOverallDrawingList.get(i);​ 
- touch_start(partDrawingList.get(0),​ partDrawingList.get(1));​ 
- 
- int partDrawingListSize = partDrawingList.size();​ 
- 
- for (int j = 2; j < partDrawingListSize;​ j += 2) 
- { 
- // Simulate the move gestures 
- touch_move(partDrawingList.get(j),​ partDrawingList.get(j + 1)); 
- } 
- 
- mPath.lineTo(mX,​ mY); 
- 
- // Commit the path to our offscreen 
- canvas.drawPath(mPath,​ mPaint); 
- 
- // Reset path so we don't double draw 
- mPath.reset();​ 
- } 
- } 
-</​code>​ 
- 
- 
-=== Step 03: [TABLETS] Create New Drawing Board === 
- 
-In this step, we want to bring up a clean drawing board whenever user clicks on '​Create New' button. ​ 
-  * So, firstly, search for a background .png (or .jpg) image  of a chalkboard, or you can download it from [[https://​github.com/​panwaria/​Board/​blob/​master/​res/​drawable/​chalkboard.png|here]]. Rename it to ''​chalkboard.png''​ (or ''​chalkboard.jpg''​),​ if not already. 
-  * Create a folder named ''​drawable''​ inside ''​res''​ folder, and copy the downloaded file to that folder. 
-  * To add ''​DrawingView''​ to FragmentTwo,​ we'll implement a method ''​createNewBoard()''​ in ''​FragmentTwo''​ class, which will be called when user clicks on '​Create New' button. 
-<code java FragmentTwo.java>​ 
- private DrawingView mDrawingView = null; 
-  
- /** 
- * Method to add a new DrawingView to the fragment. ​ 
- */ 
- public void createNewBoard() 
- { 
- // Removing any existing DrawingView 
- cleanUpExistingView();​ 
- 
- // Making the drawing layout visible 
- Activity parentActivity = getActivity();​ 
- LinearLayout drawingLayout = (LinearLayout) parentActivity.findViewById(R.id.layout_drawing);​ 
- parentActivity.findViewById(R.id.textview_welcome).setVisibility(View.GONE);​ 
- drawingLayout.setVisibility(View.VISIBLE);​ 
-  
- // Adding DrawingView to the DrawingLayout 
- mDrawingView = new DrawingView(parentActivity);​ 
- mDrawingView.setBackgroundResource(R.drawable.chalkboard);​ 
- drawingLayout.addView(mDrawingView);​ 
- } 
-  
- /** 
- * Removing any existing DrawingView 
- */ 
- private void cleanUpExistingView() 
- { 
- Activity parentActivity = getActivity();​ 
-  
- // Resetting the drawing title'​s text 
- ((EditText)parentActivity.findViewById(R.id.edittext_drawing)).setText(""​);​ 
-  
- if (mDrawingView != null)  
- { 
- // Remove DrawingView from DrawingLayout 
- LinearLayout drawingLayout = (LinearLayout) parentActivity.findViewById(R.id.layout_drawing);​ 
- drawingLayout.removeView(mDrawingView);​ 
-  
- // Cleaning up the background as well 
- Drawable backgroundDrawable = mDrawingView.getBackground();​ 
- if(backgroundDrawable != null) 
- { 
- backgroundDrawable.setCallback(null);​ 
- mDrawingView.setBackgroundResource(0);​ 
- mDrawingView.destroyDrawingCache();​ 
- } 
- 
- mDrawingView = null; 
- } 
- } 
-</​code>​ 
- 
-  * Now, just call ''​createNewBoard()''​ method from the click callback of '​Create New' button (**for the case of TABLETS**): 
-<code java MainActivity.java>​ 
- // Let's create a new board. 
- fragmentTwo.createNewBoard();​ 
- Toast.makeText(this,​ "New board created.",​ Toast.LENGTH_SHORT).show();​ 
-</​code>​ 
- 
-=== Step 04: [TABLETS] Save the drawing === 
- 
-This means we have to implement the click callback for the '​Save'​ button. Since for Tablets, '​Save'​ button is on Main Screen, therefore, its ''​onButtonClick()''​ callback implementation should be on Main screen (i.e. ''​MainActivity''​ class) as well. Also, as you already know, ''​Fragment''​ is used to make design modular, so that it could be reused at multiple places. This is why the main implementation for such a method should lie in ''​FragmentTwo''​ class rather than ''​MainActivity''​. ​ 
-  * So, let's implement ''​saveDrawing()''​ method in ''​FragmentTwo''​ class. 
-<code java FragmentTwo.java>​ 
- public static final String PREFS = "​com.example.board.drawings";​ 
-  
- /** 
- * Method to save the drawing in SharedPreferences. 
- */ 
- public void saveDrawing() 
- { 
- Activity parentActivity = getActivity();​ 
-  
- // Get drawing title from the EditText 
- String drawingName =  ((EditText)parentActivity.findViewById(R.id.edittext_drawing)).getText().toString();​ 
- 
- // Prompt user to give a drawing name if he/she has not already entered 
- if(drawingName == null || drawingName.isEmpty()) 
- { 
- Toast.makeText(parentActivity,​ "​Please enter a name for your drawing",​ Toast.LENGTH_SHORT).show();​ 
- return; 
- } 
-  
- if(mDrawingView == null) 
- { 
- Toast.makeText(parentActivity,​ "No drawing found!",​ Toast.LENGTH_SHORT).show();​ 
- return; 
- } 
-  
- // This is where our drawing is stored 
- ArrayList<​ArrayList<​Float>>​ overallDrawingList = mDrawingView.mOverallDrawingList;​ 
- String flattenedDrawingListString = "";​ 
-  
- // Get number of lines 
- int numParts = overallDrawingList.size();​ 
- 
- // Store the drawing as a FLAT STRING in SharedPreferences 
- SharedPreferences preferences = parentActivity.getSharedPreferences(PREFS,​ Context.MODE_PRIVATE);​ 
- SharedPreferences.Editor editor = preferences.edit();​ 
- 
- // Go through every line of the drawing 
- for (int i = 0; i < numParts; i++)  
- { 
- ArrayList<​Float>​ partDrawingList = overallDrawingList.get(i);​ 
- 
- int numPoints = partDrawingList.size();​ 
- for (int j = 0; j < numPoints;​) ​ 
- { 
- flattenedDrawingListString += partDrawingList.get(j++);​ 
-  
- if (j < numPoints) 
- flattenedDrawingListString += ",";​ 
- } 
-  
- // Separate strings representing a line by a tab space ('​\t'​) 
- flattenedDrawingListString += "​\t";​ 
- } 
-  
- // Store the generated string and commit the changes. 
- editor.putString(drawingName,​ flattenedDrawingListString);​ 
- editor.commit();​ 
-  
- Toast.makeText(parentActivity,​ "'"​ + drawingName + "'​ saved successfully.",​ Toast.LENGTH_SHORT).show();​ 
- } 
-</​code>​ 
- 
-  * Let's add a case for the'​Save'​ button in existing ''​onButtonClick()''​ method in ''​MainActivity'',​ and call ''​saveDrawingMethod''​ on ''​FragmentTwo''​ object. 
-<code java MainActivity::​onButtonClick()>​ 
- case R.id.btn_two_save:​ 
- fragmentTwo.saveDrawing();​ 
- break; 
-</​code>​ 
- 
-And, at this point, our app can save the drawings! Next: retrieve these drawings. 
-{{ :​android-labs-s14:​05_board_save_drawing_tablet.png?​direct&​600 |}} 
- 
- 
-=== Step 05: [TABLETS] Open Existing Drawings === 
- 
-In this step, we would first like to see a list of already existing drawings. We can use ''​ContextMenu''​ for this purpose, as shown in the figure below: 
-{{ :​android-labs-s14:​05_board_context_menu_tablet.png?​direct&​600 |}} 
- 
-  * To create a ''​ContextMenu'',​ we first have to register it (via ''​registerForContextMenu()''​ method) with the UI control, which could then trigger it (via ''​openContextMenu()''​ method) later on. So, let's register for this context menu in onCreate() method itself, as it is the best place to initialize your UI. 
-<code java MainActivity::​onCreate()>​ 
- View openExistingButtonView = findViewById(R.id.btn_one_open_existing);​ 
- registerForContextMenu(openExistingButtonView);​  
-</​code>​ 
- 
-  * Now, just call ''​openContextMenu()''​ method in the click callback of 'Open Existing'​ button. 
-<code java MainActivity::​onButtonClick()>​ 
- case R.id.btn_one_open_existing:​ 
- openContextMenu(findViewById(R.id.btn_one_open_existing));​ 
- break; 
-</​code>​ 
- 
-  * BUT.. BUT.. BUT.., we missed specifying what will show up when this context menu is created. We do this through ''​onCreateContextMenu()''​ method. Since, we're storing the drawings in ''​SharedPreferences'',​ we the name of already existing drawings are basically the value of the keys of all the items in ''​SharedPreferences''​. So, here's how we can implement this method: 
-<code java MainActivity::​onCreateContextMenu()>​ 
- @Override 
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
- { 
- // Setting title for ContextMenu 
- menu.setHeaderTitle(getResources().getString(R.string.one_select_board));​ 
-  
- // Get all the drawing names. 
- SharedPreferences preferences = getSharedPreferences(FragmentTwo.PREFS,​ Context.MODE_PRIVATE);​ 
- Map<​String,​ ?> prefs = preferences.getAll();​ 
- 
- // Iterating through all the items in SharedPreferences 
- for(Map.Entry<​String,​ ?> entry : prefs.entrySet()) ​ 
- { 
- // Extracting the key, which is actually a drawing name. 
-     String key = entry.getKey().toString();​ 
-     ​ 
-     // Add them to menus. 
-     menu.add(key);​ 
- } 
- } 
-</​code>​ 
-  * Now, we want that if a user click on any drawing name showing up in the Context Menu, we should bring up that particular drawing back on the board. Fortunately,​ to have a click callback for an item in a ''​ContextMenu'',​ we just need to implement ''​onContextItemSelected()''​ method. 
-<code java MainActivity::​onContextItemSelected()>​ 
- @Override 
- public boolean onContextItemSelected(MenuItem item) 
- { 
- // Get the drawing name 
- String drawingName = item.getTitle().toString();​ 
-   
- // Check again, if this is a PHONE or a TABLET. 
- FragmentTwo fragmentTwo = (FragmentTwo) getFragmentManager().findFragmentById(R.id.fragment_two);​ 
- if (fragmentTwo == null) // PHONE! 
- { 
- // [Hint] Need to launch DrawingActivity and pass on extra parameters 
- // to tell DrawingActivity to execute openExistingBoard() method. 
- Toast.makeText(this,​ "Will implement later.",​ Toast.LENGTH_SHORT).show();​ 
- } 
- else //​ TABLET! 
- { 
- // FragmentTwo is in the same Activity. Update its UI to show the corresponding drawing. 
- fragmentTwo.openExistingBoard(drawingName);​ 
- } 
-  
-        return super.onContextItemSelected(item);​ 
- } 
-</​code>​ 
- 
-  * And, we come to our last step, where we'll implement ''​openExistingBoard()''​ method. In this method, we'll extract the 'flat string'​ from SharedPreferences and unfold it to show up as a drawing on the board. Here's is its implementation:​ 
-<code java FragmentTwo.java>​ 
- /** 
- * Method to open an existing drawing board. 
- 
- * @param drawingName Drawing Title given by user earlier 
- */ 
- public void openExistingBoard(String drawingName) 
- { 
- cleanUpExistingView();​ 
- 
- ArrayList<​ArrayList<​Float>>​ alreadyStoredDrawingList = new ArrayList<​ArrayList<​Float>>​();​ 
- alreadyStoredDrawingList = readAlreadyStoredDrawingList(drawingName);​ 
- 
- // If there is at least one point in the drawing 
- if (alreadyStoredDrawingList != null && !alreadyStoredDrawingList.isEmpty()) 
- { 
- Activity parentActivity = getActivity();​ 
- LinearLayout drawingLayout = (LinearLayout) parentActivity 
- .findViewById(R.id.layout_drawing);​ 
-  
- if (mDrawingView == null) 
- { 
- mDrawingView = new DrawingView(parentActivity);​ 
- mDrawingView.setBackgroundResource(R.drawable.chalkboard);​ 
- drawingLayout.addView(mDrawingView);​ 
- } 
- 
- if (mDrawingView != null) 
- { 
- mDrawingView.setOverallDrawingList(alreadyStoredDrawingList);​ 
- mDrawingView.invalidate();​ 
- } 
- 
- parentActivity.findViewById(R.id.textview_welcome).setVisibility(View.GONE);​ 
- drawingLayout.setVisibility(View.VISIBLE);​ 
- ((EditText) parentActivity.findViewById(R.id.edittext_drawing)).setText(drawingName);​ 
- } 
- } 
- 
- /** 
- * Helper method to extract already store drawing list from 
- * SharedPreferences in a specific format. 
- */ 
- private ArrayList<​ArrayList<​Float>>​ readAlreadyStoredDrawingList(String drawingName) 
- { 
- ArrayList<​ArrayList<​Float>>​ drawingList = new ArrayList<​ArrayList<​Float>>​();​ 
- 
- Activity parentActivity = getActivity();​ 
- if (parentActivity != null) 
- { 
- SharedPreferences preferences = parentActivity.getSharedPreferences(FragmentTwo.PREFS,​ 
- Context.MODE_PRIVATE);​ 
- String flattenedDrawingList = preferences.getString(drawingName,​ null); 
- 
- if (flattenedDrawingList != null) 
- { 
- // Check if the list is empty 
- if (flattenedDrawingList.isEmpty()) 
- return drawingList;​ 
- 
- String drawingLines[] = flattenedDrawingList.split("​\t"​);​ 
- 
- // Unfolding the flat string 
- for (int i = 0; i < drawingLines.length;​ i++) 
- { 
- String linePoints[] = drawingLines[i].split(","​);​ 
- 
- ArrayList<​Float>​ linesList = new ArrayList<​Float>​();​ 
- 
- for (int j = 0; j < linePoints.length;​ j++) 
- linesList.add(Float.parseFloat(linePoints[j]));​ 
- 
- drawingList.add(linesList);​ 
- } 
- } 
- } 
- 
- return drawingList;​ 
- } 
-</​code>​ 
- 
-==== Source Code ==== 
-Available at [[https://​github.com/​panwaria/​Board|Github]]. 
- 
----- 
- 
-===== Programming Assignment ===== 
-Make the **Board** app working for phone. One should be able to create a new board, make drawing, save drawing, and open an existing board on Phone. 
- 
android-labs-s14/class-05.1392662989.txt.gz · Last modified: 2014/02/17 12:49 by prakhar