Ankit
Ankit

Reputation: 4644

Giving right margin in button on being clicked in android

I have a screen where I display five buttons inside a Linearlayout vertically. this linear layout is inside a relative layout and aligned to parent right. Now I need that when any button is clicked it should shift leftwards while others remain at the original position. I am trying to give left margin to the button on being clicked but the problem is all the buttons are shifting and not only that one.

Here is the code

 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT,     LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.rightMargin = 20;

    mColorButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            fillDataList(1);
            mIsClicked = true;
            //[start] setting the colour of button
                    mColorButton.setBackgroundResource(R.drawable.yellow_stick) ;
                mColorButton.setLayoutParams(layoutParams);
                    mPatternButton.setBackgroundResource(R.drawable.brown_stick);
                    mDistributionButton.setBackgroundResource(R.drawable.brown_stick);
                    mShapeButton.setBackgroundResource(R.drawable.brown_stick);
                    mLesionButton.setBackgroundResource(R.drawable.brown_stick);
            //[end] setting the colour of button
        }           
    });

Upvotes: 0

Views: 371

Answers (1)

Adil Soomro
Adil Soomro

Reputation: 37729

May be this is the problem:

By default all Buttons are left aligned and when you give right margin to a Button it expands its parent layout size. The parent expands it self with its child. So all the Button shifted left side.

try to give android:layout_gravity="right" to all Button in XML or code.

Upvotes: 1

Related Questions