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.
Related
i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:
main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
MainActivity.java:
linearLayout = (LinearLayout) view.findViewById(R.id.result);
.......
public void addItem(){
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
EditText quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
Help me, please.
I've edit my question
Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:
Items Qtt
Cola......1
Chicken...1
Meat......2
Help me.
Do you want to get like this?
String str = item.getText().toString();
String str2 = quantity.getText().toString();
As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.
Try declaring your Views as global variables or doing it outside of the loop such as;
//....
TextView item;
EditText quantity;
for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.
This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.
Anyway. You could add the views to an list as you add them to the linearlayout.
List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();
for(int i = 0; i < cartCount; i++) {
...
childLayout.addView(item);
childLayout.addView(quantity);
items.add(item);
quantities.add(quantity);
linearLayout.addView(childLayout);
}
then loop through your views
for (TextView textView : items) {
}
for (EditText editText : quantities) {
}
item.getText().toString()
quantity.getText().toString()
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 am facing one issue for updating TextView content in RelativeLayout. When I update in the second time, I need to clear the previous text from RelativeLayout.
I have one RelativeLayout in my app activity_calendar.xml
<RelativeLayout
android:id="#+id/rel_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1">
</RelativeLayout>
This is the Main Code :
public void listall () {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
int prevTextViewId = 0;
for (int i = 0; i < titleCalendar.length; i++) {
final TextView textView = new TextView(this);
textView.setText(titleCalendar[i]);
cnvrttime = hourCalendar[i] - 3;
addcnvrtTime = 60*cnvrttime;
curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, addcnvrtTime);
params.setMargins(0, convertDPtoInt((float)addcnvrtTime), 0, 0);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
relativeLayout.addView(textView, params);
}
You need to give the TextView an id so you can find it later:
textView.setId(1234);
Then later:
TextView textView = findViewById(1234);
textView.setText("New Text");
If there aren't any child Views inside the relative layout that you want to keep, call relativeLayout.removeAllViews() before your for loop. Otherwise, use removeView(View) or removeViewAt(int) to remove the ones you don't want.
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.
I am creating some buttons and add them to a linear layout which is defined as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mylayout">
</LinearLayout>
The buttons are created with
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(1, 1, 1, 1);
btn.setText("Button");
btn.setPadding(0, 0, 0, 0);
mylayout.addView(pv, lp);
}
These buttons always have a margin (about 3px) which I'd like to remove. Is there anything I am missing? If I use a custom view which I created there is no space between them.
Should I set
lp.setMargins(-3, -3, -3, -3);
which removes the margin? Is there a drawback with this?
I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:
http://developer.android.com/guide/developing/tools/draw9patch.html
which includes fiction margins. Here you can find more info about 9-Patch.
http://developer.android.com/guide/topics/resources/drawable-resource.html
In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).
Why are you using Layout Params .. simply add the view after its creation...
This will surely remove the problem
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
btn.setText("Button");
mylayout.addView(pv);
}