Uncategorized

Android Gravity and Layout Gravity

Posted by:

Gravity is an android method for aligning view in a layout. There are two kinds of gravity, gravity and layout gravity. Basically a gravity is set in xml layout files, but you can also set a gravity of any view in java source code. To set gravity in xml use [cci]android:layout_gravity[/cci] and [cci]android:gravity[/cci] attributes. The values of both attributes is combination of the following constants:

top : Push object to the top of its container, not changing its size.
bottom : Push object to the bottom of its container, not changing its size.
left : Push object to the left of its container, not changing its size.
right : Push object to the right of its container, not changing its size.
center_vertical : Place object in the vertical center of its container, not changing its size.
fill_vertical : Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal : Place object in the horizontal center of its container, not changing its size.
fill_horizontal : Grow the horizontal size of the object if needed so it completely fills its container.
center : Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill : Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical : Additional option that can be set to have the top and/or bottom edges of the child clipped to its container’s bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal : Additional option that can be set to have the left and/or right edges of the child clipped to its container’s bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
start : Push object to the beginning of its container, not changing its size.
end : Push object to the end of its container, not changing its size.

 

android:gravity attribute

[cci]android:gravity[/cci] will set alignment to the content of a view. This aligment will only take effect if size of view is bigger than view’s content. Maybe you got confuse, what is the different between view and view content. Consider a button view below.

Button View

Button View

As you can see from image above, the button view is the rectangle boundary of a button, while the content of the button view is the text “button” it self.

Now, we will align the content of the button to bottom right of the button. To align content to bottom right we add [cci]android:gravity=”bottom|right”[/cci] attribute.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="Button"
        android:gravity="bottom|right" />

</LinearLayout>

Will result as follow:

Content of button view aligned to bottom right

Content of button view aligned to bottom right

android:layout_gravity attribute

[cci]android:layout_gravity[/cci] is used to align view child to its parent. Note that this attribute will only take effect if the parent is [cci]LinearLayout[/cci] and the size of child smaller than parent size.


 

Now we will align a button to the right of parent. To do this, we must add [cci]android:layout_gravity=”right”[/cci] attribute.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="right" />

</LinearLayout>

The result is as follow:

Button view is aligned to right

Button view is aligned to right

0

Advance Android Relative Layout Example

Posted by:

Relative Layout is an android view group to align all childrens relative to other view or its parent. Relative layout give you flexibiliy to place childs wherever you want. In the previous example, I have give some basic example of Relative Layout. Now, I will give you how to create a more complex relative layout example.

Our goal is to create layout like this:

Advance relative layout example

Advance relative layout example

See the following step by step to create above layout:

1. Aligning child to top left of parent

Actually, by default every childs is aligned to top left of parent in relative layout. So we needn’t add any special attribute to align chilc in top left of parent. But you can align child to top left of parent by adding [cci]android:layout_alignParentLeft=”true”[/cci] and [cci]android:layout_alignParentTop=”true”[/cci] attributes.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

 

2. Aligning child in top right of parent

To align child in top right of parent, add the following attributes [cci]android:layout_alignParentRight=”true”[/cci] and [cci]android:layout_alignParentTop=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

 

3. Aligning child in bottom left of parent

To align child in bottom left of parent, add the following attributes [cci]android:layout_alignParentBottom=”true”[/cci] and [cci]android:layout_alignParentLeft=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

 

4. Aligning child in bottom right of parent

To align child in bottom right of parent, add the following attributes [cci]android:layout_alignParentBottom=”true”[/cci] and [cci]android:layout_alignParentRight=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

 

5. Align child in the center of parent

This is our center child and you must give it an [cci]id[/cci] as our other 4 childs aligned relative this child. To align a child center of the parent, add the following attribute [cci]android:layout_centerInParent=”true”[/cci]. This attribute will align our child center to the parent, vertically and horizontally.

<Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button5"
        android:layout_centerInParent="true"/>

 

6. Align child in top left of the center child

