Android SurfaceView




Android SurfaceView



  • Dynamic SurfaceView Creation in Android.

  • Static SurfaceView Creation in Android.


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
   
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
   
        <TextView
            android:id="@+id/textView1"
            android:textColor="#8B008B"
            android:textSize="19dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dynamic SurfaceView" />
   
    </RelativeLayout>
   
   
    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />  
      
</LinearLayout>


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.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
   
    RelativeLayout rl;
    SurfaceView sv;
    Button b;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        rl = (RelativeLayout) findViewById(R.id.rl);
        sv = new SurfaceView(MainActivity.this);
       
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
       
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.topMargin = 40;
       
        sv.setLayoutParams(params);
        sv.setBackgroundColor(Color.LTGRAY);
       
        tv = new TextView(MainActivity.this);
        b = new Button(MainActivity.this);
       
        tv.setText("TextView");
        b.setText("Button");
       
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams
                ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
       
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams
                ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
       
        params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params2.addRule(RelativeLayout.CENTER_IN_PARENT);
       
        params1.topMargin = 75;
       
        tv.setLayoutParams(params1);
        b.setLayoutParams(params2);
       
        rl.addView(sv);
        rl.addView(tv);
        rl.addView(b);
       
        b.setOnClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            Toast.makeText(getBaseContext(), "Cool Buddy",
                Toast.LENGTH_SHORT).show();
        }
    });
    }

    @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 6 : Our output will be like this :





Static SurfaceView 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 :
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
   
        <TextView
            android:id="@+id/textView1"
            android:textColor="#8B008B"
            android:textSize="19dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Static SurfaceView" />
   
    </RelativeLayout>
   
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
   
        <SurfaceView
            android:id="@+id/surfaceView1"
            android:background="#1E90FF"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView2"
            android:textColor="#FFF"
            android:textSize="18dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/surfaceView1"
            android:layout_marginTop="25dp"
            android:layout_centerHorizontal="true"
            android:text="TextView" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Button" />
       
    </RelativeLayout>   

</LinearLayout>
 
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.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
  
    SurfaceView sv;
    Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        sv = (SurfaceView) findViewById(R.id.surfaceView1);
        b = (Button) findViewById(R.id.button1);           
      
        b.setOnClickListener(new View.OnClickListener() {
          
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          
            Toast.makeText(getBaseContext(), "Cooooool",
                Toast.LENGTH_SHORT).show();
        }
    });
    }

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