Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to add custom view in linear layout - Android
    primarykey
    data
    text
    <p>I am adding <code>myCustomView</code> in a linear layout multiple times, it is working fine for first instance of customView, but unable to add it multiple times.</p> <p>Here is the Custom View class:</p> <pre><code>public class MultiTouchView extends View { private float x, y; Bitmap image; public MultiTouchView(Context context, Bitmap image) { super(context); this.image = image; // TODO Auto-generated constructor stub } public MultiTouchView(Context context, AttributeSet attrs) { super(context, attrs); } public MultiTouchView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawBitmap(image, x, y, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch(action){ case MotionEvent.ACTION_MOVE: x = event.getX(); y = event.getY(); break; case MotionEvent.ACTION_DOWN: x = event.getX(); y = event.getY(); break; case MotionEvent.ACTION_UP: break; } invalidate(); return true; } } </code></pre> <p>This is the class where i am adding custom view:</p> <pre><code>public class AndroidTouch extends Activity { LinearLayout linear; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); linear = (LinearLayout)findViewById(R.id.linear); Bitmap backgroundCard = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); MultiTouchView mt1 = new MultiTouchView(this, backgroundCard); linear.addView(mt1); Bitmap backgroundCard2 = BitmapFactory.decodeResource( getResources(), R.drawable.icon); MultiTouchView mt2 = new MultiTouchView(this, backgroundCard2); linear.addView(mt2); } } </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.
 

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