Hi I got a problem with the RelativeLayout. The idea is to define a title bar in the XML code on top of the screen and then add a textview element BELOW this bar on the left side but when I add this TextView in the java code it is always displayed in the top left corner of the display (means it is set over the title bar). Anyone know what I'm doing wrong?
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/main">
<Button
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</ScrollView>
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.main);
for (int i=0; i<=5; i++) {
TextView tv = new TextView(this);
tv.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
if (i == 0)
params.addRule(RelativeLayout.BELOW, R.id.title);
else
params.addRule(RelativeLayout.BELOW, i-1);
rl.addView(tv, params);
}
I think you have to add an ID to the textview as well, even though you won't have to reference it. Also you can addView and set parameters in one call:
tv.setId(123);
params.addRule(RelativeLayout.BELOW, R.id.title);
rl.addView(tv, params);
Related
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 want that the buttons had identical sizes and table was fully filled the buttons. But the buttons have a different size, what do I wrong in?
My xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="horizontal" >
<LinearLayout
</LinearLayout>
<TableLayout
android:id="#+id/mainTableL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp" >
</TableLayout>
</LinearLayout>
My code:
layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mainTableLayout = (TableLayout) findViewById(R.id.mainTableL);
tableRow = new TableRow[VALUE_ROWS];
btn = new Button[VALUE_ROWS*VALUE_COLUMNS];
for (int indexRow = 0; indexRow < VALUE_ROWS; indexRow++){
tableRow[indexRow] = new TableRow(this);
tableRow[indexRow].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
for (int indexColumn = 0; indexColumn < VALUE_COLUMNS; indexColumn++){
btn[countBtn] = new Button(this);
btn[countBtn].setLayoutParams(layoutParams);
btn[countBtn].setId(countBtn);
btn[countBtn].setBackgroundResource(R.drawable.fon);
tableRow[indexRow].addView(btn[countBtn],layoutParams);
countBtn++;
}
mainTableLayout.addView(tableRow[indexRow], layoutParams);
}
I am sorry for my pure english.
Thank you
I'm not sure if changing this will help you, but this definitely should be fixed. For table rows you should set TableLayout.LayoutParams instead of TableRow.LayoutParams:
tableRow[indexRow].setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
and later when you add a row, don't specify layoutParams:
mainTableLayout.addView(tableRow[indexRow]);
Also note that specifying size for rows doesn't have any effect, their width is set to MATCH_PARENT and the height - to WRAP_CONTENT (see the doc). And similar with buttons, TableRow sets their width to WRAP_CONTENT and the height - to MATCH_PARENT regardless of what you specify in the layout params.
I have Relative layout :
<RelativeLayout
android:id="#+id/show_photo_details_comment_who_liked"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/show_photo_details_comment_who_liked_star"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/photo_rate_on" />
</RelativeLayout>
and want to add few text views into it, and I want to place them right next to ImageView...
ImageView img = (ImageView) findViewById(R.id.show_photo_details_comment_who_liked_star);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.show_photo_details_comment_who_liked);
RelativeLayout.LayoutParams youParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
youParam.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
youParam.addRule(RelativeLayout.RIGHT_OF, img.getId() );
youParam.setMargins(10,0,0,0);
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(youParam);
tv.setText( getString( R.string.who_liked_you ) );
tv.setTextColor( getResources().getColor(R.color.green) );
rl.addView(tv);
youParam.addRule(RelativeLayout.RIGHT_OF, tv.getId());
youParam.setMargins(0,0,0,0);
tv = new TextView(getApplicationContext());
tv.setLayoutParams(youParam);
tv.setText( "," );
tv.setTextColor( Color.BLACK );
rl.addView(tv);
But all theese views will show up in left top corner. Can anybody help me ?
You are reusing the same LayoutParams object with all views, so they end up using the same layout params. See May I reuse LayoutPrams with ViewGroup.addView?.
Also after you fix this, you should assign some id to the first TextView (via setId()) because otherwise the two TextViews have the same id -1, and RelativeLayout may choose the wrong one of them when processing this:
youParam.addRule(RelativeLayout.RIGHT_OF, tv.getId());
UPDATE
Actually do you really need to do all this programmatically? Consider adding all these views to the layout XML with android:visibility="gone" and unhiding them when needed.
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).
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.