To align child in top left of the center child, add the following attributes [cci]android:layout_toLeftOf=”@id/button5″[/cci] and [cci]android:layout_above=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_above="@id/button5"/>

 


 

7. Align child in top right of the center child

To align child in top right of the center child, add the following attributes [cci]android:layout_toRightOf=”@id/button5″[/cci] and [cci]android:layout_above=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toRightOf="@id/button5"
        android:layout_above="@id/button5"/>

 

8. Align child in bottom right of the center child

To align child in bottom right of the center child, add the following attributes [cci]android:layout_toRightOf=”@id/button5″[/cci] and [cci]android:layout_below=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toRightOf="@id/button5"
        android:layout_below="@id/button5"/>

 

9. Align child in bottom left of the center child

To align child in bottom left of the center child, add the following attributes [cci]android:layout_toLeftOf=”@id/button5″[/cci] and [cci]android:layout_below=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_below="@id/button5"/>

 

Final Source Codes

Below is the final source codes:

<RelativeLayout 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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />
    
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />
    
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button5"
        android:layout_centerInParent="true"/>
    
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_above="@id/button5"/>
    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button7"
        android:layout_toRightOf="@id/button5"
        android:layout_above="@id/button5"/>
    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button8"
        android:layout_toRightOf="@id/button5"
        android:layout_below="@id/button5"/>
    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button9"
        android:layout_toLeftOf="@id/button5"
        android:layout_below="@id/button5"/>
</RelativeLayout>
0

Android Linear Layout Example

Posted by:

Linear Layout is an android View Group that aligns all children in single oriented manner, vertically or horizontally. A Linear layout can have only one orientation, only vertical or only horizontal. The orientation can be specified by using attribute [cci]android:orientation[/cci].

Horizontal Orientation

By default, if you don’t specify the attribute [cci]android:orientation[/cci], the orientation of linear layout will be horizontal. But you can put [cci]android:orientation=”horizontal”[/cci] to set horizontal orientation. For example:

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world1"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world2"
         />
</LinearLayout>

The result of above layout is:

Linear layout example

Linear layout example

Vertical Orientation

To set the orieantation of linear layout to vertical, add [cci]android:orientation=”vertical”[/cci] to the View Group. For example:

<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="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world1"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world2"
         />
</LinearLayout>

The result of above layout is:

Linear layout example

Linear layout example


 

Flexible Width or Hight of Children

Linear Layout support flexibility of width or height for its child to consume remaining space in the parent. Flexible width only supported in horizontal orientation and flexible height only supported in vertical orietation. Use [cci]android:layout_weight[/cci] to make a child flexible. The value of the attribute is an integer value. The larger number of [cci]android:layout_weight[/cci] of a child, it consumes more extra space in the parent. Default value of the attributes in 0.

Flexible Width in Horizontal Orientation

To make width of a child flexible, you must set it width to [cci]0dp[/cci] and layout_weight greater than 0.

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout weight example

Layout weight example

Flexible Height in Vertical Orientation

To make height of a child flexible, you must set it height to [cci]0dp[/cci] and layout_weight greater than 0.

<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="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout weight example on vertical orientation

Layout weight example on vertical orientation

Setting margin to child

Linear Layout respects to margin attribute on child. To set margin, using [cci]android:layout_margin[/cci]. This attribute will apply to all 4 sides, top, right,bottom and left. If you need just apply margin with specified side, you can use [cci]android:layout_marginLeft[/cci],[cci]android:layout_marginTop[/cci],[cci]android:layout_marginRight[/cci],[cci]android:layout_marginBottom[/cci]. Note, you must put margin value in [cci]dp[/cci] unit.
Here is an example:

<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="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout margin example

Layout margin example

Setting gravity to child

Another feature of Linear Layout is gravity of a child. Gravity is an aligment of child with its parent. I will give tutorial about gravity in the next tutorial.

0

Android Relative Layout example

Posted by:

