HELLO ANDROID

In this post we create a HELLO ANDROID program which contain a text field and a button.
the text that we write in the text field will display on screen by clicking the button.

—main.xml—



    

    
    
    
        
    

—string.xml—


   
    Helloandroid
     Enter your name:
    say hii
    Please enter your name.OKHi %s!

—HelloandroidActivity.java—

package android.android1;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HelloandroidActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
private Button hibutton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        hibutton = (Button)findViewById(R.id.hi_button);
        hibutton.setOnClickListener(this);
    }

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText nameField=(EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
//if name not enter then show error message
if (name.length() == 0) {
new AlertDialog.Builder(this).setMessage(R.string.error_name_missing.setNeutralButton(R.string.error_ok, null).show();
return;
}
       if (v == hibutton)
        {
        
int resourceId = v == hibutton ? R.string.hi_greeting: R.string.app_name;
        String greeting = getResources().getString(resourceId, name);
        Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();
        TextView greetingField = (TextView) findViewById(R.id.greeting_field);
        greetingField.setText(greeting);
}
}
}