Sunday, July 4, 2010

Customizing Buttons

We can customize the buttons easily. Just change the background of it and specify the drawable images in the place of button.

<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/backbtn"></Button>

Weight

If we have a apple and need to divide in to 4 parts we will cut that in to 0.25 pieces. In the same way if we want to divide a parent layout in to parts then we need to specify the total layout weight for parent layout and we need to specify the each small part in the whole total weight.

For example if the total layout needs to divide into 3 equal parts we can specify the total layout as weight as 3 and the child layout weight as ‘1’ for all the three Childs.

The below example shows how to divide a layout into 3 diff sizes using weight.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:weightSum="4">

<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></Button>

<Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="2"></Button>

<Button android:text="@+id/Button03" android:id="@+id/Button03" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></Button>

</LinearLayout>


Gravity

We can specify the Fields style over here. We have lot of properties and we can easily customize the layouts.

Absolute Layout

This layout is deprecated and only supports for a specific resolution. We cannot use this layout if we concentrated our app to work on all the resolutions.

Relative Layout

This layout is used to arrange the fields according to the other field. Here we can specify the left, right, top and bottom fields. According to which the other fields will be arranged.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Button01" android:text="@+id/Button01" android:layout_toRightOf="@+id/EditText01"></Button><CheckBox android:layout_width="wrap_content" android:id="@+id/CheckBox01" android:layout_height="wrap_content" android:layout_below="@+id/EditText01" android:text="@+id/CheckBox01"></CheckBox>

<ImageView android:layout_below="@+id/EditText01" android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/CheckBox01" android:background="@drawable/icon"></ImageView>

</RelativeLayout>


Table Layout

This Layout is used when ever we want to create the screen with Table rows and columns

TabHost

This is used to create the Tabs as like as you think. But this is fully Buggy. I really worked hard on this to make it work. But, it’s having lot of issues it won’t work properly.

We are unable to work on the ADT plug-in for Tabs. Really its buggy. It will through NullpointerException, while displaying the screen. Don’t bother about the Nullpointer just run away the code and enjoy the execution.

Most of the time the XML execution Fails for TabHost and TabWidget. I tried to work on this for Android 1.5 SDK. 90% Fail.

We can handle this by using the code successfully. This one is awesome and easy to handle. I attached the code over here. You can get the both approaches i.e.,

1. XML approach

2. Code approach

TabHost is used to create the Object for Tab Manager. TabSpec Objects will be added to the TabHost in order to add the Tabs.

Code Approach:

final TabHost tabHost = getTabHost();

tabHost.addTab(tabHost.newTabSpec("TAB1")

.setIndicator("Name of the TAB")

.setContent(new Intent(this,XYZ.class)));

tabHost.addTab(tabHost.newTabSpec("TAB2")

.setIndicator("Name of the TAB")

.setContent(new Intent(this,ABC.class));

XML Approach: You can check it in the below linked source

Source Code for TabHost with source here or here
Source Code for TabHost with XML
here
or here

Android Custom Tab Handling is introdued in Android 1.6. Here you can customize each tab including heights of each tab.

Portrait and Landscape Layouts

If you want to handle landscape devices then create a “layout-land” named folder in project “res” folder and create the XML files related to landscape devices with same name just like in layout folder and customize it accordingly.

High,Medum and Low Resolution

Android sdk 1.6 supports multi dimensions. For which we need to create the below folders to handle different dimensions.

“drawable-hdpi” ---------- for higher Resolutions,

” drawable-ldpi” ---------- for lower Resolutions,

” drawable-mdpi” -------- for medium resolutions.

Here we can place the Images to different dimensions and use thw same name for them.

Activity Calls / Data Passing / Object Passing bet Activities

Setting UI to screen

Whatever the user interface you need, just create it in XML files. Specify the name of XML from source code just as shown below.

setContentView(R.layout.XML_FILE_NAME);

Calling an Activity

Starting an activity can be done by creating an intent as shown below.

Intent i = new Intent(“this activity name”.this, “activity name to start”.class);

startActivity(i);

Finishing the Activity

Use the following code, then it will finish the current Activity. If you move the Activity to the next Activity then you can easily move to the previous Activity by using Back Button. If you don’t want it to Re-appear again, then just finish the activity so that the activity doesn’t appear back.

For example if you are displaying the splash and that should be displayed only once @ starting the app only. So after completion of the splash time we need to finish it. So that it won’t appear again even though you press back on the first page of the app.

finish();

Sending string data

As we already know that we can transfer from one to other Activity by using Intents.

Intent i = new Intent(MainActivity.this,GoActivity.class);

String data = “bla bla”;// Data String to send

i.putExtra("DATA",data );

Receiving the Data

We can receive the above the string data as shown below.

Intent i = getIntent();

String data = i.getStringExtra("DATA");// you well get the String data i.e., BLA BLA

The Red Colored DATA should be same while sending and receiving the data.

Sending Object

Here is the code to send the Object from one to other class. One Important thing to send the Object is the class should implement the Serializable class.

The below Red Colored text should be same.

//MainActivity.java

Intent i = new Intent(MainActivity.this,startActivity.class);

ObjectClassName object = new ObjectClassName();

i.putExtra("THIS", Object);

Receiving Object

// startActivity.java

Intent i = getIntent();

ObjectClassName obj = (ObjectClassName) getIntent().getSerializableExtra("THIS");// TypeCasting needed

ListActivity Example

The below code is used to create the normal list with String Array .

public class MainActivity extends ListActivity {

static final String[] COUNTRIES = new String[] {

“ABC”,”DEF”,”GHI”,”XYZ”

};

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, COUNTRIES));

getListView().setTextFilterEnabled(true);

}

}

This won’t has customization or anything. If you want customized list with different Images or Text or anything you can get it in the next example.



Next Post will contain the Internet Connectivity and Customized List with diff Images and XML parsing with src.


Thanks Guys,

3 comments: