Wednesday, 10 September 2014

Andriod register events and implementation


Android provides controls as components to design our application layout. in eclipse palette provides various categories of components : TextFileds,Layouts,Composite, Images & Media , Date and time.Tansitions,,ect

Layouts: these are main controls/components to design screen/activity UI. using respective layout contorl(Grid,Linear,Relative,Frame,Table,TableRow,Fragement,Space).


in this we created/default design layout is RelativeLayout, in that we place button and editText controls/components in Relativelayout. andriod.layout_below: order of control placed in layout/relativelayout.

in Code of  MainActivity.we need refer these components, so that we need find these control. then handling/registering the events.


here we casted 2 ways --1 is view casting && 2nd is direct casting to repective control(EditText) type. next article is types of layouts.

2nd implementation:

       Button b1;
       TextView tv;

        tv=(TextView)findViewById(R.id.textView1);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener()
        {
       public void onClick(View v)
       {
        tv.setText("welcome to android");
       }});
     
         }
3rd implementation

        Button b1;
        TextView tv;
   
            protected void onCreate(Bundle savedInstanceState)
   {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);
                     tv=(TextView)findViewById(R.id.textView1);
                     b1=(Button)findViewById(R.id.button1);
                     b1.setOnClickListener(myhandler1);  ****
   }    
   OnClickListener myhandler1 = new View.OnClickListener()
   {
       public void onClick(View v) {        
        tv.setText("welcome to androids");
       }
     };
     
4th implementation : declare funcation in Code & at activity_main.xml

  <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_first" android:onClick="ButtonOnClick" />


     


type castings: View is base type

View v=findViewById(R.id.button1);
Button b=(Button)v;
or 
Button b=(Button)findViewById(R.id.button1);

public void onClick(View v)
{
   if(v==b1)
       tv.setText("Button-1 Clicked");
        else if(v.getId()==R.id.button2)
       tv.setText("Button-2 Clicked");
else
     finish();
 }


No comments:

Post a Comment