archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Understanding Android Views, View Groups and Layouts - Techotopia
    set on each child to specify how much of the layout space that child should occupy relative to other children TableLayout Arranges child views into a grid format of rows and columns Each row within a table is represented by a TableRow object child which in turn contains a view object for each cell FrameLayout The purpose of the FrameLayout is to allocate an area of screen typically for the purposes of displaying a single view If multiple child views are added they will by default appear on top of each other positioned in the top left hand corner of the layout area Alternate positioning of individual child views can be achieved by setting gravity values on each child For example setting a center vertical gravity on a child will cause it to be positioned in the vertical center of the containing FrameLayout view RelativeLayout Probably the most powerful and flexible of the layout managers this allows child views to be positioned relative both to each other and the containing layout view through the specification of alignments and margins on child views For example child View A may be configured to be positioned in the vertical and horizontal center of the containing RelativeLayout view View B on the other hand might also be configured to be centered horizontally within the layout view but positioned 30 pixels above the top edge of View A thereby making the vertical position relative to that of View A The RelativeLayout manager can be of particular use when designing a user interface that must work on a variety of screen sizes and orientations AbsoluteLayout Allows child views to be positioned at specific X and Y coordinates within the containing layout view Use of this layout is discouraged since it lacks the flexibility to respond to changes in screen size and orientation GridLayout The GridLayout is a relatively new layout manager that was introduced as part of Android 4 0 A GridLayout instance is divided by invisible lines that form a grid containing rows and columns of cells Child views are then placed in cells and may be configured to cover multiple cells both horizontally and vertically allowing a wide range of layout options to be quickly and easily implemented Gaps between components in a GridLayout may be implemented by placing a special type of view called a Space view into adjacent cells or by setting margin parameters When considering the use of layouts in the user interface for an Android application it is worth keeping in mind that as will be outlined in the next section these can be nested within each other to create a user interface design of just about any necessary level of complexity The View Hierarchy Each view in a user interface represents a rectangular area of the display A view is responsible for what is drawn in that rectangle and for responding to events that occur within that part of the screen such as a touch event A user interface

    Original URL path: http://www.techotopia.com/index.php/Understanding_Android_Views,_View_Groups_and_Layouts (2016-02-13)
    Open archived version from archive


  • Designing an Android User Interface using the Graphical Layout Tool - Techotopia
    instructed Eclipse not to create an activity for us we are now going to create our own This will be a valuable learning exercise since there are many instances in the course of developing Android applications where new activities need to be created from the ground up Right click on the LayoutSample project in the Package Explorer panel and select the New Class menu option In the resulting New Java Class dialog Figure 13 5 we will need to configure the new activity class Begin by clicking in the Package text field Within this field we need to enter the package name of our project Either this can be typed manually or the Ctrl Space keyboard combination used to pop up a Content Assist panel containing a list of package names from which to choose In this case type in the package name for the application as declared when the project was created for example com example layoutsample Name the new activity LayoutSampleActivity Since most activities are subclassed from the Activity class change the Superclass value from java lang Object to android app Activity before clicking on Finish to create the new class Figure 13 5 Locate the source file for the newly created activity which will be located within the Package Explorer under LayoutSample src packagename LayoutSampleActivity java Double click on the file to load it into the source code editor where it should read as follows package com example layoutsample import android app Activity public class LayoutSampleActivity extends Activity Clearly Eclipse has generated only the outline template of the new class In order to ensure that the user interface layout is displayed when the activity is launched we will need to override the onCreate method of the Activity superclass and set the layout created above in the layout xml file as the activity s content view With these requirements in mind modify the LayoutSampleActivity java file so that it reads as follows package com example LayoutSample import android app Activity import android os Bundle public class LayoutSampleActivity extends Activity Override public void onCreate Bundle savedInstanceState super onCreate savedInstanceState setContentView R layout layout With the activity created and modified to display the layout contained in layout xml all that remains is to modify the application s manifest file so that the activity is launched when the application starts Save the source file before proceeding Adding the New Activity to the Manifest File The chapter entitled The Anatomy of an Android Application described briefly the concept of the application manifest file Each Android application has associated with it a manifest file containing an XML based description of the application including information about the activities that it contains By default this file is named AndroidManifest xml and as with all project files it can be located in the Package Explorer under the tree for each project In order to notify the Android runtime system about the existence of the LayoutSampleActivity activity it will be necessary to add an entry for the activity to this manifest file Locate the AndroidManifest xml file for the LayoutSample project and double click on it to load the Manifest editor panel as illustrated in Figure 13 6 Figure 13 6 We will add the activity entry directly into the XML file so select the AndroidManifest xml tab located along the bottom edge of the manifest editor Once selected the content should appear in the editing panel as follows manifest xmlns android http schemas android com apk res android package com example layoutsample android versionCode 1 android versionName 1 0 uses sdk android minSdkVersion 19 android targetSdkVersion 19 application android allowBackup true android icon drawable ic launcher android label string app name android theme style AppTheme application manifest At present the manifest contains the standard template information for an Android application Absent however is an entry for our LayoutSampleActivity activity Within the editor panel therefore modify the XML so that it reads as follows xml version 1 0 encoding utf 8 manifest xmlns android http schemas android com apk res android package com ebookfrenzy LayoutSample android versionCode 1 android versionName 1 0 uses sdk android minSdkVersion 10 application android icon drawable ic launcher android label string app name android theme style AppTheme activity android label string app name android name LayoutSampleActivity intent filter action android name android intent action MAIN category android name android intent category LAUNCHER intent filter activity application manifest The directives between the activity and activity tags serve to notify the Android runtime system that the application contains the new activity and that the activity can be launched at runtime Running the Application All that remains is to test that the application runs Right click on the LayoutSample project name in the Package Explorer panel and select Run As Android Application Select either the simulator or a physical Android device and wait for the application to start Assuming the absence of errors the application and activity should launch and appear exactly as designed using the Graphical Layout tool Manually Creating an XML Layout It is important to keep in mind when using the Graphical Layout tool that all it is really doing is providing a user friendly approach to creating XML layout resource files At any time during the design process the underlying XML can be viewed and directly edited simply by clicking on the tab displaying the filename in this case layout xml located at the bottom of the Graphical Layout tool panel To return to the tool simply click on the Graphical Layout tab Whilst the design of layouts using the layout tool greatly improves productivity it is still possible to create XML layouts by manually editing the XML The structure of an XML layout file is actually straightforward and follows the hierarchical approach of the view tree The first line of an XML resource file should include the following standard declaration xml version 1 0 encoding utf 8 This declaration should be followed by the root element of the layout typically

    Original URL path: http://www.techotopia.com/index.php/Designing_an_Android_User_Interface_using_the_Graphical_Layout_Tool (2016-02-13)
    Open archived version from archive

  • Creating an Android User Interface in Java Code - Techotopia
    layout with the fragment layout named fragment java layout Once the project has been created navigate within the Package Explorer panel to src package name and double click on the JavaLayoutActivity java file so that it loads into an editing panel As we have come to expect Eclipse has created the template activity and overridden the onCreate method providing an ideal location for Java code to be added to create a user interface Adding Views to an Activity The onCreate method is currently designed to use a resource layout file and a fragment for the user interface Begin therefore by deleting these lines from the method so that it reads as follows Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceState The next modification to the onCreate method is to write some Java code to add a RelativeLayout object with a single Button view child to the activity This involves the creation of new instances of the RelativeLayout and Button classes The Button view then needs to be added as a child to the RelativeLayout view which in turn is displayed via a call to the setContentView method of the activity instance package com example javalayout import android app Activity import android app ActionBar import android app Fragment import android os Bundle import android view LayoutInflater import android view Menu import android view MenuItem import android view View import android view ViewGroup import android os Build import android widget Button import android widget RelativeLayout public class MainActivity extends Activity Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceState Button myButton new Button this RelativeLayout myLayout new RelativeLayout this myLayout addView myButton setContentView myLayout Once the above additions have been made compile and run the application either on a physical device or an emulator Once launched the visible result will be a button containing no text appearing in the top left hand corner of the RelativeLayout view Setting View Properties For the purposes of this exercise we need the background of the RelativeLayout view to be blue and the Button view to display text that reads Press me Both of these tasks can be achieved by setting properties on the views as outlined in the following code fragment package com example javalayout import android app Activity import android app ActionBar import android app Fragment import android os Bundle import android view LayoutInflater import android view Menu import android view MenuItem import android view View import android view ViewGroup import android os Build import android widget Button import android widget RelativeLayout import android graphics Color public class MainActivity extends Activity Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceState Button myButton new Button this myButton setText Press me myButton setBackgroundColor Color YELLOW RelativeLayout myLayout new RelativeLayout this myLayout setBackgroundColor Color BLUE myLayout addView myButton setContentView myLayout When the application is now compiled and run the layout will reflect the property settings such that the layout will appear with a blue background and the button will display the assigned text on a yellow background Adding Layout Parameters and Rules By default the RelativeLayout view has placed the button view in the top left corner of the display In order to instruct the layout view to place the button in a different location in this case centered both horizontally and vertically it will be necessary to create a LayoutParams object and initialize it with the appropriate values Typically a new LayoutParams instance is created by passing through the height and width values for the view These values should be set to either MATCH PARENT WRAP CONTENT or specific size values The MATCH PARENT setting instructs the parent layout to expand the child view so that it matches the size of the parent WRAP CONTENT on the other hand instructs the parent to size the child view so that it is only large enough to display any content it may be configured to show to the user The code to create a LayoutParams object for our button would read as follows RelativeLayout LayoutParams buttonParams new RelativeLayout LayoutParams RelativeLayout LayoutParams WRAP CONTENT RelativeLayout LayoutParams WRAP CONTENT The above code creates a new RelativeLayout LayoutParams object named buttonParams and sets the height and width such that the button will only be large enough to display the Press Me text previously configured via the property setting Now that the LayoutParams object has been created the next step is to add some additional rules to the parameters to instruct the layout parent to center the button vertically and horizontally This is achieved by calling the addRule method of the buttonParams object passing through the appropriate values as arguments buttonParams addRule RelativeLayout CENTER HORIZONTAL buttonParams addRule RelativeLayout CENTER VERTICAL Simply creating a new LayoutParams object and configuring it is only useful if that object is then assigned to the child view One way to achieve this is to pass the LayoutParams object through as an argument when the child view is added to the parent myLayout addView myButton buttonParams Alternatively the parameters can be assigned to the child via a call to the setLayoutParams method of the view myButton setLayoutParams buttonParams Bringing these together results in a modified onCreate method that reads as follows Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceState Button myButton new Button this myButton setText Press me myButton setBackgroundColor Color YELLOW RelativeLayout myLayout new RelativeLayout this myLayout setBackgroundColor Color BLUE RelativeLayout LayoutParams buttonParams new RelativeLayout LayoutParams RelativeLayout LayoutParams WRAP CONTENT RelativeLayout LayoutParams WRAP CONTENT buttonParams addRule RelativeLayout CENTER HORIZONTAL buttonParams addRule RelativeLayout CENTER VERTICAL myLayout addView myButton buttonParams setContentView myLayout Having made the above changes compile and run the application once again this time noting that the button is now centered within the RelativeLayout view as illustrated in Figure 14 1 Figure 14 1 In order to gain a clearer understanding of the height and width layout parameter settings temporarily modify the buttonParams creation code to read as follows then re compile and run the application RelativeLayout LayoutParams buttonParams new RelativeLayout LayoutParams RelativeLayout LayoutParams MATCH

    Original URL path: http://www.techotopia.com/index.php/Creating_an_Android_User_Interface_in_Java_Code (2016-02-13)
    Open archived version from archive

  • Using the Android GridLayout Manager in the Graphical Layout Tool - Techotopia
    grid layout xml tab located along the bottom edge of the Graphical Layout panel and edit the XML to remove the padding properties GridLayout xmlns android http schemas android com apk res android xmlns tools http schemas android com tools android id id GridLayout1 android layout width wrap content android layout height wrap content android paddingBottom dimen activity vertical margin android paddingLeft dimen activity horizontal margin android paddingRight dimen activity horizontal margin android paddingTop dimen activity vertical margin tools context GridLayoutActivity GridLayout The GridLayout instance is now ready to have some views placed within cells An Overview of the GridLayout in the Graphical Layout Tool The first point to note about the Graphical Layout tool is that some additional toolbar buttons Figure 15 2 appear when a GridLayout is present in the user interface Figure 15 2 The left most button switches the tool in and out of Grid Model Mode Grid Model Mode provides a greater level of control over adding deleting and inserting rows and columns When dragging views from the palette on to the layout canvas in this mode options will be provided to insert and add rows to the GridLayout to accommodate the current position of the view as it is dragged over the GridLayout area When in Grid Model Mode the two right hand buttons are also visible allowing new columns and rows to be added respectively When rows and columns already exist within the grid two additional buttons appear as shown in Figure 15 3 for the purposes of deleting rows and columns Figure 15 3 The second button from the left is the Show Structure button which turns on and off visible guidelines indicating the structure of the grid When Grid Model Mode is disabled the add remove buttons disappear and a new Snap to Grid button the right most of the buttons in Figure 15 4 appears Figure 15 4 When enabled a range of dots will appear in the layout to which views will snap when added to cells By far the most flexible way to manage the views in a GridLayout when using the Graphical Layout tool is to do so in Grid Model Mode with the Show Structure option enabled With this in mind make sure that these two modes are selected before proceeding to the next section eBookFrenzy com Purchase the fully updated Android 6 Edition of this Android Studio Development Essentials publication in eBook 9 99 or Print 38 99 format Android Studio Development Essentials Android 6 Edition Print and eBook ePub PDF Kindle editions contain 65 chapters Adding Views to GridLayout Cells To place view objects into the cells of a GridLayout simply click on the required view in the palette and drag it to the GridLayout instance in the layout canvas As the view passes over the GridLayout information will appear indicating the placement of the view if it is dropped at that point Begin by clicking and dragging a Button view from the palette

    Original URL path: http://www.techotopia.com/index.php/Using_the_Android_GridLayout_Manager_in_the_Graphical_Layout_Tool (2016-02-13)
    Open archived version from archive

  • Working with the Android GridLayout in XML Layout Resources - Techotopia
    context GridXMLActivity GridLayout Adding Child Views to the GridLayout Child views can be added to a GridLayout by declaring the elements within the GridLayout structure in the XML file If no row and column values are declared for a child it is positioned automatically by the GridLayout class based on the configuration of the layout and the position of the view in the XML file The following XML places four buttons within the above GridLayout with each view placed in the top left hand corner of the encapsulating cell GridLayout xmlns android http schemas android com apk res android xmlns tools http schemas android com tools android id id GridLayout1 android layout width wrap content android layout height wrap content android columnCount 2 android rowCount 2 android orientation horizontal tools context GridXMLActivity Button android id id button1 android layout gravity left top android text Button Button android id id button2 android layout gravity left top android text Button Button android id id button3 android layout gravity left top android text Button Button android id id button4 android layout gravity left top android text Button GridLayout The above layout would be visually represented as illustrated in Figure 16 1 Figure 16 1 A view can be placed within a specific cell by specifying the intersecting row and column number of the destination cell The following Button view will be placed in the cell and row 1 column 2 of the parent GridLayout Button android id id button5 android layout column 2 android layout row 1 android layout gravity left top android text Button eBookFrenzy com Purchase the fully updated Android 6 Edition of this Android Studio Development Essentials publication in eBook 9 99 or Print 38 99 format Android Studio Development Essentials Android 6 Edition Print and eBook ePub PDF Kindle editions contain 65 chapters Declaring Cell Spanning Gravity and Margins The child of a GridLayout can be configured to span multiple cells using the android layout rowSpan and android layout columnSpan properties The gravity of the child is controlled via the android layout gravity property In the XML fragment below a Button view is configured to span 3 columns and 2 rows and to fill the space available both horizontally and vertically Button android id id button4 android layout columnSpan 3 android layout rowSpan 2 android layout gravity fill android text Button The margins around a view within a cell can be declared for all sides of the view using the android layout margin margin property Alternatively margins for individual sides may be defined using the topMargin bottomMargin leftMargin and rightMargin properties The following Button view declares a 10dp margin on all four sides Button android id id button3 android layout gravity left top android layout margin 10dp android text Button Bringing this knowledge together we can now review the XML layout file for the GridLayout based user interface created in the previous chapter GridLayout xmlns android http schemas android com apk res android xmlns tools http schemas android com

    Original URL path: http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources (2016-02-13)
    Open archived version from archive

  • An Overview and Example of Android Event Handling - Techotopia
    the creation of a blank activity and the use of the default launcher icons On the New Blank Activity screen of the New Android Application wizard set the Activity Name to EventExampleActivity the Layout Name to activity event example and the Fragment Layout to fragment event example Designing the User Interface The user interface layout for the EventExampleActivity class in this example is to consist of a RelativeLayout view a Button and a TextView as illustrated in Figure 17 1 Figure 17 1 eBookFrenzy com Purchase the fully updated Android 6 Edition of this Android Studio Development Essentials publication in eBook 9 99 or Print 38 99 format Android Studio Development Essentials Android 6 Edition Print and eBook ePub PDF Kindle editions contain 65 chapters Locate and select the fragment event example xml file created by Eclipse located in the Package Explorer under EventExample res layout fragment event example xml and double click on it to load it into the editing panel Switch from the Graphical Layout tool to the XML file using the tab at the bottom of the editing panel and delete the current content of the file With a blank canvas either use the Graphical Layout tool to design the user interface from Figure 17 1 making sure to change the IDs of the Button and TextView objects to myButton and myTextView respectively or directly enter the following XML into the editor RelativeLayout xmlns android http schemas android com apk res android android id id myLayout android layout width fill parent android layout height fill parent Button android id id myButton android layout width wrap content android layout height wrap content android layout centerHorizontal true android layout centerVertical true android text string mybutton string TextView android id id myTextView android layout width wrap content android layout height wrap content android layout above id myButton android layout centerHorizontal true android layout marginBottom 41dp android text string mytextview string android textAppearance android attr textAppearanceLarge RelativeLayout Within the Graphical Layout tool right click on the Button view and select the Edit Text menu option In the Resource Chooser dialog click on the New String button and in the Create New Android String dialog enter Press Me into the String field and mybutton string into the New R string field Figure 17 2 Click on OK in the new android string dialog to create the new string resource and then again in the resource chooser dialog to assign the string to the button view Repeat these steps on the TextView object to create a new string resource named mytextview string with a string that reads Status Be sure to save the file once the changes are complete With the user interface layout now completed the next step is to register the event listener and callback method The Event Listener and Callback Method For the purposes of this example an onClickListener needs to be registered for the myButton view This is achieved by making a call to the setOnClickListener method of the

    Original URL path: http://www.techotopia.com/index.php/An_Overview_and_Example_of_Android_Event_Handling (2016-02-13)
    Open archived version from archive

  • Android Touch and Multi-touch Event Handling - Techotopia
    therefore it is essential that the ID value be used as the touch reference in order to make sure the same touch is being tracked When calling methods that require an index value this should be obtained by converting the ID for a touch to the corresponding index value via a call to the findPointerIndex method of the MotionEvent object An Example Multi Touch Application The example application created in the remainder of this chapter will track up to two touch gestures as they move across a layout view As the events for each touch are triggered the coordinates index and ID for each touch will be displayed on the screen Launch Eclipse and create an Android Application Project named MotionEvent with the appropriate package name and SDK selections As with previous examples request the creation of a blank activity and the use of the default launcher icons On the New Blank Activity screen of the New Android Application wizard set the Activity Name to MotionEventActivity and the Layout and Fragment Layout names to activity motion event and fragment motion event respectively Designing the Activity User Interface The user interface for the application s sole activity is to consist of a RelativeLayout view containing two TextView objects Within the Package Explorer panel navigate to MotionEvent res layout and double click on the fragment motion event xml layout resource file to load it into the Graphical Layout tool One option is to design the layout illustrated in Figure 18 1 using a RelativeLayout as the root view and keeping in mind that the lower TextView component is centered horizontally within the parent view and the upper TextView is positioned a relative distance above the lower TextView Alternatively switch to the XML editor by selecting the fragment motion event xml tab at the bottom of the layout panel and replace the current XML with the following xml version 1 0 encoding utf 8 RelativeLayout xmlns android http schemas android com apk res android android id id RelativeLayout1 android layout width fill parent android layout height fill parent android orientation vertical TextView android id id textView2 android layout width wrap content android layout height wrap content android layout centerHorizontal true android layout centerVertical true android text Large Text android textAppearance android attr textAppearanceLarge TextView android id id textView1 android layout width wrap content android layout height wrap content android layout above id textView2 android layout alignLeft id textView2 android layout marginBottom 47dp android text Large Text android textAppearance android attr textAppearanceLarge RelativeLayout Figure 18 1 Before moving on to write the code for the callback method be sure to save the fragment motion event xml file Implementing the Touch Event Listener In order to receive touch event notifications it will be necessary to register a touch listener on the RelativeLayout1 view within the onStart method of the activity class Within the Package Explorer panel locate the MotionEventActivity java file and double click on it to load it into the editor panel Within the

    Original URL path: http://www.techotopia.com/index.php/Android_Touch_and_Multi-touch_Event_Handling (2016-02-13)
    Open archived version from archive

  • Detecting Common Gestures using the Android Gesture Detector Class - Techotopia
    and creating an Android Application Project named CommonGestures with the appropriate package name and SDK selections As with previous examples request the creation of a blank activity and the use of the default launcher icons On the New Blank Activity screen of the New Android Application wizard set the Activity Name to CommonGesturesActivity and the Layout and Fragment names to activity common gestures and fragment common gestures respectively Once the new project has been created double click on the CommonGestures res layout fragment common gestures xml file in the Package Explorer to load it Within the Graphical Layout tool right click on the Hello World TextView component and select Assign ID from the resulting menu In the subsequent dialog enter gestureStatusText as the ID Next assign an ID of RelativeLayout1 to the RelativeLayout view Move the TextView so that it is positioned in the center of the display and then save the layout file before proceeding Implementing the Listener Class As previously outlined it is necessary to create a class that implements the GestureDetector OnGestureListener interface and if double tap detection is required the GestureDetector OnDoubleTapListener interface Whilst this can be an entirely new class it is also perfectly valid to implement this within the current activity class For the purposes of this example therefore we will modify the CommonGesturesActivity class to implement these listener interfaces Within the Package Explorer panel locate and double click on the CommonGestures src package name CommonGesturesActivity java file Once loaded into the editing panel modify the source code so that it reads as follows to declare the interfaces and to extract and store a reference to the TextView component in the user interface package com example commongestures import android app Activity import android app ActionBar import android app Fragment import android os Bundle import android util Log import android view LayoutInflater import android view Menu import android view MenuItem import android view View import android view ViewGroup import android os Build import android view GestureDetector import android widget TextView public class CommonGesturesActivity extends Activity implements GestureDetector OnGestureListener GestureDetector OnDoubleTapListener private TextView gestureText Override protected void onStart super onStart gestureText TextView findViewById R id gestureStatusText Declaring that the class implements the listener interfaces mandates that the corresponding methods also be implemented in the class package com example commongestures import android app Activity import android app ActionBar import android app Fragment import android os Bundle import android util Log import android view LayoutInflater import android view Menu import android view MenuItem import android view View import android view ViewGroup import android os Build import android view GestureDetector import android widget TextView import android view MotionEvent public class CommonGesturesActivity extends Activity implements GestureDetector OnGestureListener GestureDetector OnDoubleTapListener private TextView gestureText Override public boolean onDown MotionEvent event gestureText setText onDown return true Override public boolean onFling MotionEvent event1 MotionEvent event2 float velocityX float velocityY gestureText setText onFling return true Override public void onLongPress MotionEvent event gestureText setText onLongPress Override public boolean onScroll MotionEvent e1 MotionEvent e2 float distanceX float

    Original URL path: http://www.techotopia.com/index.php/Detecting_Common_Gestures_using_the_Android_Gesture_Detector_Class (2016-02-13)
    Open archived version from archive



  •