1

I created class customTextView extent TextView and I set textAlignment to center. In onMeasure I changed the textview width.

After that textview align change to left and I used this.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); but it didn't change to center

Isabel Inc
  • 1,871
  • 2
  • 21
  • 28
reza malekmohamadi
  • 507
  • 1
  • 8
  • 14

2 Answers2

0

add below code in your class.

Runnable runnable = new Runnable() {
            public void run() {
              view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            }
   };

Handler handler = new Handler();
handler.postDelayed(runnable,1000);
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
0

like this?

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        ImageView imageView = (ImageView)((Activity) context).findViewById(R.id.list_r);
        int imageViewHeight = imageView.getMeasuredWidth();

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

        int final_width = parentWidth - 2*imageViewHeight;
        this.setMeasuredDimension(final_width, parentHeight);

        Runnable runnable = new Runnable() {
            public void run() {
                view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            }
        };

        Handler handler = new Handler();
        handler.postDelayed(runnable,1000);
    }
reza malekmohamadi
  • 507
  • 1
  • 8
  • 14