Pages

Sunday 15 April 2012

Button-Android Fundamental-part-1

Button:-
               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.

<Button 
        android:id="@+id/button1"
        android:layout_width="106dp"
        android:layout_height="90dp"
        android:textSize="15px"
        android:text="Home"
        android:drawableTop="@drawable/faq"
        android:textColor="@color/myWhiteColor"
        android:background="@drawable/examguide_btnmain"
        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(R.id.button1); 
      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.
                           4.android:drawableTop:- We can assign any image to button on respective side. Like top, Bottom, Right, Left.
                           5.android:textColor:- Using this property we can assign color to Button-text.
                           6.android:gravity:- Gravity are use to set gravity for text.you can choose any gravity as per your need.
                           7.android:layout_marginTop:- We can allocate margin in left, right, top and at bottom.
                           8.android:background:-  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.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(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
        setContentView(layout);
  
   }

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()
        {
          
            @Override
            public void onClick(View v)
            {

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

                 //Uri uri = Uri.parse("http://www.mpsc.gov.in/advertisements.jsp?head=1&status=1");
                // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                // startActivity(intent);
 
            }
        });


Commented line are use to jump on specific web page.

4.Image:-
                    Here is image which use all above properties.
 


                  
         

Enjoy..........

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


 

No comments:

Post a Comment