Android adds an image at the top right of a button by program

advertisements

I have a button and I want to pro grammatically add a picture of a green tick to the top right button after it has been selected, how would one do that?


First call the relative layout family and using LayoutPram update the position of the picture

ImageView imageView = (ImageView)findViewById(R.id.theImage);

Button myBtn = (Button)findViewById(R.id.myButton);
myBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
               params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
               params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
               imageView.setLayoutParams(params);
          }
      });

Try this!