Android

Android Example Overlap SurfaceView

Using Android API Level : 7 
In this example we shall be going through a simple scenario:
There are two surface views of different color background added onto the main 
Activity screen (having RelativeLayout).
One surfaceview is allowed to be dragged using touch on screen,
and there will be a check to detect when touch up event is fired, when the dragged view
comes/placed on top of the other surface view. In this case a message will be poped up
on screen, as shown in the following images.

 
ExampleOverlapSurfaceView/.classpath

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="src" path="gen"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> <classpathentry kind="output" path="bin/classes"/> </classpath>


ExampleOverlapSurfaceView/.project

<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>TestOverlapViewPOC</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.android.ide.eclipse.adt.ApkBuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>com.android.ide.eclipse.adt.AndroidNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>


ExampleOverlapSurfaceView/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.techiecommunity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ExampleOverlapView" android:label="@string/title_activity_example_overlap_view" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


ExampleOverlapSurfaceView/res/layout/activity_example_overlap_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/relativeLayout"> <SurfaceView android:id="@+id/surView" android:layout_width="100dp" android:layout_height="100dp" android:background="#FF0000" android:layout_marginTop="50dp" android:layout_marginLeft="50dp"/> <SurfaceView android:id="@+id/surView1" android:layout_width="100dp" android:layout_height="100dp" android:background="#0000FF" android:layout_marginTop="250dp" android:layout_marginLeft="150dp"/> </RelativeLayout>


ExampleOverlapSurfaceView/res/menu/activity_example_overlap_view.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_settings" android:title="@string/menu_settings" android:orderInCategory="100" /> </menu>


ExampleOverlapSurfaceView/res/values/strings.xml

<resources> <string name="app_name">TestOverlapViewPOC</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_example_overlap_view">ExampleOverlapView</string> </resources>


ExampleOverlapSurfaceView/res/values/styles.xml

<resources> <style name="AppTheme" parent="android:Theme.Light" /> </resources>


ExampleOverlapSurfaceView/src/net/techiecommunity/ExampleOverlapView.java

package net.techiecommunity; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.Toast; public class ExampleOverlapView extends Activity { private ViewGroup viewGrp; private int x; private int y; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example_overlap_view); final SurfaceView surView = (SurfaceView) findViewById(R.id.surView); final SurfaceView surView1 = (SurfaceView) findViewById(R.id.surView1); viewGrp = (ViewGroup) findViewById(R.id.relativeLayout); surView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { x = (int)motionEvent.getRawX(); y = (int)motionEvent.getRawY(); switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { break; } case MotionEvent.ACTION_UP: { if(intersect(view, surView1)) { Toast.makeText(getApplicationContext(), "Overlapped", Toast.LENGTH_SHORT).show(); } break; } case MotionEvent.ACTION_MOVE: { RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); layoutParams.leftMargin = x-view.getWidth()/2; layoutParams.topMargin = y-view.getHeight(); view.setLayoutParams(layoutParams); view.bringToFront(); viewGrp.invalidate(); break; } } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_example_overlap_view, menu); return true; } public boolean intersect(View v1, View v2) { int v2left = v2.getLeft(); int v2right = v2.getRight(); int v2top = v2.getTop(); int v2bottom = v2.getBottom(); int v1left = v1.getLeft(); int v1right = v1.getRight(); int v1top = v1.getTop(); int v1bottom = v1.getBottom(); return !(v2left > v1right || v2right < v1left || v2top > v1bottom || v2bottom < v1top); } }


This example is tested using Android API Level 7, If anyone find any technical issues/difficulties using this example code on any other version, be it device or Android Platform, please share your comments using following form.
If interested in commenting related to Android Example Overlap SurfaceView
Click Here

Name (To be shown on this Page:*

Email (Not to be shown):*

Website (To be shown on this Page)

Your Comments here (To be shown on this Page):*

(You can use <a> Tag in your comments)

Please enter same number as shown here 382




Android Thread Example

REST Interview Questions

JAX-WS Webservice Example

Android Overlap SurfaceView Example

Spring3 Integration of File Endpoint

JAXWS Webservice Example

Android WebView Example

Android Context Menu Example

Setting up Eclipse for Android App Development

Android SensorEvent Orientation Example

Android ImageSwitcher Example

REST API Design for Android App

Android ExpandableListView Example

Singleton Serializable on Android

Android GPS with Google MAP Example

Android Project With Example

Android Project Manageing Files

Project Ideas for App on Android Platform

Android EditText with Background

Android SQLite Database Example

Android AutoCompleteTextView Example

Android Tester

Android Interview Questions

Android ListView Example

Android Intent Notification Example

Android Intent Broadcast Example

Android TextView Link Example

Android Sensor List

Android Sensor Orientation Example

Android Intent Broadcast Receiver

Android Gallery SurfaceViews Example

Android Location Example

Android Text to Speech Example

Android Intent Example

Android Layout Example

Android Download File Example

Android Expandable ListView Example

Android Tab Example

Android RelativeLayout Example

Android Intent Example

Android Text to Speech Example

Android SQLite Example

Android CustomView Example

Android Canvas Example

Android SharedPreferences Example

Android Shape Background Example

Android ShapeButton Example

Android ViewFlipper Example

Android Gallery Example

Android TimePickerDialog Example





If interested commenting on this Page :

comments powered by Disqus
Any source code found on any of the page(s) in Techiecommunity.net is associated with following notice: 
   /*
     * TECHIECOMMUNITY.NET MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
     * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TECHIECOMMUNITY.NET SHALL NOT BE LIABLE FOR
     * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
     * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
     *
     * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
     * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
     * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
     * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
     * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
     * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
     * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). TECHIECOMMUNITY.NET
     * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
     * HIGH RISK ACTIVITIES.
     */
   
The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
techiecommunity.net is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

For any of the content, if you would like to bring it to notice for removal from this web site, 
please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
we would take appropriate action and remove disputed content from this web site within 24 hours 
starting from the time it has brought to techiecommunity.net Administrator notice.

This website uses cookies and third party cookies to improve the user experience. 
And to provide more relevant ads. 
In case of continued use of this website implies that you accept the use of cookies on this website.
		
While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.