I’m trying to make a function, which would return a RelativeLayout, with an ImageView and a TextView in the center.
public RelativeLayout makeKey(String letter, int alfa)
{
final RelativeLayout RelBtn = new RelativeLayout(this);
RelBtn.setGravity(Gravity.CENTER);
RelBtn.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ));
final ImageView ivBg = new ImageView(this);
ivBg.setImageBitmap(bmBtnBg);
ivBg.setPadding(5, 5, 5, 5);
RelBtn.addView(ivBg);
TextView tvLetterSHD = new TextView(this);
tvLetterSHD.setTextSize(22);
tvLetterSHD.setTypeface(null, Typeface.BOLD);
tvLetterSHD.setTextColor(0xFF000000);
tvLetterSHD.setText(letter);
tvLetterSHD.setPadding(0, 3, 0, 0);
RelBtn.addView(tvLetterSHD);
TextView tvLetter = new TextView(this);
tvLetter.setTextSize(22);
tvLetter.setTypeface(null, Typeface.BOLD);
tvLetter.setTextColor(0xFFFFFFFF);
tvLetter.setText(letter);
RelBtn.addView(tvLetter);
return RelBtn;
}
Yet I’m getting something like this, as you can see, the TextView is off center.
Any ideas what might be wrong? Thanks! 🙂

U can set LayoutParams
android:layout_centerInParentfor child particles: