Question

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?

Was it helpful?

Solution

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);
 }

OTHER TIPS

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

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top