Reputation: 193
I created 4 textviews programmatically and i added it in my layout, i have one problem gravity center property not working.i mean when i increase my textview's text size gravity Center Repealed.default text size is 14dp Please check my code snippet.
private class DigitView extends TextView {
public DigitView(Context context) {
this(context, null);
}
public DigitView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DigitView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// If selected draw the accent
if (isSelected()) {
this.setBackgroundResource(R.drawable.pincode_background_border_select);
}
else
{
this.setBackgroundResource(R.drawable.pincode_background_border_unselect);
}
}
}
for (int i = 0; i < mDigits; i++) {
DigitView digitView = new DigitView(getContext());
digitView.setWidth(valueInPixels);
digitView.setHeight(valueInPixels);
digitView.setTextColor(Color.WHITE);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.leftMargin = 10; // margin goes here. Use marginStart instead to support right-to-left layouts
}
childView.addView(digitView, lp);
}
and this is a my layout xml code
<LinearLayout
android:id="@+id/my_Container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:id="@+id/my_Container_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
my_Container_child is my layout
what am i doing wrong? if anyone knows solution please help me thanks
Upvotes: 0
Views: 1798
Reputation: 633
Dont give fix height and width to textview
i think error is in this code
DigitView digitView = new DigitView(getContext());
digitView.setWidth(valueInPixels);
digitView.setHeight(valueInPixels);
Upvotes: 0
Reputation: 983
Try this.It will work...
<LinearLayout
android:id="@+id/my_Container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
>
<LinearLayout
android:id="@+id/my_Container_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
Upvotes: 1