Monday, December 05, 2011
Android Graphical User Interface
1. We want to create a button named "Open" and second button named "Save".
Coding of filename.java :
//Open Button View
Button btnOpen=(Button) findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You clicked on Open button.",Toast.LENGTH_SHORT).show();
}
});
//Save Button View
Button btnSave=(Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You clicked on Save button.",Toast.LENGTH_SHORT).show();
}
});
Coding of main.xml file :
<Button
android:id="@+id/btnOpen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open" />
<Button
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save" />
Output :
2. We want to create a checkbox with text "Remember Me".
Coding of filename.java :
//CheckBox view
CheckBox chkbox=(CheckBox) findViewById(R.id.chkbox);
chkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked())
Toast.makeText(getBaseContext(), "Checkbox is checked.",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Checkbox is not checked.",Toast.LENGTH_SHORT).show();
}
});
Coding of main.xml file :
<CheckBox
android:id="@+id/chkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Me" />
Output :
3. We want to create a textbox(or EditBox).
Coding of main.xml file :
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Your Name" />
There is no need to write any code in "filename.java"(source file) for create textbox.
Output :
Note :
#For Source File :
Copy above code and paste in body section of onCreate() method.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
----------PASTE HERE---------
}
#For XML File :
Copy above code and paste between LinearLayout start tag and end tag.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
-----------------PASTE HERE-------------
</LinearLayout>
1. We want to create a button named "Open" and second button named "Save".
Coding of filename.java :
//Open Button View
Button btnOpen=(Button) findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You clicked on Open button.",Toast.LENGTH_SHORT).show();
}
});
//Save Button View
Button btnSave=(Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You clicked on Save button.",Toast.LENGTH_SHORT).show();
}
});
Coding of main.xml file :
<Button
android:id="@+id/btnOpen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open" />
<Button
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save" />
Output :
2. We want to create a checkbox with text "Remember Me".
Coding of filename.java :
//CheckBox view
CheckBox chkbox=(CheckBox) findViewById(R.id.chkbox);
chkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked())
Toast.makeText(getBaseContext(), "Checkbox is checked.",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Checkbox is not checked.",Toast.LENGTH_SHORT).show();
}
});
Coding of main.xml file :
<CheckBox
android:id="@+id/chkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Me" />
Output :
3. We want to create a textbox(or EditBox).
Coding of main.xml file :
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Your Name" />
There is no need to write any code in "filename.java"(source file) for create textbox.
Output :
Note :
#For Source File :
Copy above code and paste in body section of onCreate() method.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
----------PASTE HERE---------
}
#For XML File :
Copy above code and paste between LinearLayout start tag and end tag.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
-----------------PASTE HERE-------------
</LinearLayout>

Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →
Related Posts:
Subscribe to:
Post Comments (Atom)
0 comments: