I have some problem with add linearlayout dynamically. It's add on the top of screen, overlay other linearlayout.
Here XML,code and results.
XML:
<TextView
android:id="#+id/top_km"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#888"
android:gravity="top"
android:textColor="#fff"
android:textSize="30dip"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:id="#id/textLayout">
</RelativeLayout>
Code:
myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);
Results:
enter link description here
Thanks
Don't use a RelativeLayout as your holder. Use a LinearLayout with orientation="vertical" instead.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:orientation="vertical"
android:id="#id/textLayout" />
then in code
myLayout = (LinearLayout) page.findViewById(R.id.textLayout);
followed by
// rest of your code
It's because you use RealativeLayout for proper adding use
1. RelativeLayout.LayoutParams for st LayoutParams
2. In LayoutParams use field below
Example:
RelativeLayout rl=new RelativeLayout(this);
LinearLayout ll1=new LinearLayout(this);
TextView tx1=new TextView(this);
tx1.setText("Test1");
ll1.addView(tx1);
rl.addView(ll1);
LinearLayout ll2=new LinearLayout(this);
TextView tx2=new TextView(this);
tx2.setText("Test1");
ll2.addView(tx1);
rl.addView(ll2);
RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();
And then use lp2.addRule
Here some help:
Parameters
verb One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.
anchor The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.
Maybe it's easier for you to add it in the XML file with android:visibility="GONE" and then in the code just show it (View.VISIBLE) or hide it (View.GONE).
Related
I am trying to make buttons wrap in a LinearLayout in Android, but they are just continuing off to the right of the view (the word shown in the screenshot should be "HELLO", so I want the "O" to drop down to the next line).
I am adding the buttons programmatically, but even if I code them into the XML layout file, they still don't wrap. Here is the layout file with the LinearLayout container into which I am dynamically adding the buttons:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constrainedWidth="true"
tools:context=".LetterTileView">
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And here is the code I am using to create and add the tile buttons:
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
Any advice would be appreciated. Thanks!
I ended up using FlexboxLayout, which works great for this. Thanks to those who offered suggestions!
First of all, there is no need to use a ConstraintLayout you can use your LinearLayout as the parent layout.
Then for the purpose of displaying all buttons in one line, you have to set weight for the LinearLayout in XML and set weight for the views you add to it.
The xml file should look like:
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="5"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And in code you should set weight for each view you by adding ,1.0f to LayoutParam :
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1.0f );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
I want to generate programmatically Horizontal scrolling LinearLayout with Imageview and textview at center bottom.
this is my Java code.:
LinearLayout rec=(LinearLayout)findViewById(R.id.hori_recom);
//ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(450,450);
ViewGroup.LayoutParams params1=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// for(int g=0;g<5;g++)
for(int g=0;g<imgpath_orignal.size();g++)
{
ImageView recimg=new ImageView(Details.this);
recimg.setId(g+1);
recimg.setPadding(25,0,0,0);
Picasso.with(Details.this).load(al_gallary_img.get(g)).placeholder(R.drawable.logo)
.error(R.drawable.logo).into(recimg);
recimg.setLayoutParams(params);
TextView txtlabel=new TextView(Details.this);
txtlabel.setId(g+1);
txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);
txtlabel.setPadding(15,15,15,15);
txtlabel.setText(""+al_img_caption.get(g));
txtlabel.setLayoutParams(params1);
rec.addView(txtlabel);
rec.addView(recimg);
This is my Xml:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/hori_recom"
android:layout_marginTop="10dp"/>
</HorizontalScrollView>
Problem is I am not getting my textview at bottom-center of my Imageview.
Do one thing - combine your ImageView and TextView in one layout
Say this layout as row_layout.
<LinearLayout
....
....
<ImageView
...
... />
<TextView
...
... />
</LinearLayout>
Now add Layout into the LinearLayout which lies into HorizontalScrollView -
for(int g=0; g<imgpath_orignal.size(); g++) {
View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
// Do your layout binding stuff over here..
ImageView ivPhoto = (ImageView) v.findViewById(R.id.imageview_id); // Give reference
Picasso.with(context).load(url).into(ivPhoto); // Loading image using Picasso
rec.addView(v);
}
This is because you have set gravity of TextView as center_bottom instead of layout_gravity.
Just remove
txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);
and instead of
ViewGroup.LayoutParams params1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);`
do this
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.CENTER|Gravity.BOTTOM;
This is the way of setting layout_gravity programatically.
I have this layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/rect"
android:id="#+id/searchRelativeLayout">
<LinearLayout
android:id="#+id/linearLayoutProc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_alignParentRight="false"
android:gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="2dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/inputQuery"
android:inputType="text"
android:ellipsize="middle"
android:hint="#string/search_hint"
style="#style/AutoCompleteTextViewOrangeAutoComplete"/>
</LinearLayout>
</RelativeLayout>
I'm trying to add, without success, a TextView dynamically on top off that LinearLayout, this is, in the end, the LinearLayout should be below the added TextView
This is my code:
RelativeLayout compareLayout = (RelativeLayout) layoutToShow.findViewById(R.id.searchRelativeLayout);
TextView compareItemOneMessage = new TextView(mContext);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
compareItemOneMessage.setLayoutParams(lp);
compareItemOneMessage.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
compareItemOneMessage.setGravity(Gravity.CENTER);
compareItemOneMessage.setTypeface(fontBold);
compareItemOneMessage.setTextColor(mContext.getResources().getColor(R.color.orange));
compareItemOneMessage.setText("test");
compareLayout.addView(compareItemOneMessage);
The new TextView is added but it's overlapped with the content of the EditText
Any ideas?
You need to add a rule to keep it at top
compareItemOneMessage.addRule(RelativeLayout.ALIGN_PARENT_TOP);
and you also need to add rule for linearlayout to put it down.
LinearLayout l =(LinearLayout) findViewById(R.id.linearLayoutProc);
RelativeLayout.LayoutParams params = l.getLayoutParams();
l.addRule(RelativeLayout.BELOW, your_text_view_id);
I would do somthing like that:
// Create the textView here
compareLayout.addView(compareItemOneMessage);
List<View> views = new ArrayList<View>();
views.add(compareItemOneMessage);
for(int i=0; i<compareLayout.getChildCount(); ++i)
{
views.add( compareLayout.getChildAt(i) );
compareLayout.removeView( compareLayout.getChildAt(i) );
}
for (int i=0; i<views.size(); i++) compareLayout.addView(views.get(i));
This should do the job. I haven't tested it as I'm not on a dev desktop.
ViewGroup parent = (ViewGroup) view.getParent();
LayoutParams layoutParams = view.getLayoutParams();
parent.addViewInLayout(compareItemOneMessage, 0, layoutParams);
i can't imagine how it looks and what kind of overlapping you get (maybe you should provide an image).
but why are you using RelativeLayout instead of LinearLayout as root? LinearLayout works better in the most cases.
you could try padding or margin to fix overlapping.
it also might be because of the orientation of the Layout
the problem with IllegalStateException can be fixed this way (but pls only reuse stuff this way if you know the lifetime of your view what was the last parent):
ViewGroup vg = (ViewGroup) view.getParent();
vg.removeView(view);
//then do the stuff that adds the view - whatever failed before
linearLayout.add(view);
I want to implement an imageview and a textview side by side. I achieved this by using XML. However i wanted to achieve this programmatically but had no luck so far. My XML and Java code are below. Please help me to execute programmatically.
I'm executing the Java code in a fragment.
XML CODE:
<RelativeLayout
android:id="#+id/rl1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<com.loopj.android.image.SmartImageView
android:id="#+id/my_image1"
android:layout_width="160dip"
android:layout_height="100dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/my_image1"
android:layout_alignTop="#+id/my_image1"
android:layout_toRightOf="#+id/my_image1"
android:gravity="center_vertical"
/>
</RelativeLayout>
JAVA CODE:
RelativeLayout rl1 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlParams;
rlParams = new RelativeLayout.LayoutParams(
newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rlParams.addRule(LinearLayout.VERTICAL);
rl1.setLayoutParams(rlParams);
SmartImageView siv1 = new SmartImageView(getActivity());
siv1.setId(rand.nextInt(50000) + 1);
siv1.setLayoutParams(new LayoutParams(width,height));
siv1.setAdjustViewBounds(true);
siv1.setScaleType(ScaleType.FIT_XY);
siv1.setImageUrl(Uri);
TextView tv1 = new TextView(getActivity());
RelativeLayout.LayoutParams relativeLayoutParams;
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(relativeLayoutParams);
relativeLayoutParams.setMargins(10, 0, 0, 0);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText("Sample Text");
rl1.addView(siv1);
rl1.addView(tv1);
ll.addView(rl1);
By executing the above code, i'm getting the image but the text is inside the image. But, i want to get the image on the left and the text on the right.
Thanks in advance
Add below code to your Activity:
rlParams.addRule(RelativeLayout.ALIGN_TOP, siv1);
rlParams.addRule(RelativeLayout.ALIGN_BOTTOM, siv1);
rlParams.addRule(RelativeLayout.RIGHT_OF, siv1);
tv1.setLayoutParams(rlParams);
and then do:
rl1.addView(siv1);
rl1.addView(tv1);
Hope this helps.
In your case it is better to use a horizontal LinearLayout:
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(param);
TextView textview = new TextView(getActivity());
...
SmartImageView siv1 = new SmartImageView(getActivity());
...
ll.addView(textview);
ll.addView(siv1);
To align the views in RelativeLayout is much difficult. I will suggest you to use the TableRows or LinearLayouts. Both these give much easier way to align views side by side. Here is a source in which one TextView is aligned-left with 4 ImageViews aligned on its right. You can get idea from this
TextView taking/consuming too much space above and below the text
You can set the margin of your text like this
relativeLayoutParams.setMargins(200, 0, 0, 0);
but this is not the best practice so do one thing take linear layout set orientation to horizontal add both imageview and textview to it and then add this linearlayout to your relative layout.
Hope this helps.
I tried to add some GUI elements like an ImageView or a TextView to a LinearLayout programmatically. But the elements aren't displayed.
To see if a element is drawn or not, I set a different background color for each element. The result was that I can only see the background color of the LinearLayout. But why?
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
setLayoutParams(params);
setBackgroundColor(Color.RED);
imageView = new ImageView(context);
params = new LinearLayout.LayoutParams(100, 100);
imageView.setLayoutParams(params);
imageView.setBackgroundColor(Color.BLUE);
addView(imageView);
}
}
The strange thing is that I can see the red background color of the LinearLayout but in the size of the ImageView. If I add some other GUI elements like a TextView, I can see how the LinearLayout grows. But I can not see the TextView.
I'm really confused, because this not the first time I do something like this. Can u tell me what I'm doing wrong?
This is a snippet of the layout.xml file:
<LinearLayout android:layout_width="match_parent"
android:layout_height="45dp"
android:id="#+id/bottom_bar"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<FrameLayout android:id="#+id/block_edit_delete_layout"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/block_edit_delete_selector">
<ImageView android:layout_height="match_parent"
android:layout_width="wrap_content"
android:src="#drawable/block_edit_delete"
android:scaleType="fitXY"
android:contentDescription="#string/delete"/>
</FrameLayout>
<LinearLayout
android:id="#+id/block_edit_progress"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"/>
<FrameLayout android:id="#+id/block_edit_random_layout"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/block_edit_delete_selector">
<ImageView android:layout_height="match_parent"
android:layout_width="wrap_content"
android:src="#drawable/block_edit_random"
android:scaleType="fitXY"
android:contentDescription="#string/random_numbers"/>
</FrameLayout>
</LinearLayout>
The LinearLayout with the ID block_edit_progress is the container layout of multiple instances of the class MyLinearLayout. The instances are added in the code:
for(int i = 0; i < numberOfMyLinearLayouts; i++) {
MyLinearLayout v = new MyLinearLayout(getContext());
addView(v);
}
I hope this helps.
If i convert your code to xml, it would be something like:
<LinearLayout layout_width=wrap_content, layout_height = wrap_content>
<LinearLayout id= MyLinearLayout>//just an idea, syntax may be wrong
<LinearLayout layout_width= 100, layout_width=100>
<ImageView color=BLUE>
</ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Whenever you call setLayoutParams on a View, parameter params you give should be parent element.
Try something like if you want linearlayout to be the parent of your linearlayout, use MATCH_PARENT for width, height if you want your view to span the width, height of view's parent
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(lp);//lp is parent view
Also try this, just in case views are getting added to right of your views, and you are not able to see them on screen
yourview.setOrientation(LinearLayout.VERTICAL);
Change the width and height of linear layout to match_parent and see how it changes. wrap_content will only show the content of the linear layout, which seems to be your problem.
I solved the problem. (Or found a workaround)
I moved the complete initialization stuff out of the constructor of the MyLinearLayout. If I then adding a View after the layout has been completely generated, everything works.
Like this:
MyLinearLayout ll = new MyLinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
ll.setLayoutParams(params);
ll.setBackgroundColor(Color.RED);
ImageView v = new ImageView(getContext());
params = new LinearLayout.LayoutParams(50, 50);
v.setLayoutParams(params);
v.setBackgroundColor(Color.GREEN);
ll.addView(v);
addView(ll);
I don't know why the other way doesn't work. Thanks for the fast answers!