Saturday 9 June 2012

Using Control in Android program

 Before describing the user control.I told you about  View in my previous positioning.The android.view.View is our base class for user interface.There are three type of view:
  1. SurfaceView
  2. ViewGroup
  3. Widget

The most basic view is SurfaceView. Here we will not discuss this SurfaceView. because it is concern the drawing issue.
ViewGroup is layout concern and contain other view
Widget it is UI component which most often used.
     Widget is part of android.widget package e.g. ListView,Spinner etc.
We start from Button control.
Button Control example:-
In this example we make Edittext and TextView and a button
Step 1: Layout consideration :
       We have two choice to make Layout consideration
(a)Graphical Method
(b)By main.xml file


we take buttonexamplea1 as new project.


Graphical Method: you are seeing the two tab Graphical Layout and main.xml. we click in Graphical Layout.In Graphical we drag a EditText (abh shown in left tab) under TextFields and drop in layout screen.we also drag button and TextView from Form Widget(in left tab)

 When you are designing the layout through Geometrical Method, automatically main.xml file is coded.
   Now we have to  hard coding  in main.xml file
Method 2:-
Line 1:<?xml version="1.0" encoding="utf-8"?>

Line 2:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
Line 3: android:layout_width="fill_parent"
Line 4: android:layout_height="fill_parent"
Line 5: android:orientation="vertical" > 
Line 6: <EditText  
Line 7: android:id="@+id/mytext"
Line 8: android:layout_width="wrap_content" 
Line 9: android:layout_height= "fill_parent"
Line 10: android:text="@string/entxt"
Line 11: android:hint="@string/entxt"      
       />


Line 12:  <TextView 
Line 13:  android:id="@+id/txxt"
Line 14:  android:layout_width="wrap_content" 
Line 15:  android:layout_height="wrap_content" 
Line 16: android:text="@string/t1"        
        />



    <Button 

Line 17: android:id="@+id/but"
Line 18: android:layout_width="wrap_content" 
Line 19: android:layout_height="wrap_content" 
Line 20: android:text="@string/but_lab"
Line 21: android:onClick="showtext"        
        />



</LinearLayout>

Line 1-5  I already explained in previous post
Line 7 To link the main.xml file and Java source through R.class file.There is crying need to create a id attribute "@+id/mytext. so that we can use in java source code.
Line 10 Here we using a string which shown when there is noting in Edit text box i.e default text or suggestive text we create a string either Graphical or edit in string.xml file. when you add string through the Graphical recourse it is automatically added in string.xml file


Line 10 Here we using a string which shown when there is noting in Edit text box i.e default text or suggestive text we create a string either Graphical or edit in string.xml file. when you add string through the Graphical recourse it is automatically added in string.xml file
   you can add string resource by typing string tag with name attribute  and the value of name attribute. It will automatically generated string resources in Graphical mode of string.xml
Line 11:android:hint="@string/entxt"   The hint attribute is used for suggestive text in Edit box
Line 13:android:id="@+id/txxt" it is another a id attribute for TextView
Line 16: android:text="@string/t1" t1 is string resources for android TextView
Line 17:android:id="@+id/but" Id attribute for button
Line 20:android:text="@string/but_lab" It is label name which show on the button.since again it is string resource it create same way as i told you. 
Line 21: android:onClick="showtext"   "showtext" is the name of function it call when onClick event occurred.since it link with the onClick event.   
      
When mention the name of function in main.xml file and do not define the function in Java source file.On execution of application as soon as click the button This above kind of screen is show.
  Explanation:- When you press the button ,you trying to update the  View  i.e. added a new String resources and modified the view. for the button a click event is define but it did not get any function which define the action so it gives error.

Line:1 public void showtext(View view)
{
Line:2        EditText ed = (EditText)findViewById(R.id.mytext);
Line:3        TextView tv = (TextView)findViewById(R.id.txxt);
Line:4        tv.setText(ed.getText());     
    }



Line 1: public void showtext(View view) It is the name of function it contains view object
Line 2,3:  EditText ed = (EditText)findViewById(R.id.mytext) I already told that findViewById(R.id.resource id) i.e similar method to JavaScript in which we use the getdooucmentById().findViewById  can reference the EditText but  you access through R.class it is View kind of object hence it is very necessary to cast to EditText and TextView as requirement.
Line 4:tv.setText(ed.getText())-getText and setText is method by which get value of EditText View and that value is set to TextView 
  Now you compile the application
 



No comments:

Post a Comment