Lets begin by creating a new project on eclipse:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:text="Welcome to Android!"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<EditText
android:id="@+id/sampleText"
android:text=""
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="@+id/popupSampleText"
android:text="Pop up"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
- TextView - is basically an element that is used to display text on screen or to the user. It is one of the most basic views and most commonly used in developing an android application. Also, do take note that the text inside this view type is not editable.
- EditText - is another type of view that is used for character/text input such as name or passwords.
- Button - it represents a push-button widget which can be pushed or clicked by the user to perform an activity, an event or an action.
package com.app.techie;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BasicViewsPractice extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basicviews);
Button popUp = (Button) findViewById(R.id.popupSampleText);
popUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView sampleText = (TextView) findViewById(R.id.sampleText);
if(0 != sampleText.getText().toString().length()){
Toast.makeText(getBaseContext(), sampleText.getText().toString(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "The textbox is empty!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<EditText
android:id="@+id/sampleTextPassword"
android:text="sample"
android:password="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/sampleImageButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
Lets do another update by adding the following code:
<CheckBox android:id="@+id/chkSample"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Checkbox Sample"
/>
<CheckBox android:id="@+id/chkStarLook"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Star checkbox"
/>
CheckBox chkSample = (CheckBox) findViewById(R.id.chkSample);
chkSample.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox) v).isChecked()){
Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show();
}
}
});
That suffice how you are going to verify a chechbox view state using java code. That same code also applies to the star checkbox. Just change the id of the checkbox on your java code. Try running the code and you should be able to see something like below.
<ToggleButton android:id="@+id/tglSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light switch"
/>
To be able to check its current state, add the following code below on the BasicViewsPractice.java.
ToggleButton tglSample = (ToggleButton) findViewById(R.id.tglSample);
tglSample.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((ToggleButton) v).isChecked()){
Toast.makeText(getBaseContext(), "Light On", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Light Off", Toast.LENGTH_SHORT).show();
}
}
});
As you can see above, its more or less like the code on checking a checkbox view. Try running the code and see for yourself.
Nutshell:
In this tutorial we were able to learn and experiment on textView, edittext, button, imagebutton, checkbox and toggle button. However, there are still more views to learn and they will be discussed in detail my next article. Hope you learned from this article. Enjoy!
0 comments :
Post a Comment