Thursday, September 19, 2013

ANDROID PROGRAMMING 2

1. Running your application

+ AndroidManifest.xml: describes the basic characteristics of the app. You should set the  android:targetSdkVersion as high as possible and test your app on the corresponding platform version.
+ src/: contains main source files.
+ res/: contains some sub-directories for app resources. Here are just a few:
        ++ drawable-hdpi/: for drawable objects that are designed for high-density (hdpi) screen.
        ++ layout/: for files that define your app's user interface.
        ++ values/: for other various XML files that contain a collection of resources, such as string and color definitions.
2. Steps to compose a basic application
     2.1. Create a layout in res/layout/activity_main.xml
LinearLayout is a view group:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
</LinearLayout>
Some components inside the LinearLayout, each components should have an "id":
<EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_message" /> 
<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
     2.2. Add string resources in res/values/strings.xml
File strings.xml to store just string variables!
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>
     2.3. Starting another activity
a. Add corresponding method in the src/MainActivity class: 
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    ...
    public void sendMessage(View view){
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
    }}
 Another way:
           Intent i = new Intent();
            i.setClass(MainActivity.this, DisplayMessageActivity.class);
            startActivity(i);
Notes:
+ Intent(from, to): is used to start another activity.
+ findViewByID(id) returns a view by its id.
+ gen/R.java: has all resources ids provided to resources.
+ getText(): get the text label of a view component.
+ Intent.putExtra(variable_name, value): push a variable into Intent.
b. Create a new src/DisplayMessageActivity and a new res/layout/activity_display_message.xml file:
+ src/DisplayMessageActivity:
       HierachicalParent: point to com.example.myfirstapp.MainActivity
+ res/layout/activity_display_message.xml: 
       Anything arrangement you like, but there must be an element with id "ketqua".
c. Modify the onCreate() method in DisplayMessageActivity to display the intent data:
import android.widget.TextView;
import android.content.Intent;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    //setContentView(textView);
    setContentView(R.layout.activity_display_message);
    TextView textView2 = (TextView) findViewById(R.id.ketqua);
    textView2.setText(message);
}

3. Some style modifications
Font:
android:textColor="#ff0000" 
android:textColorHightlight="#66ccff"
android:textSize="12dp"
android:text="Arial"
android:textStyle="Bold"
Color State List resource:
<!-- in res/color/nutnhan.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>
<!-- in res/layout/xyz.xml -->
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />
Image background:
    android:background="@drawable/hinhnen"
Alignment, padding, margin:
    android:layout_centerInParent="true" 
    android:padding="30dip" 
    android:layout_margin="6dip"
    android:gravity="center_horizontal|center_vertical"
4. Defining layout for a set of views
LinearLayout – Positions child views in a single row or column depending on the orientation selected. A weight value can be set on each child to specify how much of the layout space that child should occupy relative to other children.

TableLayout – Arranges child views into a grid format of rows and columns. Each row within a table is represented by a TableRow object child, which, in turn, contains a view object for each cell.

FrameLayout – The purpose of the FrameLayout is to allocate an area of screen, typically for the purposes of displaying a single view. If multiple child views are added they will, by default, appear on top of each other positioned in the top left hand corner of the layout area. Alternate positioning of individual child views can be achieved by setting gravity values on each child. For example, setting a center_vertical gravity on a child will cause it to be positioned in the vertical center of the containing FrameLayout view.

RelativeLayout – Probably the most powerful and flexible of the layout managers, this allows child views to be positioned relative both to each other and the containing layout view through the specification of alignments and margins on child views. For example, child View A may be configured to be positioned in the vertical and horizontal center of the containing RelativeLayout view. View B, on the other hand, might also be configured to be centered horizontally within the layout view, but positioned 30 pixels above the top edge of View A, thereby making the vertical position relative to that of View A. The RelativeLayout manager can be of particular use when designing a user interface that must work on a variety of screen sizes and orientations.

GridLayout – The GridLayout is a relatively new layout manager that was introduced as part of Android 4.0. A GridLayout instance is divided by invisible lines that form a grid containing rows and columns of cells. Child views are then placed in cells and may be configured to cover multiple cells both horizontally and vertically allowing a wide range of layout options to be quickly and easily implemented. Gaps between components in a GridLayout may be implemented by placing a special type of view called a Space view into adjacent cells, or by setting margin parameters.
5. Some useful links
A simple Android Splash Screen 
Android Service Tutorial
Style and Theme

0 comments:

Post a Comment