Thursday, July 3, 2014

Android Table Layout | TableLayout Example in Android | Demo code for Android Table Layout

Android Table layout:   Basically android have 5 types of layouts, we will discuss them one by one with examples-




Here I am Introducing to Table Layout:
Table Layout means display the view in form of row and column. This type of layout helps us to display tabular form of data. Like result, records etc.

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

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@android:color/darker_gray"
                android:gravity="center"
                android:padding="6dp"
                android:text="Roll No." />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@android:color/darker_gray"
                android:gravity="center"
                android:padding="6dp"
                android:text="Name" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@android:color/darker_gray"
                android:gravity="center"
                android:padding="6dp"
                android:text="Marks" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="101"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Manish"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="888"
                android:textSize="20sp" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="102"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="James"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="878"
                android:textSize="20sp" />
        </TableRow>
    </TableLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="@string/app_name"
        android:textSize="20sp" />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:text="androidhub4you.com"
        android:textSize="20sp" />

</LinearLayout>


And use this MainActivity class to display your views-

package com.androidhub4you.tablelayout;

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

public class MainActivity extends Activity {

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

      

}


Your comments and feedback are awaited!
Thank You!




Saturday, June 21, 2014

Android Absolute Layout | Absolute Layout Example in Android | Create Layout using X,Y Coordinate in Android

Android Absolute layout:   Basically android have 5 types of layouts, we will discuss them one by one with examples-



Here I am Introducing to Absolute Layout:
Absolute Layout means that specify exact locations (x, y) coordinates of its children. This layout are not more in use because it is less flexible and harder to maintain than other types of layouts.

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="10px"
        android:text="@string/app_name"
        android:textSize="22sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="110px"
        android:src="@drawable/desert" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="250px"
        android:layout_y="150px"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="20px"
        android:layout_y="350px"
        android:text="androidhub4you.com"
        android:textSize="30sp" />

</AbsoluteLayout>

And use this MainActivity class to display your views-

package com.androidhub4you.absolutelayout;

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

public class MainActivity extends Activity {

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

}


Your comments and feedback are awaited!
Thank You!




Tuesday, June 10, 2014

Android FrameLayout demo | Framelayout Example in Android | Create Frame Layout in android

Android Frame layout:   Basically android have 5 types of layouts, we will discuss them one by one with examples-



Frame Layout means each view comes in form of frame like a view upon another view. This layout helps us if we want to display any text or image on any background image.

<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_marginTop="15dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:scaleType="fitXY"
            android:layout_gravity="center"
            android:src="@drawable/desert" />
       
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="#CCCC00"
            android:textSize="35sp"
            android:text="@string/app_name" />
    </FrameLayout>

</RelativeLayout>

And use this MainActivity class to display your views-

package com.androidhub4you.framelayout;

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

public class MainActivity extends Activity {

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


}



Your comments and feedback are awaited!
Thank You!




Monday, May 19, 2014

Android Transparent Background | Set Opacity in Android | Make android application with transparent background

Hi Friends,

I am again here with very small and important demo in android. How to make your activity transparent and how to set opacity in background.  I am sharing code here step by step-


1)      In your manifest make that activity theme to Translucent-
          <activity
            android:name="com.androidhub4you.transparent.background.MainActivity"
            android:theme="@android:style/Theme.Translucent"
            android:label="@string/app_name" >

2)      Now your page will be 100% transparent, so if you need some background color then set opacity like-

<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"
    android:background="#80000000"
    tools:context=".MainActivity" >

Concentrate on this line- android:background="#80000000"

Here 80 stand for alpha color from 0-255 and 000000 for black color. So just add 2 first digit of alpha with your color code. Below is the normal chart for opacity percentage-

100% — FF
90% — E6
80% — CC
70% — B3
60% — 99
50% — 80
40% — 66
30% — 4D
20% — 33
10% — 1A
0% — 00

And below is the whole code-
   
1)    MainActivity.java

package com.androidhub4you.transparent.background;

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

public class MainActivity extends Activity {

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

      
}


    2)    Activity_main.xml

<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"
    android:background="#8C000000"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="www.androidhub4you.com\n(From: Manish Srivastava)"
        android:textColor="#FFFFFF"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:src="@drawable/logo" />

</RelativeLayout>


     3)    AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidhub4you.transparent.background"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidhub4you.transparent.background.MainActivity"
            android:theme="@android:style/Theme.Translucent"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Your comments and feedback are awaited!

Thank You!