[cci]RelativeLayout[/cci] is one of android container or [cci]ViewGroup[/cci]. Like it’s name, the main feature of [cci]Relativelayout[/cci] is to place one [cci]View[/cci] relative to other [cci]View[/cci] or with it’s parent it’s self.

Place a view relative to other view

In order to make a relative position of one view to another view, an [cci]id[/cci] is must be defined in referred view. Says you have two [cci]TextView[/cci] in layout, you want second [cci]TextView[/cci] placed to the right of first [cci]TextView[/cci] but the top of second [cci]TextView[/cci] is place to bottom of first [cci]TextView[/cci]. To do this, you must define an [cci]id[/cci] to the first [cci]TextView[/cci].

<RelativeLayout 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">
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView1"
        android:layout_below="@id/textView1"        
        android:text="@string/hay"
         />
</RelativeLayout>

In above source code, we have put 2 [cci]TextView[/cci]. The frist [cci]TextView[/cci] have an id [cci]textView1[/cci]. Now, take a look at hightlighted line of the above source code. The line [cci]android:layout_toRightOf=”@id/textView1″[/cci] means set the position of [cci]TextView[/cci] to the right of first [cci]TextView[/cci]. While [cci]android:layout_below=”@id/textView1″[/cci] means place second [cci]TextView[/cci] to the bottom of first [cci]TextView[/cci].
Here the result of above layout:

Relative layuot exampl

Relative layuot exampl

There are more attributes that can be used to make relative position between views. Here are the complete list:

  • [cci]android:layout_toLeftOf[/cci] Positions the right edge of this view to the left of the given anchor view ID.
  • [cci]android:layout_toRightOf[/cci] Positions the left edge of this view to the right of the given anchor view ID.
  • [cci]android:layout_above[/cci] Positions the bottom edge of this view above the given anchor view ID.
  • [cci]android:layout_below[/cci] Positions the bottom edge of this view below the given anchor view ID.
  • [cci]android:layout_alignBaseline[/cci] Positions the baseline of this view on the baseline of the given anchor view ID.
  • [cci]android:layout_alignLeft[/cci] Makes the left edge of this view match the left edge of the given anchor view ID.
  • [cci]android:layout_alignTop[/cci] Makes the top edge of this view match the top edge of the given anchor view ID.
  • [cci]android:layout_alignRight[/cci] Makes the right edge of this view match the right edge of the given anchor view ID.
  • [cci]android:layout_alignBottom[/cci] Makes the bottom edge of this view match the bottom edge of the given anchor view ID.
  • [cci]android:layout_toStartOf[/cci] Positions the end edge of this view to the start of the given anchor view ID.
  • [cci]android:layout_toEndOf[/cci] Positions the start edge of this view to the end of the given anchor view ID.
  • [cci]android:layout_alignStart[/cci] Makes the start edge of this view match the start edge of the given anchor view ID.
  • [cci]android:layout_alignEnd[/cci] Makes the end edge of this view match the end edge of the given anchor view ID.

 

Place a view relative to it’s parent

Another relative layout capability is place a view relative to it’s parent. This means you can make bottom edge of a view match bottom edge of its parent. Just add simple attribute [cci]android:layout_alignParentBottom=”true”[/cci] to your view then your view automatically aligned to its parent. I will give an example how to do this in code.

<RelativeLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

Here the result of above layout:

Relative layout example

Relative layout example

Except [cci]android:layout_alignParentBottom=”true”[/cci], there are another attributes for aligning to parent. Here are complete list

  • [cci]android:layout_alignParentTop=”true”[/cci] If true, makes the top edge of this view match the top edge of the parent.
  • [cci]android:layout_alignParentLeft=”true”[/cci] If true, makes the left edge of this view match the left edge of the parent.
  • [cci]android:layout_alignParentRight=”true”[/cci] If true, makes the right edge of this view match the right edge of the parent.
  • [cci]android:layout_alignParentBottom=”true”[/cci] If true, makes the bottom edge of this view match the bottom edge of the parent.
  • [cci]android:layout_alignParentTop=”true”[/cci] If true, makes the top edge of this view match the top edge of the parent.
  • [cci]android:layout_alignParentStart=”true”[/cci] If true, makes the start edge of this view match the start edge of the parent.
  • [cci]android:layout_alignParentEnd=”true”[/cci] If true, makes the end edge of this view match the end edge of the parent.
