Translate

Thursday, July 24, 2014

Basic Android Programming Codes !!

THE FIRST STEP OF ANDROID PROGRAMMING :

Toast Function :

This function is mainly used to display a notification message under the bottom of the screen. It is used to make sure that the process has been done successfully. It is already known to us in various formats like messaging,alarm activity,network error, log in successful etc. 
This can be depicted as follows;
                                             

               (The Image shown here is an XML file on android and that is to be programmed separately)

The code to display this function is;

 package com.example.(Your Application Name);
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 {
Button b;
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() 
{

public void onClick(View arg0) 
{
Toast.makeText(getBaseContext(), "Button clicked...", Toast.LENGTH_LONG).show();
}
});
}
}

  • The code "import android.widget.Toast;" is the header file for this toast function.
  • The statement "Button b;" is the declaration of the particular button,where b is the variable.
  • The statement "b=(Button)findViewById(R.id.button1);" is used to find the exact button with an id (id will be present in the generated file path(Gen)) and to perform action on that button.
The main Statement is Toast.makeText(getBaseContext(), "Button clicked...", Toast.LENGTH_LONG).show();

It performs the toast operation .LENGTH_LONG is for displaying the message long time [upto 5s],whereas LENGTH_SHORT is for displaying the message to a short time [upto 3s]

For Further Clarifications and your satisfaction plz comment below:-)

No comments:

Post a Comment