Marcel Marino
Marcel Marino

Reputation: 1002

Android - Center Entire TextView, including background

I'm adding a textview programmatically and wish to put a background on it, but I can't seem to get the background to center. (updated)

  FrameLayout mTableView1 = new FrameLayout(this);
  FrameLayout mTableView2 = new FrameLayout(this);
  FrameLayout mTableView3 = new FrameLayout(this);
  int bgTop = getResources().getIdentifier("tableb1" , "drawable", this.getPackageName());
  int bgMid = getResources().getIdentifier("tableb2" , "drawable", this.getPackageName());
  int bgBot = getResources().getIdentifier("tableb3" , "drawable", this.getPackageName());
  setTextStyle(tv1, "Dashboard", Gravity.CENTER, mLayoutView, 40, getResources().getColor(R.color.white), getResources().getColor(R.color.transparent), 0, 5);
  setTextStyle(tv2, "Happening Now:", Gravity.LEFT, mLayoutView, 15, getResources().getColor(R.color.white), getResources().getColor(R.color.transparent), 0, 5);
  setTableStyle(tv3, "Mark Novak Scheduled", Gravity.CENTER, mTableView1, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgTop);
  setTableStyle(tv4, "Attendance up 3%", Gravity.CENTER, mTableView2, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgMid);
  setTableStyle(tv5, "Location of next event", Gravity.CENTER, mTableView3, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgBot);

 mLayoutView.addView(mTableView1);
 mLayoutView.addView(mTableView2);
 mLayoutView.addView(mTableView3);
}

   private void setTextStyle(TextView tv, String s, int g, LinearLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad){
   tv.setText(s);
   tv.setGravity(g);
   tv.setTextColor(textColor);
   tv.setTextSize(fsize);
   tv.setPadding(lpad, tpad, lpad, 0);
   L.addView(tv);
 }

    private void setTableStyle(TextView tv, String s, int g, FrameLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad, int bg){
   tv.setText(s);
   tv.setGravity(g);
   tv.setTextColor(textColor);
   tv.setTextSize(fsize);
   tv.setPadding(lpad, 0, 0, 0);
   tv.setBackgroundResource(bg);
   tv.setWidth(200);

   L.addView(tv);

}

What I get is that the text is off-center 200 pixels, but the whole textview isn't. The background image stretches from one end to the other. Is there a way around this? I've tried setting the width. I had it working for a little bit, but then something went wrong and it stretched out again.

Any suggestions?

Upvotes: 1

Views: 122

Answers (3)

vipul mittal
vipul mittal

Reputation: 17401

If you want to alien your TextView itself. You need to set layout_gravity not gravity. In java you can do it by setting layout params.

For ex:

 private void setTextStyle(TextView tv, String s, int g, LinearLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad){

   tv.setText(s);
   tv.setLayoutParams(new FrameLayout.LayoutParams(300, 25, Gravity.CENTER_HORIZONTAL));
   tv.setGravity(g);
   tv.setTextColor(textColor);
   tv.setTextSize(fsize);
   tv.setPadding(lpad, tpad, lpad, 0);
   L.addView(tv);
 }

Upvotes: 1

sddamico
sddamico

Reputation: 2130

Unfortunately, Android backgrounds always scale to match their View's bounds. If you want a more advanced ScaleType, you'll have to use an ImageView.

 private void setTableStyle(TextView tv, String s, int g, FrameLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad, int bg){
     tv.setText(s);
     tv.setGravity(g);
     tv.setTextColor(textColor);
     tv.setTextSize(fsize);
     tv.setPadding(lpad, 0, 0, 0);
     tv.setWidth(200); 

     ImageView bgView = new ImageView(tv.getContext);
     bgView.setScaleType(ScaleType.CENTER);
     bgView.setImageResource(bg);
     FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     flp.gravity = Gravity.CENTER;
     L.addView(bgView, flp);
     L.addView(tv);
}

Upvotes: 0

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Dont use linear layout to center stuff in it. use FrameLayout or RelativeLayout.

Upvotes: 0

Related Questions