0

Give an ID to your view in android xml file

Posted by:

Give an ID to your view object is very useful if you want to refer to that view later in source code. For example you want to set text of a TextView in source code. To do that, put [cci]android:id[/cci] attibute to your view.

<RelativeLayout 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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
</RelativeLayout>

In above code, you can see I have put an [cci]android:id=”@+id/textView1″[/cci]. This line defines an [cci]id[/cci] of our [cci]TextView[/cci] to [cci]textView1[/cci].

Refers view from activity

After you have defined an id, now you can refer to your [cci]TextView[/cci] in an activity like this

TextView textView = (TextView) findViewById(R.id.textView1);

Now, with your [cci]TextView[/cci] in hand, you can set text to it as you want like this

if(textView != null) textView.setText("new text");

 

Refers view in xml file

Sometimes we need to refers to our view in the xml layout file. For example you want to align top another [cci]TextView[/cci] to other. To achieve this, you can refer to our last [cci]TextView[/cci] like this [cci]@id/textView1[/cci]. Note you must replace ‘+’ sign when referring to already defined [cci]id[/cci]. Use ‘+’ sign only in first time when you define an [cci]id[/cci].

<RelativeLayout 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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/textView1"/>
0

Android hello word application

Posted by:

This tutorial will guide you to create a simple hello word application in android. Through this tutorial you will learn about the basic understanding of android activity and layout. Most common tool to create android application is Eclipse ADT plugin. You can follow step by step how to install eclipse ADT Plugin in the official developer site here.

After your eclipse ADT plugin was installed, now you’re ready to create android Hello Word project. In eclipse, select menu

File->New->Other

This will open a wizard.

android project wizard

android project wizard

On the wizard, open  Android sub tree and choose Android Applicaton Project then click Next.

android new project

android new project

The wizard will prompt you to fill below informations:

  1. Application Name: this name will shown in the google play store
  2. Project Name: name used by eclipse, must be unique in a workspace
  3. Package Name: this is your application identifier, must be unique for each application.

The next screen will allow you to put icon for your application. Choose your application icon or just click next until you see this window:

create android activity

create android activity

Activity in android just like a screen that is visible to the user. For now, just choose Blank Activity. 

create android activity

create android activity

Now you must set Activity Name and Layout Name. Activity name is a class name for your activity and layout name is your layout file name. Click finish to close the wizard. Now, you will see a HelloWord project on eclipse project explorer. Here is the folder structure of HelloWord project.

android folder structure

android folder structure

Here is basic description about  project sub folders:

  1. src: contains your java source files
  2. bin: contains your compiled java class
  3. gen: contains android generated files
  4. libs: here you can place third party library (jar file)
  5. res: contains your projects’ resources, like images, icons, layouts, strings, etc

Now open [cci]res/layout/activity_main.xml[/cci] file, it will looks like this:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />

</RelativeLayout>

This file defines your layout structure. You can place your view object here. There are two categories of view in android, [cci]View[/cci] and [cci]ViewGroup[/cci] or container. [cci]View[/cci] object can be [cci]TextView, EditText, Spinner[/cci], etc while [cci]ViewGroup[/cci] acts as the [cci]View[/cci] container.

As you can see from above source code, it uses [cci]RelativeLayout[/cci] as main container. This layout places it’s childs relative to other childs. With this layout you can place any child overlapping to others.  Another most common layout is [cci]LinearLayout.[/cci] The main function of [cci]LinearLayout[/cci] is to  place their child in orientation manner, vertical or horizontal.

