Android: Programatically rotating the text inside a button

109 views Asked by At

{Visual Aid} I am making an application with a drag and drop menu. User can fill out three edit texts (width, height, and rotation) then press on "add new" button and a new button with the specified values is created at the origin. However if the rotation attribute is anything other than 0 say 45 degrees, then it rotates the whole button along with the text. I would like for the text to remain horizontal with no rotation. I have looked up but the post all refer back to the rotation attribute which i am already using.

onViewCreated()

tvAddTable = view.findViewById(R.id.btn_add_table);
    tvWidth = view.findViewById(R.id.et_width);
    tvHeight = view.findViewById(R.id.et_height);
    tvRotation = view.findViewById(R.id.et_rotation);
    mSize = new Point();
    mDisplay = Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay();
    mDisplay.getSize(mSize);

    final View.OnTouchListener touchListener = this;
    tvAddTable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {[enter image description here][1]
            ConstraintLayout layout = view.findViewById(R.id.floor_layout);

            //set the properties for button
            Button btnTag = new Button(getContext());
            btnTag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            if(ids.isEmpty()){
                ids.add(0);
            }else{
                ids.add(ids.size());
            }
            btnTag.setId(ids.size());
            btnTag.setText(Integer.toString(btnTag.getId()));

            //add button to the layout
            layout.addView(btnTag);
            btnTag.setLayoutParams(new ConstraintLayout.LayoutParams(Integer.parseInt(tvWidth.getText().toString()), Integer.parseInt(tvHeight.getText().toString())));
            btnTag.setRotation(Integer.parseInt(tvRotation.getText().toString()));
            btnTag.setOnTouchListener(touchListener);
        }
    });
1

There are 1 answers

0
Gabe Sechan On

The rotation attribute rotates the entire view, not just the text or just the background. That's by design. There is no text or background rotation attribute. If you need that, make a custom view.

If you only want the background to rotate, you can probably do it by setting the background drawable to a RotateDrawable that wraps the background you want.