Why this isn't working?
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
lp.setMargins(0, 0, 0,0);
//lp.addRule(RelativeLayout.ALIGN_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_START); fails
lp.addRule(RelativeLayout.ALIGN_PARENT_START);
myImg.setLayoutParams(lp);
mat.addView(myImg);
}
the xml
<RelativeLayout
android:id="#+id/PLAY_Mat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
>
</RelativeLayout>
The ImageView is successfully being added, but it's centered vertically. I want it to align to top.
I expect this to be the way even without the added rule, as "By default, all child views are drawn at the top-left of the layout" (RelativeLayout documentation).
Try this code:
RelativeLayout rl = new RelativeLayout(this);
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.setMargins(0, 0, 0,0);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl.addView(myImg, lay);
//myImg.setLayoutParams(lp);
//mat.addView(myImg);
}
set MATCH_PARENT to RelativeLayout
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.MATCH_PARENT));
or
You can get using getLayoutParams() of RelativeLayout from XML
RelativeLayout.LayoutParams lp = parentRelativeImageview.getLayoutParams();
Solution by OP.
This code works:
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.a1);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(CardWidth, CardHeight);
lay.setMargins(playingCard.UI_MarginLeft, playingCard.UI_MarginTop, 0, 0);
mat.addView(myImg, lay);
}
The key here is CardWidth and CardHeight are both set, and both correct. By correct, I mean correct ratios. Want to double the width? Then double the height etc. If one of w or h is a pixel int, and the other WRAP_CONTENT, then weirdness happens (in the form of the image having either a top margin or a left margin).
Once both w and h are set correctly, lp.addRule(RelativeLayout.ALIGN_PARENT_START) isn't needed.
Related
I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);
I'm trying to add pragmatically a margin top to my Relative Layout in my activity.
Using the xml i can do it in this mode: android:layout_marginTop="10dp", but when i'm trying to do it pragmatically nothing change...
This is the code that i'm using:
//SCROLL VIEW
ScrollView scrollView = new ScrollView(this);
scrollView.setBackground(getResources().getDrawable(R.drawable.background));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT));
scrollView.setPadding(20, 20, 20, 20);
//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int i=1; i<=3; i++){
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(head_params);
//Need to understand how put a margin top to the relativeLayout
//IMAGE VIEW
ImageView selectedPhoto = new ImageView(this);
selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
selectedPhoto.setImageResource(R.drawable.ic_launcher);
//TEXT VIEWS
TextView numberCopies = new TextView(this);
numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
numberCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
numberCopies.setText("2 copies ");
RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
numberCopies.setLayoutParams(layoutParamsNumberCopies);
TextView priceCopies = new TextView(this);
priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
priceCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
priceCopies.setTextColor(getResources().getColor(R.color.redColor));
priceCopies.setText("$ 25 ");
RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
priceCopies.setLayoutParams(layoutParamsPriceCopies);
relativeLayout.addView(selectedPhoto);
relativeLayout.addView(numberCopies);
relativeLayout.addView(priceCopies);
linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);
}
I have tried also relativeLayout.setMargins(20, 0, 0, 0); but seems that this method isn't available for relativeLayout. I'm not sure about it.
Thanks
Change your setmargins line to
head_params.setMargins(0, 80, 0, 0);
Margin parameters go clockwise starting with the left margin, i.e. (left, top, right, bottom). However, if you have other margins other than the top one, this will delete them. A much easier and more intuitive way is to simply write
header_params.topMargin=80;
Also not to forget, you never set your header_params back to your view. Add this line below:
relativeLayout.setLayourParams(header_params);
Try this
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)headView.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);//substitute parameters for left, top, right, bottom
Plz validate the below relative layout
RelativeLayout objRLActionBar=new RelativeLayout(this);
objRLActionBar.setId(2534);
RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,(int) (screenHeight*layoutHeights[1]));
objRLActionBarParams.addRule(RelativeLayout.BELOW,objRLTitleBar.getId());
objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));
ImageView objIVActivityIcon = new ImageView(this);
objRLActionBar.setId(25324);
RelativeLayout.LayoutParams objIVActivityIconParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objIVActivityIconParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,objRLActionBar.getId());
objIVActivityIcon.setLayoutParams(objIVActivityIconParams);
objIVActivityIcon.setImageResource(R.drawable.home_def);
objIVActivityIcon.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objIVActivityIcon);
ImageView objIVSeperator = new ImageView(this);
objIVSeperator.setId(25342);
RelativeLayout.LayoutParams objIVSeperatorParams=new RelativeLayout.LayoutParams(1,LayoutParams.FILL_PARENT);
objIVSeperatorParams.addRule(RelativeLayout.RIGHT_OF,objIVActivityIcon.getId());
objIVSeperator.setLayoutParams(objIVSeperatorParams);
objIVSeperator.setImageResource(R.drawable.separator);
objIVSeperator.setBackgroundColor(Color.parseColor("#1f3449"));
objRLActionBar.addView(objIVSeperator);
TextView objTVPageName = new TextView(this);
RelativeLayout.LayoutParams objTVPageNameParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objTVPageNameParams.addRule(RelativeLayout.RIGHT_OF,objIVSeperator.getId());
objTVPageName.setLayoutParams(objTVPageNameParams);
objTVPageName.setTextColor(Color.WHITE);
objTVPageName.setTextSize(TypedValue.COMPLEX_UNIT_PX,18+sizeAdjust);
objTVPageName.setText("House Details");
objTVPageName.setTypeface(null, Typeface.BOLD);
objTVPageName.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objTVPageName);
objRLBody.addView(objRLActionBar,objRLActionBarParams);
And the output is is as shown below
the image overlapped with text and the 'separator image' comes first! I need the components in this order objIVActivityIcon, objIVSeperator,objTVPageName. What is wrong with the above code plz help...
objRLActionBar.setId(25324);
should be
objIVActivityIcon.setId(25324);
I'm trying to position items inside my RelativeLayout so that the image appears first, the title to the right of it, and the content below the image area. Those three content items appear on top of each other. I understand that my LayoutParams probably aren't being set, but I'm not sure why or how to fix it. Any help? Thanks.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
RelativeLayout item = new RelativeLayout(this);
item.setId(i);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(320, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(i>0)
{
lp.addRule(RelativeLayout.RIGHT_OF,i-1);
}
item.setLayoutParams(lp);
ImageView iv = new ImageView(this);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
item.addView(iv);
}
else
{
iv.setImageResource(R.drawable.noimage);
item.addView(iv);
}
RelativeLayout.LayoutParams text1lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
text1lp.addRule(RelativeLayout.RIGHT_OF,iv.getId());
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(text1lp);
item.addView(tv);
RelativeLayout.LayoutParams text2lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
text2lp.addRule(RelativeLayout.BELOW,iv.getId());
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(lp);
item.addView(tv);
rl.addView(item);
}
That looks painful to debug. It sounds like maybe the relative rules aren't being set or accepted. I suggest simplifying to a static test set to verify that one row of views can be set relative to each other (and ensure that one of them is anchored to the layout itself).
friends,
i want to set android:layout_centerVertical="true" property of layout
through java code of an image.
can any one guide me how to achieve this. here is my code.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.height = (int)totalHeight;
img.setLayoutParams(params);
i have tried using setScaleType(ScaleType.FIT_CENTER) but no use.
any help would be appriciated.
Try this Code..
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
img.setLayoutParams(params);
Correct me if I'm wrong but it sounds like you are trying to set the alignment (or positional orientation) of an image inside of a layout? To do that you need to set the gravity property of the layout containing the View you align.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
layout.addView(imageView);
In this example I'm programmatically adding an image to a RelativeLayout in my layout resource, adding an ImageView, and aligning it so it will be placed at the top, vertical center position.
In the new SDK there's no Gravity.VERTICAL_CENTER, but maybe you could set this with
layout.setGravity(Gravity.CENTER).