Either [cci]View[/cci] and [cci]ViewGroup[/cci] must have [cci]android:layout_width[/cci] and [cci]android:layout_height[/cci], these attributes respectively defines [cci]View[/cci] width and height. You can put value for both attributes using integer value in [cci]dp[/cci] such as [cci]0dp, 30dp, 100dp[/cci], etc. [cci]dp[/cci] or [cci]dip[/cci] is android unit which means [cci]density independent pixels[/cci]. Also you can put following constant value :

  1. [cci]MATCH_PARENT: [/cci]make the size of [cci]View[/cci] as big as it’s parent (container) minus padding.
  2. [cci]WRAP_CONTENT: [/cci]make the size of[cci] View [/cci]to match content size.

Now take a look inside [cci]RelativeLayout[/cci] at above code. There is one TextView inside with an attribute [cci]android:text:”@string/hello_word”[/cci], this attribute defines the text for [cci]TextView[/cci], but as you can see, the attribute value is not direct string but refers to string resources. You can find the string resource in folder [cci]res/values/string.xml[/cci].

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

    <string name="app_name">HelloWord</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

Android will search in the string resources for string that has name [cci]hello_word[/cci] and then use the value. Actually you can put direct string like this [cci]android:text:”Hello world!”[/cci], but the recommended way is using string resource to put your string.

Now how to tell android to use this layout? Goto [cci]src[/cci] folder, and open [cci]MainActivity.java[/cci],

package com.semurjengkol.helloword;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

To use you layout in activity, you must override [cci]onCreate[/cci] method of the [cci]Activity[/cci] class and then call [cci]setContentView(R.layout.activity_main);[/cci] now you can run your [cci]HelloWord[/cci] application, it’s will looks like this.

Android hello word app

Android hello word app

Download source code HelloWord

0

Tricky way to drop multiple table in oracle

Posted by:

Droping a table in oracle is very simple task. With just one row of sql statement you can drop a table. For example you want to drop [cci]school[/cci] table in database. Use this simple sql statement, the table will be dropped.

DROP TABLE school;

Dropping multiple tables

If you have multiple tables to drop, trivial way is adding the above row as much as total of tables. How if you have hunderds table to dropped? It’s no problem if you want to copy and paste the above statement and change the table name line by line. If you are lazy to do that, I will give some tricky way to generate the sql statement without copy paste then change the table name.

By using String Concatenation we can generate sql statement to drop multiple tables. If you are not familiar with string contatenation, I recommend you to read my previous tutorial string concatenation.

SELECT 'DROP TABLE ' || table_name || ';' from user_tables;

If you run the above statement, you will give result like this. The table name is depend on user currently logged in.

DROP TABLE P_SCHOOL;
DROP TABLE P_CLASS;
DROP TABLE P_DISK;
DROP TABLE P_CAT;
DROP TABLE P_STUDENT;
DROP TABLE P_RES;
DROP TABLE P_KIT;
DROP TABLE R_SCHOOL;
DROP TABLE R_MAS;
DROP TABLE R_KIT;
DROP TABLE R_ANG;
DROP TABLE R_GUS;
DROP TABLE R_AFTER;
DROP TABLE R_BEFORE;
DROP TABLE R_NIGHT;
DROP TABLE R_TODAY;
DROP TABLE R_COLUMN;
DROP TABLE P_LANG;
DROP TABLE P_CUBE;
DROP TABLE P_GUN;
DROP TABLE P_OPEN;
DROP TABLE P_CLOSE;
DROP TABLE P_MISS;
DROP TABLE P_CON;
DROP TABLE P_RIP;
DROP TABLE P_JEWEL;

Now you can copy paste the result statement and then execute it.

Generating statement conditionally

When generating sql statement, you can filter the generated statement to meet your requirement. For example, if you want to drop only table with suffix “P”. You can put where clause.

SELECT 'DROP TABLE ' || table_name || ';' FROM user_tables WHERE table_name LIKE 'P%';

The above statement will give result:

