I'm building an application with the following layout:
A RelativeLayout container has a ImageView child as its background and two other RelativeLayouts as the actual content of the page.
The rules of these views are set up as follows:
// Define rules and params for views in the container
ImageView backgroundImg = new ImageView(this);
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lpImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backgroundImg.setImageResource(R.drawable.baggrund_smartphones_retina);
backgroundImg.setLayoutParams(lpImg);
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.fragment_main_layout);
RelativeLayout storeLayout = (RelativeLayout)findViewById(R.id.fragment_store_layout);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_main_layout);
mainLayout.setLayoutParams(lp1);
storeLayout.setLayoutParams(lp2);
root.removeAllViews();
//Add background first to ensure its in the back
snapView.addInfoPage(backgroundImg);
snapView.addInfoPage(mainLayout);
Now here is my problem. But my RelativeLayouts are aligning properly to their parent, but the ImageView is not. Instead the imageview is places like this:
What am I doing wrong?
may be your image dimensions is smaller than the screen dimensions of your parentLayout .
So if you want the image to Fit all the screen use the attribut android:scaleType in your imageView Tag via xml :
<ImageView android:id="#+id/img"
blabla
blabla
android:scaleType="fitXY" />
or programmatically :
imgView.setScaleType(ScaleType.FIT_XY);
change like this
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
In place of
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
And fix imageview like this
backgroundImg.setLayoutParams(ScaleType.FIT_XY);
Related
I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.
I don't know how I can set the TextView on the right side in my RelativeLayout. Adding gravity to the TextView didn't work. Thanks for helping me.
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar
textView = new TextView(context);
textView.setText(String.valueOf(nuke_count));
textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon));
textView.setTextSize(16);
relativeLayout.addView(textView); // TextView over image
relativeLayout.addView(imageView); // adding image
linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout
You can set layout parameters to TextView and also set alignment of parent right.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewsetLayoutParams(params);
Now You can Use this code :)
I need to programmatically align an ImageView to the bottom of a LinearLayout.
I, of course, already tried to add the ImageView as a child of the LinearLayout, but the image get shrinked (cause the LinearLayout is smaller than the image), and I need the image not be resized, only bottom-aligned.
I also tried this :
RelativeLayout main_layout = (RelativeLayout) view.findViewById(R.id.main_layout);
LinearLayout line_0_layout = (LinearLayout) view.findViewById(R.id.line_0_layout);
ImageView horse_img = new ImageView(getActivity());
horse_img.setImageResource(R.drawable.horse);
RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout_params.addRule(RelativeLayout.ALIGN_BOTTOM, line_0_layout.getId());
horse_img.setLayoutParams(layout_params);
main_layout.addView(horse_img);
but it doesn't work : the image is added to the view but not aligned to the LinearLayout.
It seems simple question, but it's not.
Thanks in advance for your help.
layout_params.addRule(RelativeLayout.BELOW, line_0_layout.getId());
I've RelativeLayout created in layout/activity.xml
And i want to add some elements there programmatically by following way:
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlayout.addView(CustomView,p);
And it works, but elements which were added doesn't fill all view, but i need it.
and also i want to add such elements in square (width=height), how can I do it?
To fill all view Use LayoutParams.MATCH_PARENT instead of LayoutParams.WRAP_CONTENT.
To make layout as square just create int width,height = 300; and then :
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height);
or pass LayoutParams.WRAP_CONTENT into RelativeLayout.LayoutParams and change height and width of your custom view.
To make view square :
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
to make fill on all view you can use this:
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
Also you can add rules for your layout items like this :
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Best wishes.
i am adding an imageView to a Relative layout.It shows up at the beginning of layout
I have two buttons in the layout.
I button to the layout left and one to layout right. Now i want to place these under the button to right.
how can i do that. My code is
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
layout.addView(image);
Try using the
android:Layout_below="#id/yourButtonId" or
android:LayoutBelow="#id/yourButtonId"
Programmatically it means :
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, yourButtonVariable.getId());
yourRelativeLayout.addView(yourImageView, rlp);