How can I center an item on the screen programmatically? - android

How can I center an item on the screen programmatically?
I want to set imageView in the center of any screen that will run my app.

Assuming your parent is RelativeLayout use this:
ImageView myImageView = (ImageView) findViewById(YOUR_IMAGE_VIEW_ID);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myImageView.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
myImageView.setLayoutParams(params);

Related

How to place center of a view inside FrameLayout

I have a FrameLayout which already holds a child view, and I know its width and height in pixels. I want to programmatically add a new view so that its center is at the specific point of the frame layout. Size of the view is not known and its FrameLayout.LayoutParams is set to WRAP_CONTENT, WRAP_CONTENT. How can I do it?
Say you want to position a TextView in the middle of a FrameLayout.
TextView text = (TextView)frame.findViewById(R.id.text);
FrameLayout frame = (FrameLayout)findViewById(R.id.frame1);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Now add your TextView to your FrameLayout and set gravity to center
frame.addView(text);
params.gravity=Gravity.CENTER;
I haven't tried it but I think it should work.
In KOTLIN
viewToADD?.layoutParams = FrameLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).
apply {gravity = Gravity.CENTER and Gravity.CENTER_VERTICAL}
frameLayout.addView(viewToADD)

Aligning ImageView to LinearLayout programmatically

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());

Set framelayout margin and its children width, height and position

I am currently having a programatically created framelayout with a imageview and a text view. By original layout is relative layout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rel);
FrameLayout fr = new FrameLayout(this);
fr.setBackgroundResource(R.drawable.question_bar_icon);
ImageView b1 = new ImageView(this);
TextView b2 = new TextView(this);
b1.setBackgroundResource(R.drawable.mcq);
fr.addView(b1);
fr.addView(b2);
layout.addView(fr);
Now i am unable to resize the imageview and the button, also m unable to position them, in the sence textview in the center and imageview in the verticle center and left positioned.
I tried layout params, but not working, any help would be greatly appreciated!
Try using the overloaded version of addView that takes a LayoutParameter as well in every case you are programatically adding a view, otherwise it is inserted to the ViewGroup with default layout parameters. (P.S. you can also control the order of insertion).
For example for the ImageView you wanted to be in the center of the frame layout:
int width = FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
int height= FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height, Gravity.CENTER);
fr.addView(b1,lp);
Do the same for the other view, and most importantly, do the same, using RelativeLayout.LayoutParams for inserting the frame element itself into your RelativeLayout.

Adding button on top of Imageview at runtime

I am creating a imageview at runtime and now want a add a button on top of the Imageview dynamically at run time and if I pan the image in the imageview the button in it should also pan accordingly. The problem is that Imageview does not allow to add subviews in it.
I am able to add the button on top of the imageview but when I pan the imageview the button remains constant in the screen. Any idea on how to do this?
Here is what I did:
panImageView = new PanView(this); //Custom Imageview class to handle panning
panImageView.setScaleType(ImageView.ScaleType.CENTER);
//panimg.setAdjustViewBounds(true);
FrameLayout mainRL = (FrameLayout) findViewById(R.id.mainRelativeLayout);
mainRL.addView(panImageView);
ImageWorker imgTask = new ImageWorker(panImageView, is);
imgTask.execute(2000, 2000);
//Start button
Button startButton = new Button(this);
int iRndBtn = R.drawable.round_button;
startButton.setBackgroundResource(iRndBtn);
startButton.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
//layoutParams.setMargins(intialPage.mapStartX, intialPage.mapStartY, 0, 0);
layoutParams.setMargins(200, 200, 0, 0);
startButton.setLayoutParams(layoutParams);
//startButton.
mainRL.addView(startButton,layoutParams);
Add Image view first in any Layout and the add Button in that Layout.
Use a RelativeLayout with the ImageView & button. set the button visibility to gone at start. When you need change the visibility.

Place an imageView dynamically in Android

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);

Categories

Resources