DROP TABLE P_SCHOOL;
DROP TABLE P_CLASS;
DROP TABLE P_DISK;
DROP TABLE P_CAT;
DROP TABLE P_STUDENT;
DROP TABLE P_RES;
DROP TABLE P_KIT;
DROP TABLE P_LANG;
DROP TABLE P_CUBE;
DROP TABLE P_GUN;
DROP TABLE P_OPEN;
DROP TABLE P_CLOSE;
DROP TABLE P_MISS;
DROP TABLE P_CON;
DROP TABLE P_RIP;
DROP TABLE P_JEWEL;
0

String concatenation in oracle

Posted by:

String concatenation is joining one or more string to build a new string. Oracle use [cci]||[/cci] to join two string.

SELECT 'semur' || 'jengkol' FROM dual

Above statement will result string [cci]semurjengkol[/cci]. Note, you must use single quote [cci]'[/cci] to represent a string in oracle or error will be shown.

Adding prefix to column result

String concatenation can be used to giving a prefix (or suffix) to column result. For example you have [cci]person[/cci] table contains 2 columns, [cci]ID[/cci] and [cci]NAME[/cci].

ID        NAME
1         Ahmad
2         Fatih
3         Umar

The statement below will give prefix “Mr.” to column [cci]NAME[/cci] int the result

SELECT ID, 'Mr. ' || NAME FROM person;

Here the result:

ID        NAME
1         Mr. Ahmad
2         Mr. Fatih
3         Mr. Umar

Joining more than two strings

As you can guest, joining more than two strings is trivial. Just separates all strings with [cci]||[/cci] operator.

SELECT ID, "Mr. " || NAME || ', you are great' FROM person;

And the result is:

ID        NAME
1         Mr. Ahmad, you are great
2         Mr. Fatih, you are great
3         Mr. Umar, you are great

Joining strings which contains quotes (‘ or “)

To join string which contains single quote ([cci]'[/cci]) or double quote ([cci]”[/cci]), you must repeat it twice. So you must write [cci]”[/cci] for single quote and [cci]””[/cci] for double quote. See below statement.

SELECT ID, '''Mr. ' || NAME || '''' || ' welcome to ""semurjengkol.com""' FROM person;

The statement will result:

ID        NAMA
1         'Mr. Ahmad' welcome to "semurjengkol.com"
2         'Mr. Fatih' welcome to "semurjengkol.com"
3         'Mr. Umar' welcome to "semurjengkol.com"
0

How to get domain name in javascript

Posted by:

Getting domain name from javascript is simple. You can get domain name from [cci]window.location.hostname[/cci] variable.

var hostname = window.location.hostname;
alert(hostname);

Click following button to test above code.

Get full domain name

Domain name you got this way is simply domain name in browser address bar. Sometimes address bar you got not include [cci]http://[/cci] or [cci]www[/cci] prefix. To make sure you get the full domain name, you must check it first.

var hostname = window.location.hostname;
hostname = (hostname.indexOf('www.')!=-1?hostname:'www.'+hostname);
hostname = (hostname.indexOf('http')!=-1?hostname:'http://'+hostname);
alert(hostname);

Click button belo to try above code:

0

Filter associative array based on array key in PHP

Posted by:

PHP has so many built in function to work with array. In this tutorial I would present to you how to filter an associative array based on array keys. For example, you have an associative array [cci]$groups[/cci]:

$groups = array(
    'a' => 'book',
    'b' => 'pencil',
    'c' => 'pen'
);

And array of keys:

$keys = array('a', 'b');

Now, how to get sub array of [cci]$groups[/cci] which key contained in [cci]$keys[/cci]? Our goal is to create new associative array like this:

$news = array(
    'a' => 'book',
    'b' => 'pencil'
);

Every keys in array [cci]$news[/cci] contained in [cci]$keys[/cci]. Here the way to do that:

$news = array();

foreach($groups as $k=>$v){
    if(in_array($k, $keys)){
        $news[$k]  = $v;
    }
}
0
Page 3 of 4 1234