0

I have a ViewGroup class which adds a custom View and draws a pie chart on a canvas. In the center of the circle i have a TextView. But whatever i do, i cannot get the text in the TextView centered vertically.

The Root view in this case is a LinearLayout.

Snippet:

public class PieChart extends ViewGroup {

    public PieChart(Context context) {
        super(context);
        mPieView = new PieView(getContext());
        addView(mPieView);
    }

    ...

    private class PieView extends View {
        public PieView(Context context) {
            super(context);
            text = new TextView(getContext());
            text.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));

            text.setHeight(400);
            text.setText(getCenterTextValue());
            text.setTextColor(Color.BLACK);
            text.setGravity(Gravity.CENTER);
            text.setBackgroundColor(Color.GRAY);


            addView(text);   
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            text.layout((int)mBoundsInner.left, (int)mBoundsInner.top, (int)mBoundsInner.right, (int)mBoundsInner.bottom);
        }
    }

    ...
}

This is how it looks like in the emulator. I have set a grey background for demonstration purposes so that you can see the bounds of the TextView Picture of how it looks like in android emulator

Here is the activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ls="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:orientation="vertical" >

    <com.lifescraper.view.PieChart
        android:id="@+id/Pie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="0dip">

    </com.lifescraper.view.PieChart>

</LinearLayout>
amofasser
  • 59
  • 1
  • 7
  • check [this answer](http://stackoverflow.com/questions/17526935/how-to-set-textview-in-center-dynamically) – hrskrs Feb 12 '15 at 14:50
  • I see you're using a lot of layout_gravity. This might be cause of why gravity is not working for your text view, it's been dicussed [here](http://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android) and they refer to [these](https://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/) examples. – Bart de Ruijter Feb 12 '15 at 15:51
  • But you're using RelativeLayout params for the textview. Whish means the TextView must be relative to something and in this case it is not. Try using LinearLayout, and set the text view to center with that. If it's easier for you to do it in XML, write it there and set visibility to 0. And then when you need the text view to pop up, use it in code to se visibility back to visible (with modified text). – David Kasabji Feb 12 '15 at 15:57
  • I tried using LinearLayout as well without any luck. I will try @hrskrs answer though. I don't know if i should be using a ViewGroup. I just need to add a TextView to an existing View programmatically. Do i need a ViewGroup to do that? – amofasser Feb 12 '15 at 22:22

0 Answers0