Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Check my blogspot in which I have <a href="http://grishma102.blogspot.in/2013/11/simple-drag-drop-views-on-screen-in.html" rel="nofollow">shown the drag drop image <code>onTouch</code> event</a>.</p> <p><code>Edit</code>: here is the full code:</p> <p>First of all create android project in your eclipse and create a class and layout file as below:</p> <p><code>MainActivity.java</code></p> <pre><code>public class MainActivity extends Activity { private ImageView m_ivImage, m_ivImage1; private int m_counter = 0; float m_lastTouchX, m_lastTouchY, m_posX, m_posY, m_prevX, m_prevY, m_imgXB, m_imgYB, m_imgXC, m_imgYC, m_dx, m_dy; private LinearLayout m_llTop; private AbsoluteLayout m_alTop; private Button m_btnAddView, m_btnRemove; private Context m_context; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); m_context = this; m_prevX = 0; m_prevY = 0; m_imgXB = 50; m_imgYB = 100; m_imgXC = 150; m_imgYC = 100; m_ivImage = (ImageView) findViewById(R.id.ivImage); m_ivImage1 = (ImageView) findViewById(R.id.ivImage1); m_llTop = (LinearLayout) findViewById(R.id.llTop); m_alTop = (AbsoluteLayout) findViewById(R.id.alTop); m_btnAddView = (Button) findViewById(R.id.btnAdd); m_btnRemove = (Button) findViewById(R.id.btnRemove); m_ivImage.setOnTouchListener(m_onTouchListener); m_ivImage1.setOnTouchListener(m_onTouchListener); m_btnAddView.setOnClickListener(m_onClickListener); m_btnRemove.setOnClickListener(m_onClickListener); } OnClickListener m_onClickListener = new OnClickListener(){ @Override public void onClick(View p_v) { switch (p_v.getId()) { case R.id.btnAdd: addView(); break; case R.id.btnRemove: removeView(); break; default: break; } } }; OnTouchListener m_onTouchListener = new OnTouchListener(){ @Override public boolean onTouch(View p_v, MotionEvent p_event) { switch (p_event.getAction()) { case MotionEvent.ACTION_DOWN: { m_lastTouchX = p_event.getX(); m_lastTouchY = p_event.getY(); break; } case MotionEvent.ACTION_UP: { break; } case MotionEvent.ACTION_MOVE: { m_dx = p_event.getX() - m_lastTouchX; m_dy = p_event.getY() - m_lastTouchY; m_posX = m_prevX + m_dx; m_posY = m_prevY + m_dy; if (m_posX &gt; 0 &amp;&amp; m_posY &gt; 0 &amp;&amp; (m_posX + p_v.getWidth()) &lt; m_alTop.getWidth() &amp;&amp; (m_posY + p_v.getHeight()) &lt; m_alTop.getHeight()) { p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), (int) m_posX, (int) m_posY)); m_prevX = m_posX; m_prevY = m_posY; } break; } } return true; } }; /** * Add view dynamically for drag and drop */ private void addView() { ImageView m_img = new ImageView(m_context); TextView m_tv=new TextView(m_context); if (m_counter &lt; 5) { if (m_counter % 2 == 0) { m_img.setBackgroundResource(R.drawable.bol_green); m_tv.setText("Hello! Drag Me! "); m_alTop.addView(m_tv, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB))); m_alTop.addView(m_img, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB))); } else { m_img.setBackgroundResource(R.drawable.bol_paars); m_alTop.addView(m_img, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXC), ((int) m_imgYC))); } m_counter++; if (m_counter == 5) m_btnAddView.setEnabled(false); } m_img.setOnTouchListener(m_onTouchListener); m_tv.setOnTouchListener(m_onTouchListener); } public void removeView() { m_counter = 0; m_alTop.removeAllViews(); m_alTop.invalidate(); m_btnAddView.setEnabled(true); } } </code></pre> <p><code>main_layout.xml</code></p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/llTop" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;AbsoluteLayout android:id="@+id/alTop" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_margin="10dp" android:layout_weight=".70" &gt; &lt;ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:src="@drawable/ic_menu_share" android:visibility="gone" /&gt; &lt;ImageView android:id="@+id/ivImage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_x="193dp" android:layout_y="29dp" android:src="@drawable/ic_launcher" /&gt; &lt;/AbsoluteLayout&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight=".30" &gt; &lt;Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Add View" /&gt; &lt;Button android:id="@+id/btnRemove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Remove View" android:visibility="visible" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is the boundary file xml code which shows the border of the layout in which object moves.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item&gt; &lt;shape&gt; &lt;stroke android:width="1dp" android:color="#000000"/&gt; &lt;corners android:topLeftRadius="8dp" android:topRightRadius="8dp" android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp"/&gt; &lt;padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp"/&gt; &lt;gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#FFFFFF" android:type="linear" android:centerColor="#FFFFFF"/&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload