Android AbsoluteLayout



Android AbsoluteLayout


  • Static AbsoluteLayout Creation in Android.

  • Dynamic AbsoluteLayout Creation in Android.



Static AbsoluteLayout Creation in Android.


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<AbsoluteLayout 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:textSize="22dp"
        android:textColor="#4B0082"
        android:layout_x="80dp"
        android:layout_y="55dp"
        android:text="AbsoluteLayout" />
   
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:layout_x="114dp"
        android:layout_y="193dp"
        android:text="Button" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:layout_x="38dp"
        android:layout_y="322dp"
        android:text="ToggleButton 1" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:layout_x="200dp"
        android:layout_y="320dp"
        android:text="ToggleButton 2" />

</AbsoluteLayout>


Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        final ToggleButton tg1 = (ToggleButton) findViewById(R.id.toggleButton1);
        final ToggleButton tg2 = (ToggleButton) findViewById(R.id.toggleButton2);
       
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            if(tg1.isChecked())
                Toast.makeText(getBaseContext(), "Toggle Button 1" +
                    "\n \n \t is Checked", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getBaseContext(), "Toggle Button 1" +
                    "\n \n is not Checked", Toast.LENGTH_SHORT).show();
        }
    });
       
        b.setOnLongClickListener(new View.OnLongClickListener() {
           
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
           
            if(tg2.isChecked())
                Toast.makeText(getBaseContext(), "Toggle Button 2" +
                    "\n \n \t is Checked", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getBaseContext(), "Toggle Button 2" +
                    "\n \n is not Checked", Toast.LENGTH_SHORT).show();
           
            return true;
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
}


Step 4 : Open AndroidManifest.xml and add following code :


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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            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>


Step 5 : Our output will be like this :




Dynamic AbsoluteLayout Creation in Android.


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
   
    AbsoluteLayout abs;
    AbsoluteLayout.LayoutParams param1, param2;
    TextView[] tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        abs = new AbsoluteLayout (MainActivity.this);
       
        param1 = new AbsoluteLayout.LayoutParams(300, 150, 100, 150);
               
        param2 = new AbsoluteLayout.LayoutParams(300, 150, 100, 300);
       
        TextView t1 = new TextView(MainActivity.this);
        t1.setText("Dynamic AbsoluteLayout");
        t1.setTextSize(17);
        t1.setTextColor(Color.MAGENTA);
        t1.setLayoutParams(param1);
       
        Button b = new Button (MainActivity.this);
        b.setText("Android");
        b.setTextSize(17);
        b.setTextColor(Color.BLUE);
        b.setLayoutParams(param2);
       
        abs.addView(t1);
        abs.addView(b);
       
        setContentView(abs);
       
        b.setOnClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            Toast.makeText(getBaseContext(), "Success !!!",
                Toast.LENGTH_SHORT).show();
        }
    });       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 3 : Open AndroidManifest.xml and add following code :

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            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>


Step 4 : Our output will be like this :