Saturday, February 8, 2014

working with Button

User Interface in Android. 
     After successfully running the your hellow world example we turn to some UI (User Interface)Concept.
in Android.

     The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects. View objects are usually UI widgets such as buttons or text fields and ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.
Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.

     Their are various User Interface are aviable thar are as follows:
  1. TextView.
  2. Button.
  3. ToggleButton.
  4. CheckBox.
  5. RadioButton.
  6. Spinner.
  7. ProgressBar.
  8. SeekBar.
  9. ImageView.
  10. DatePicker & TimePicker. 
Our Output Will be Following:-


working with Button  
 

Step 1:- First you need to create the new  Android Applicaiton Project.
 How to create it goto File->New->AndroidApplicationProject. One window will be open in that fill the requird information such as, Application Name, Project Name, Package Name etc.
  

    




















step2 :- follow the process till Finish Window will not come.
























After clicking the finish button. it will create the folder structure. and font of the window show the activity_main.xml and MainActivity.java file's.

















Step 3:-delete the hello world content in activity_main.xml file.
            on the left hand site of the screen Paletee ber select the  FormWidgest tab and take button from that.
 
















Step 4:- On Button you see the one triange in yellow color that is warning how to solve that warning.
              Goto res->values->string.xml(click on it) . Android Resources(Default) window will open on the     screen in that click on string  ->Add  




















Step 5:- In activiy_main.xml just click on button . and right bottom corner Propery window is there in that click on Button then one window will be disple in that choose the name wich you register in string.xml file ->ok.













after completing the this process you warning will remove from button.




















Step  6:- we have to perform the action on the button. that's why we need to write code in java file just open MainActivity.java file following is code of the java file.

MainActivity.java       

package com.baba.workingwithbutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting the reference of button.
        Button button=(Button)findViewById(R.id.button1);
       
        //set the event handeling on button
        button.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                //when you click on the button then tost will be execuated
                Toast.makeText(getApplicationContext(), "Congratulation ! Your Button is working ",Toast.LENGTH_LONG).show();
               
            }
        });
       
       
    }
}


2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="@string/button" />

</RelativeLayout>






















 Output
                                                                          






 
 



No comments:

Post a Comment