Android View




Android View



  • Static View Creation in Android.

  • Dynamic View Creation in Android.



Static View 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 :

<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:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Static View"
        android:textSize="18dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="150dp"
        android:background="#8B008B" />

    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_centerInParent="true"
        android:background="#FF0000" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="150dp"
        android:background="#8B008B" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/view1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"  />

</RelativeLayout>



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.TextView;

public class MainActivity extends Activity {

    View line;
    TextView tv;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        line = (View) findViewById(R.id.line);
        tv = (TextView) findViewById(R.id.textView2);
       
        line.setOnClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            tv.setText("Line 2 Clicked !!!");
        }
    });
    }

    @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="8"
        android:targetSdkVersion="19" />

    <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 View 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 :
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    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:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Dynamic View"
        android:textSize="18dp" />

   
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:layout_centerHorizontal="true"   />

</RelativeLayout>
 
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.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
  
    RelativeLayout rl;
    TextView tv;
    View line1, line2, line3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        rl = (RelativeLayout) findViewById(R.id.rl);
        tv = (TextView) findViewById(R.id.textView2);
      
        line1 = new View(MainActivity.this);
        line2 = new View(MainActivity.this);
        line3 = new View(MainActivity.this);
      
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams
                ((int) LayoutParams.MATCH_PARENT, 3);
      
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams
                ((int) LayoutParams.MATCH_PARENT, 8);
      
        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams
                ((int) LayoutParams.MATCH_PARENT, 3);
      
        params1.topMargin = 240;
      
        params2.addRule(RelativeLayout.CENTER_IN_PARENT);
      
        params3.topMargin = 450;
              
        line1.setLayoutParams(params1);
        line1.setBackgroundColor(Color.MAGENTA);
      
        line2.setLayoutParams(params2);
        line2.setBackgroundColor(Color.RED);
      
        line3.setLayoutParams(params3);
        line3.setBackgroundColor(Color.MAGENTA);
      
        rl.addView(line1);
        rl.addView(line2);
        rl.addView(line3);
      
        line2.setOnClickListener(new View.OnClickListener() {
          
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          
            tv.setText("Line 2 Clicked !!!");
        }
    });      
    }

    @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="8"
        android:targetSdkVersion="19" />

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