At last, it is time to start coding! You need to know the details about auto generated code for your application. Go to the “src” folder. In this folder, you will find the package folder, open the HelloWorld.java file. You will see the default code for an Android Activity:
Also read: GTA 3 APK For Android
package com.test.helloworld; //the package we are working in
//some android packages we need to import
import android.app.Activity;
import android.os.Bundle;
//our activity class (extendes the default activity class)
public class HelloWorldApp extends Activity {
/** Called when the activity is first created. */
@Override
//the function called when activity is created
public void onCreate(Bundle savedInstanceState) {
//call the create fct. Of the base class
super.onCreate(savedInstanceState);
//load the layout specified in the layout.xml
setContentView(R.layout.main);
}
}
As you can see, we create a new activity by extending the default Android activity class. Then we override the default onCreate
function, which is called when the project is created. In there, we load our own layout from the resources and also call the onCreate
function of the base class. Now let’s take a closer look at the layout file. You find it in the layout folder under resources e.g res\layout\main.xml. When you open it, it should look like this:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
</LinearLayout>
- RelativeLayout: The positions of the children are specified in relation to the other children.
- TableLayout: The child elements are placed with a grid.
- AbsoluteLayout: The child elements are positioned based on absolute coordinates (in pixel).
Once you have chosen a layout type, you can add child elements. In the code given, there is already a TextView, which is used to display text on the screen. The current content is a reference to a resource defined in the values.xml file. As you will see, it uses the whole width of the screen but is only as long as it needs to, to display the content. We might start with some small changes. We have also shared, Kingoroot Apk.
Let’s change the text color of the TextView to green:
<textview android:layout_height=”wrap_content”
android:layout_width=”fill_parent” android:text=”@string/hello”
android:textcolor=”#FF00FF00″ />
Now, launch the project and see the changes. Next, let’s add a new control called EditText:
<linearlayout android:layout_height=”fill_parent”
android:layout_width=”fill_parent” android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android” />
<edittext android:layout_height=”wrap_content”
android:layout_width=”fill_parent” android:textcolor=”#FF0000FF”
android:id=”@+id/et_Text” />
<textview android:layout_height=”wrap_content” android:layout_width=”fill_parent”
android:textcolor=”#FF00FF00″ android:id=”@+id/lv_View” />
</linearlayout />
When we want to access the controls in code, they need to have an ID. Next we create some code for the controls. Go to the HelloworldApp.java file.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//loading the layout over the resource reference
setContentView(R.layout.main);
//get the two controls we created earlier, also with the resource reference and the id
final TextView tv_View = (TextView)findViewById(R.id.tv_View);
final EditText et_Text = (EditText)findViewById(R.id.et_Text);
//add new KeyListener Callback (to record key input)
et_Text.setOnKeyListener(new OnKeyListener()
{
//function to invoke when a key is pressed
public boolean onKey(View v, int keyCode, KeyEvent event)
{
//check if there is
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
//check if the right key was pressed
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//add the text to the textview
tv_View.setText(tv_View.getText() + “, ” +
et_Text.getText());
//and clear the EditText control
et_Text.setText(“”);
return true;
}
}
return false;
}
});
}
We have also shared Android Screen Orientation Change.
We will analyze the code line by line. First of all, as before, we load the layout. Then we create a TextView and an EditText variable and load our interface objects in them (that’s what we need the ID for). Finally, we add a new OnKeyListener to the EditText control.
In this OnKeyListener, we create the method onKey, which is called when a key is pressed, when the control is active. In the method, we perform two checks: the first to be sure that a key is pressed down (and not released), and the second to specify the key (in this case, the center key of the D-pad). If both checks are passed, we add the text of the EditText control to the TextView, and finally, the text of the EditText control is deleted. Run and test the application. Great, you created your first real Android app.
