Saturday, 1 December 2012

How to use TextView,Button in your app


    Today,its time to move on to learn,How to use some basic control in our app.its very easy to play with every basic control form widget,Layout,form field an all.
     Only we have to keep in mind that, as we know in these world individuality is known as our name ... like its an id for us,in somewhat,okay.
    samely,while we using every control in our app ,we need to assign unique id for it.  But How???............ that i l show u today on my these blog.
so fasten your seat belt.. n lets start to fly in the magical world of android ... Best of Luck :)


Okay,in android app as we know very well every control of android is designed is presented in main.xml (present in res->layout folder).

In main.xml, we have one root tag as linearlayout. our all controls are inside of these tag.we can learn the nature of main.xml by two ways.like graphical & actual coding way.

How to add TextView in your main.xml file.... 

1) click on graphical when you are on main.xml file.

2) then drag n drop one textview on your main.xml file as follows


fig: main.xml_1

3) now double click on your textview,you will get code for it... 
     
    we can use many control  for your textview.... like
    
    * you can set text for your textview as.....................
               android:text="MY NAME IS Sushant " 
by these way you can set any string for your textview.

   *  also, 
                android:gravity="left"
      your textview will be set on the left side of your linear layout.
      

       try also.......
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="100dp"


                                             fig: main.xml_2

you can even set your height n width for your control... with parameter 

      * wrap_content=it allocates that much size form linearlayout as much as it needed.

      * fill_parent=it allocates as much as it parents contain(Here parent will be LinearLayout)


& we can assign unique id for your control .when you add every control on your main.xml file...it comes with one default id.you can modified it.no issue.


*******how to use of id... ?

In java code., just written these code 

        TextView txt=(TextView) findViewById(R.id.textview1);   

it means... we are using id of textview main.xml in our java code.
its easy....

Run your app... freely.............. :)



***************************


Lets Go, For   Button .....................................



1) samely add Button on your main.xml file.



fig: main.xml with Butoon

2)Here, we will be coding like when we will clicking on button ... one toast will come on your screen with pop-up.like "You click on Button"

&, its coded like.....................

in java code.. which is presented on src folder.... 


package com.examples;

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

public class Basic_ViewActivity extends Activity {
    /** Called when the activity is first created. */
Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button) findViewById(R.id.button1);
        
        
        btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Basic_ViewActivity.this,"You Click on Button",Toast.LENGTH_SHORT).show();
}
});
    }
}



if you are good in java,then you know how to use onClickListener .samely its work in android.

& Toast is used to show pop-up on user screen.we using  Toast. maketext(this,"Yourstring",Toast.LENGTH_SHORT).show();

Run Your app... & app will look like



fig: Your app with Toast






Stay tunned :) , Happy Coding