Sunday, 15 April 2012

Button-Android Fundamental-part-1

               This is first part of Android fundamental for new Android user.In this part we study about the Button.

Create Button:-
                           To use Button, we need to create button in sample.xml file.we can create button by two way.
1.Using .xml file
2.By java code

1.Sample.xml file:-
                              You need to drag and drop button from form widget. or you manually write code for button in sample.xml file.
Here is code for Button.

        android:gravity="center" android:layout_marginTop="25dp"></Button>

Property Description:- 
                            1.Android-id:-  You need to set id for button for further use in .java file.we need id of any widget to allocate some work for further coding.   
               Button Home=(Button)findViewById(; 
      Here we assign id to Home button.
                           2.Android:text:- Text which we require to show on Button. we assign "Home" text to Button.
                           3.Android:layout_height/Android:layout_width:-Here we decide height and width of Button.
                  We can assign any image to button on respective side. Like top, Bottom, Right, Left.
                  Using this property we can assign color to Button-text.
                  Gravity are use to set gravity for can choose any gravity as per your need.
                  We can allocate margin in left, right, top and at bottom.
                   We use this property to hide button view. using this property we able to show only text and image of button but you need to use appropriate color which match with background.

2. By Programatically:-
                                       Here is sample code for add Button in java program.

    public void onCreate(Bundle savedInstanceState) {

          //Create instance of button
          Button Home = new Button(this);
          Home .setText("Home");

         //we use linear layout to add button.
        LinearLayout layout = (LinearLayout ) View.inflate(this, R.layout.sample, null);
        //add the button to the view
       layout  .addView(Home );
       //set the layout using the inflated view

3. Use of Button:-  
                            We use button for do some work after click on it.
 we use Onclick() method for that. Here is sample code which jump from one activity to second activity.

Home.setOnClickListener(new View.OnClickListener()
            public void onClick(View v)

                 Intent intent = new Intent(getApplication(),Activity2.class);

                 //Uri uri = Uri.parse("");
                // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                // startActivity(intent);

Commented line are use to jump on specific web page.

                    Here is image which use all above properties.



SAVE TREE SAVE LIFE................


No comments:

Post a Comment