Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:45:39+00:00 2026-06-14T09:45:39+00:00

I am adding myCustomView in a linear layout multiple times, it is working fine

  • 0

I am adding myCustomView in a linear layout multiple times, it is working fine for first instance of customView, but unable to add it multiple times.

Here is the Custom View class:

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;
      } 
     }

This is the class where i am adding custom view:

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:45:41+00:00Added an answer on June 14, 2026 at 9:45 am

    If you set a background color to the MultiTouchView views, then you will find that the color of the entire screen is same as the color of the 2nd view which was added.

    I specified layout params for the child views and i was able to get the views to draw on screen. the modified code:

    public class AndroidTouch extends Activity {
    
        LinearLayout linear;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_c);
    
            linear = (LinearLayout)findViewById(R.id.main);
    
            Bitmap backgroundCard = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
            int w = backgroundCard.getWidth();
            int h = backgroundCard.getHeight();
            LayoutParams params = new LayoutParams(w, h);
            MultiTouchView mt1 = new MultiTouchView(getApplicationContext(), backgroundCard);
            //mt1.setBackgroundColor(Color.RED);//try uncommenting this wihtout layout params change
            linear.addView(mt1, params);
    
    
            Bitmap backgroundCard2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
            params = new LayoutParams(backgroundCard2.getWidth(), backgroundCard2.getHeight());
            MultiTouchView mt2 = new MultiTouchView(getApplicationContext(), backgroundCard2);
            //mt1.setBackgroundColor(Color.BLUE);<-- Entire screen turns blue
            linear.addView(mt2, params);
        }
    }
    

    Screenshot after code change
    But i believe you will face another problem after this. From the code it is evident that you are trying to draw the views at the touched positions. I would suggest using one single custom view and adding multiple images to the same view. Only in this case you have to determine which view needs to be drawn in a different co-ordinate and which view remains same.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Adding strings localization files in iPhone bundle is very simple and perfectly fine but
Adding Multiple series to the ComboChart with different graph types is straightforward, but I
Adding more than one child to a WPF StatusBar results in poor layout with
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding the row one by one in the button click.. (i tried but its
Adding an image to a UINavigationItem's titleView is pretty trivial, but when I push
Adding player name into list: chatList0.Add(s); Adding player msg into list: chatList.Add(s); Printing the
When adding a new view via the Add View... dialog in VS2010SP1 with IIS
For adding an item I use public void add(Object s) { list.add(s); } For
Adding files to .hgignore will prevent them from being tracked. But we have some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.