As you can see on the picture below, I put a GradientDrawable as background of a textview.
The result is weird : the text is croped.
Result
And there is the code :
GradientDrawable border = new GradientDrawable();
border.setColor(0x00FFFFFF);
border.setStroke(1,Color.GRAY);
border.setCornerRadius(size);
EditText surname = new EditText(this);
surname.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
surname.setBackground(border);
surname.setTypeface(Stats.fontRegular);
FrameLayout.LayoutParams lps = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, size_x);
surname.setLayoutParams(lps);
view.addView(surname);
If you know a way to uncrop the text it will be very usefull for me :)
You can change the gravity with this XML code:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/**yourstringname**"
/>
I found something that works :
When you use a EditText, it seems that there are some paddings inside the view. So you can 'disable' them using
EditText.setPadding(int left, int top, int right, int bottom)
Try this:
GradientDrawable border = new GradientDrawable();
border.setColor(0x00FFFFFF);
border.setStroke(1,Color.GRAY);
border.setCornerRadius(size);
border.setSize(450, 150);
Related
I am working on layout on Android project.
Where the layout parameters are coming programmically. My current layout is coming -
Code-
mRedBackground=initBackground(getResources().getColor(R.color.transparent_red_80), this.mWidthScreen, (int)(0.0984375F * this.mWidthScreen), 0, 0);
And initBackground looks like-
public RelativeLayout initBackground(int intColor, int intWidthscreen, int intHeight, int intMerginLeft, int intMerginTop)
{
RelativeLayout localRelativeLayout = new RelativeLayout(this.mContext);
if (intColor != 0)
localRelativeLayout.setBackgroundColor(intColor);
RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(intWidthscreen, intHeight);
localLayoutParams.setMargins(intMerginLeft, intMerginTop, 0, 0);
localRelativeLayout.setLayoutParams(localLayoutParams);
return localRelativeLayout;
}
But I need it in rounded shape. something like-
I can not use other than RelativeLayout due to project restriction. I also can not use Background XML also. All I need to set from code. I am sorry for that. Any Help will be appreciated.
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
view.setBackground(gd);
I want to set Imageview and Textview on same line in LinearLayout but the Imageview is always higher than the Textview.
Here is my code:
String b[] = cacBuocThucHien.split("#");
for (int j = 0; j < b.length; j++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(30,30);
lp.setMargins(50, 0, 0, 0);
ImageView img = new ImageView(RecipesDetailActivity.this);
TextView tv = new TextView(RecipesDetailActivity.this);
img.setLayoutParams(lp2);
img.setImageResource(R.mipmap.testic);
tv.setLayoutParams(lp);
tv.setText(b[j] + "\n");
layoutHuongDan.addView(img);
layoutHuongDan.addView(tv);
}
This You can use to put image in one line.. and it will work in all kind of layouts. Hope this will help you
<LinerLayout>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
....... // Put how many TextViews you want
<TextView
android:id="#+id/textn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinerLayout>
You can set visibility gone initially if you wanna show these textviews only after setting text.
Now in your activity make an array of ids of textviews like this...
public static int[] textRows = {R.id.text1, R.id.text2, ........ R.id.textn};
then use for loop for initializing them and setting text and images like this
TextView[] allTexts = new TextView[n];
for (int i = 0; i < n; i++) {
allTexts[i] = (TextView) findViewById(textRows[i]);
allTexts[i].setText("your text");
allTexts[i].setCompoundDrawables(left_image, null, null, null);
}
It will work. Try it out
set textview drawable left no need of extra imageview
android:drawableLeft="#drawable/img"
If you only want to show an icon beside that TextView, in my opinion you should consider using drawable icon feature of TextView
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
But if you would insist to using two views beside each other, using layout gravity would be useful in such circumstances.
lp.gravity= Gravity.CENTER_VERTICAL;
If you want to display only TextView and ImageView.. go for setting CompounDrawable in TextView itself..
But, for having two views side-by-side in LinearLayout programmatically try setting the orientation=horizontal, check out this link.
In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter).
I need the text and the picture to have the same size. Is there a way to do that?
Here's the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
ImageView img = new ImageView(this);
Tag.setText(data);
if(stato.equalsIgnoreCase("presente")){
img.setImageResource(R.drawable.ic_presente);
}else{
img.setImageResource(R.drawable.ic_assente);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
layoutParams.setMargins(50, 0, 0, 0);
img.setLayoutParams(layoutParams);
LayoutPadre.setLayoutParams(params);
LayoutPadre.addView(Tag);
LayoutPadre.addView(img);
The problem is that the image is not horizontally centered like the img.
Look at the screen
Thanks to all.
keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that
If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"
I'm confused, this should be centering, yet it's not.
public void addTableImage(String imageName, LinearLayout L){
ImageView newImage = new ImageView(c);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
newImage.setLayoutParams(layoutParams);
int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName());
RelativeLayout l1 = new RelativeLayout(c);
l1.setLayoutParams(new RelativeLayout.LayoutParams(400, 160));
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER);
newImage.setImageResource(id);
l1.addView(newImage);
L.addView(l1);
}
And I'm getting this as a result:
Image shows up fine (yes, it's just a placeholder green box), the background is set, and everything is set to be Gravity.CENTER, yet it's still left oriented.
Bonus points: Anyone know why "Ask a Question" now autocompletes your last question's info? It's a bit confusing, making me think I'm actually editing an old question.
Change your code to:
public void addTableImage(String imageName, LinearLayout L){
ImageView newImage = new ImageView(c);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
newImage.setLayoutParams(layoutParams);
int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName());
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(400, 160);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER);
newImage.setImageResource(id);
l1.addView(newImage);
L.addView(l1);
}
Your image is centered inside relativelayout by setting gravity of relative layout to center.
But to set relative layout itself to the center of the Linearlayout it is in you need to set layout gravity to center of relative layout to center or gravity of the LinearLayout to center.
You can try above code it should work.
Here's the thing: I want to add some images programmatically. The images should have to have a topMargin of 5dip except for the first image, in a LinearLayout with a vertical orientation manner. below the code segment:
LinearLayout body = (LinearLayout) findViewById(R.id.body);
for (int i = 1; i <= 4; i++) {
ImageView img = new ImageView(this);
MarginLayoutParams lp = new MarginLayoutParams(-2, -2);
img.setImageResource(R.drawable.image);
if (i != 1) {
lp.setMargins(0, 5, 0, 0);
}
img.setLayoutParams(lp);
body.addView(img);
body.requestLayout();
}
By running the program I can see the the 4 images(here) vertically aligned one by one but there is no topMargin(as in the code,5dip). body is the id of the LinearLayout. here's the XML segment to:
<LinearLayout
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#901b0e08"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="8dp" >
</LinearLayout>
I cant get what went wrong here.
Thanks.
Try changing your MarginLayoutParams to this:
LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
The reason for doing this, is that body is a LinearLayout and thus you would want to use the LinearLayout-specific LayoutParams.
Try to set padding but margin for ImageView. Sometimes it works fine. Your can directly set padding size to ImageView w/o declare LayoutParams.
You probably need to set LayoutParams' gravity property, for example:
lp.gravity = 0; //AXIS_X_SHIFT
That's what works fine for me.