Custom View in Relative Layout overlapping other Custom Views [duplicate] - android
Is it possible to set the absolute position of a view in Android? (I know that there is an AbsoluteLayout, but it's deprecated...)
For example, if I have a 240x320px screen, how could I add an ImageView which is 20x20px such that its center is at the position (100,100)?
You can use RelativeLayout. Let's say you wanted a 30x40 ImageView at position (50,60) inside your layout. Somewhere in your activity:
// Some existing RelativeLayout from your layout xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
More examples:
Places two 30x40 ImageViews (one yellow, one red) at (50,60) and (80,90), respectively:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
iv = new ImageView(this);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
rl.addView(iv, params);
Places one 30x40 yellow ImageView at (50,60) and another 30x40 red ImageView <80,90> relative to the yellow ImageView:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
int yellow_iv_id = 123; // Some arbitrary ID value.
iv = new ImageView(this);
iv.setId(yellow_iv_id);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
// This line defines how params.leftMargin and params.topMargin are interpreted.
// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);
rl.addView(iv, params);
In general, you can add a View in a specific position using a FrameLayout as container by specifying the leftMargin and topMargin attributes.
The following example will place a 20x20px ImageView at position (100,200) using a FrameLayout as fullscreen container:
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:background="#33AAFF"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
Activity / Fragment / Custom view
//...
FrameLayout root = (FrameLayout)findViewById(R.id.root);
ImageView img = new ImageView(this);
img.setBackgroundColor(Color.RED);
//..load something inside the ImageView, we just set the background color
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20);
params.leftMargin = 100;
params.topMargin = 200;
root.addView(img, params);
//...
This will do the trick because margins can be used as absolute (X,Y) coordinates without a RelativeLayout:
Just to add to Andy Zhang's answer above, if you want to, you can give param to rl.addView, then make changes to it later, so:
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
Could equally well be written as:
params = new RelativeLayout.LayoutParams(30, 40);
rl.addView(iv, params);
params.leftMargin = 50;
params.topMargin = 60;
So if you retain the params variable, you can change the layout of iv at any time after adding it to rl.
A more cleaner and dynamic way without hardcoding any pixel values in the code.
I wanted to position a dialog (which I inflate on the fly) exactly below a clicked button.
and solved it this way :
// get the yoffset of the position where your View has to be placed
final int yoffset = < calculate the position of the view >
// position using top margin
if(myView.getLayoutParams() instanceof MarginLayoutParams) {
((MarginLayoutParams) myView.getLayoutParams()).topMargin = yOffset;
}
However you have to make sure the parent layout of myView is an instance of RelativeLayout.
more complete code :
// identify the button
final Button clickedButton = <... code to find the button here ...>
// inflate the dialog - the following style preserves xml layout params
final View floatingDialog =
this.getLayoutInflater().inflate(R.layout.floating_dialog,
this.floatingDialogContainer, false);
this.floatingDialogContainer.addView(floatingDialog);
// get the buttons position
final int[] buttonPos = new int[2];
clickedButton.getLocationOnScreen(buttonPos);
final int yOffset = buttonPos[1] + clickedButton.getHeight();
// position using top margin
if(floatingDialog.getLayoutParams() instanceof MarginLayoutParams) {
((MarginLayoutParams) floatingDialog.getLayoutParams()).topMargin = yOffset;
}
This way you can still expect the target view to adjust to any layout parameters set using layout XML files, instead of hardcoding those pixels/dps in your Java code.
Just in case it may help somebody, you may also try this animator ViewPropertyAnimator as below
myView.animate().x(50f).y(100f);
myView.animate().translateX(pixelInScreen)
Note: This pixel is not relative to the view. This pixel is the pixel
position in the screen.
credits to bpr10 answer
Place any view on your desire X & Y point
layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity" >
<AbsoluteLayout
android:id="#+id/absolute"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rlParent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn_blue_matte" />
</RelativeLayout>
</AbsoluteLayout>
</RelativeLayout>
Java Class
public class MainActivity extends Activity {
private RelativeLayout rlParent;
private int width = 100, height = 150, x = 20, y= 50;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(width, height, x, y);
rlParent = (RelativeLayout)findViewById(R.id.rlParent);
rlParent.setLayoutParams(param);
}
}
Done
Try below code to set view on specific location :-
TextView textView = new TextView(getActivity());
textView.setId(R.id.overflowCount);
textView.setText(count + "");
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
textView.setTextColor(getActivity().getResources().getColor(R.color.white));
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// to handle click
}
});
// set background
textView.setBackgroundResource(R.drawable.overflow_menu_badge_bg);
// set apear
textView.animate()
.scaleXBy(.15f)
.scaleYBy(.15f)
.setDuration(700)
.alpha(1)
.setInterpolator(new BounceInterpolator()).start();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = 100; // margin in pixels, not dps
layoutParams.leftMargin = 100; // margin in pixels, not dps
textView.setLayoutParams(layoutParams);
// add into my parent view
mainFrameLaout.addView(textView);
My code for Xamarin,
I am using FrameLayout for this purpose and following is my code:
List<object> content = new List<object>();
object aWebView = new {ContentType="web",Width="300", Height = "300",X="10",Y="30",ContentUrl="http://www.google.com" };
content.Add(aWebView);
object aWebView2 = new { ContentType = "image", Width = "300", Height = "300", X = "20", Y = "40", ContentUrl = "https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4" };
content.Add(aWebView2);
FrameLayout myLayout = (FrameLayout)FindViewById(Resource.Id.frameLayout1);
foreach (object item in content)
{
string contentType = item.GetType().GetProperty("ContentType").GetValue(item, null).ToString();
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(Convert.ToInt32(item.GetType().GetProperty("Width").GetValue(item, null).ToString()), Convert.ToInt32(item.GetType().GetProperty("Height").GetValue(item, null).ToString()));
param.LeftMargin = Convert.ToInt32(item.GetType().GetProperty("X").GetValue(item, null).ToString());
param.TopMargin = Convert.ToInt32(item.GetType().GetProperty("Y").GetValue(item, null).ToString());
switch (contentType) {
case "web":{
WebView webview = new WebView(this);
//webview.hei;
myLayout.AddView(webview, param);
webview.SetWebViewClient(new WebViewClient());
webview.LoadUrl(item.GetType().GetProperty("ContentUrl").GetValue(item, null).ToString());
break;
}
case "image":
{
ImageView imageview = new ImageView(this);
//webview.hei;
myLayout.AddView(imageview, param);
var imageBitmap = GetImageBitmapFromUrl("https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4");
imageview.SetImageBitmap(imageBitmap);
break;
}
}
}
It was useful for me because I needed the property of view to overlap each other on basis of their appearance, e.g the views get stacked one above other.
Related
i m using 4 imageview in relativelayout and i want to make dynamically. u can see screenshot below
i m using 4 imageview in relativelayout and that images size equal to each other in relative layout but want to make dynamically but i m not getting Heading final ImageView profile_img1 = (ImageView) view.findViewById(R.id.profile_img1); final ImageView profile_img2 = view.findViewById(R.id.profile_img2); Display display = activity.getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); int width = point.x; final double margin_15 = width * 0.15; final RelativeLayout relativeLayout=view.findViewById(R.id.relative_profile); RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); parms.setMargins((int) margin_15, 0, (int) margin_15, 0); relativeLayout.setLayoutParams(parms); // Toast.makeText(activity,width+"",Toast.LENGTH_SHORT).show(); relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { #Override public void onGlobalLayout() { relativeLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); width_relativelayout = relativeLayout.getMeasuredWidth(); h_relativelayout = relativeLayout.getMeasuredHeight(); // Toast.makeText(activity,width_linearlayout+"===",Toast.LENGTH_SHORT).show(); RelativeLayout.LayoutParams parms_img = new RelativeLayout.LayoutParams( width_relativelayout/2,350); parms_img.addRule(RelativeLayout.ALIGN_PARENT_LEFT); profile_img1.setLayoutParams(parms_img); RelativeLayout.LayoutParams parms_img2 = new RelativeLayout.LayoutParams(width_relativelayout/2,300); parms_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); profile_img1.setLayoutParams(parms_img2); profile_img2.setLayoutParams(parms_img2); } });
You can use RecyclerView and Adapter for same. and Set GridLayoutManager with number count to show, like 2 for two image view horizontally. Or you can ArrayList of ImageView.
How to make LinearLayout height to be the highest height of it's child component?
I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot: And the code: LinearLayout ll; LinearLayout.LayoutParams lp; CheckBox ch; int id = 1200, i, j; for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) { ll = new LinearLayout(NotificationSettings.this); lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); ch = new CheckBox(NotificationSettings.this); lp.weight = 1.0f; ch.setLayoutParams(lp); ch.setText(selections.get(i)); ch.setChecked(isSelected); ch.setTextColor(color); ch.setId(j); ll.addView(ch); ch = new CheckBox(NotificationSettings.this); ch.setLayoutParams(lp); ch.setText(selections.get(i + 1)); ch.setChecked(isSelected); ch.setTextColor(color); ch.setId(j + 1); ll.addView(ch); RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); if (id == 1200) p.addRule(RelativeLayout.BELOW, addBelow); else p.addRule(RelativeLayout.BELOW, id); ll.setLayoutParams(p); ll.setId(++id); rl.addView(ll); } Edit: When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout? Or try changing your Relative Layout Params' height to WRAP_CONTENT Change RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); To RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Can't place ImageView at top corner at runtime
I want to place a ImageView at top left corner when runtime, but it does not work. This is my layout and code: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:id="#+id/layout_main" android:layout_height="fill_parent" tools:context=".Main"> </RelativeLayout> This is code: RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main); ImageView imageView =new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.green_apple); imageView.setScaleX(0.3f); imageView.setScaleY(0.3f); imageView.setLeft(0); imageView.setTop(0); layout.addView(imageView); I also tried: RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main); ImageView imageView =new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.green_apple); imageView.setScaleX(0.3f); imageView.setScaleY(0.3f); imageView.setX(0); imageView.setY(0); layout.addView(imageView); And: RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main); ImageView imageView =new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.green_apple); imageView.setScaleX(0.3f); imageView.setScaleY(0.3f); layout.addView(imageView); set = new AnimatorSet(); ObjectAnimator aniX = ObjectAnimator.ofFloat(imageView, "x", 0); aniX.setDuration(100); ObjectAnimator aniY = ObjectAnimator.ofFloat(imageView, "y", 0); aniY.setDuration(100); set.playTogether(aniX, aniY); set.start() But same result, this is my result: Always have large space to Top and Left screen, Although I set 0,0. I also try set height and width of screen, it fly out of screen. I don't know why. Thank you very much for anyone can explain and how to help me fix it
That is because you use setScaleX and setScaleY, so try the code below: final ImageView iv = new ImageView(context); iv.setImageResource(R.drawable.green_apple); rl.addView(iv); iv.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { #Override public boolean onPreDraw() { iv.getViewTreeObserver().removeOnPreDrawListener(this); int w = (int) (iv.getWidth() * 0.3); int h = (int) (iv.getHeight() * 0.3); RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(w, h); iv.setLayoutParams(rlp); return true; } });
Design dynamic hotspot on Image in Android
I have to develop a UI like below: I want to show this type of image and show hotspot on that image. The position of hotspot will be dynamic, as per x,y and radius provided the circle will be drawn on the original picture. The user can click on the hotspots and onclick action will be defined on the specific hotspot on which the user will click. What is best process to develop this type of UI?
Make your main layout a RelativeLayout and then you can add programmatically a ImageView with an onClickListener to your layout with the following code: private void addImageView(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){ ImageView imageView = new ImageView(this); imageView.setAdjustViewBounds(false); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.height = height; params.width = width; imageView.setLayoutParams(params); imageView.setScaleType(ImageView.ScaleType.FIT_XY); //remove this if you want to keep aspect ratio imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); //here goes your drawable params.leftMargin = x - width/2; params.topMargin = y - height/2; imageView.setOnClickListener(onClickListener); mainLayout.addView(imageView); } to use it you call: RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //this is your main layout addImageButton(mainLayout, 200, 300, 200, 200, new OnClickListener() { #Override public void onClick(View v) { Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show(); } }); You can also use a ImageButton to achive the same porpose, although the image size will be affected by button border: private void addImageButton(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){ ImageButton imageButton = new ImageButton(this); imageButton.setAdjustViewBounds(true); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.height = height; params.width = width; imageButton.setLayoutParams(params); imageButton.setScaleType(ImageView.ScaleType.FIT_XY); imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); params.leftMargin = x - width/2; params.topMargin = y - height/2; imageButton.setOnClickListener(onClickListener); mainLayout.addView(imageButton); } Try it.
Set the absolute position of a view
Is it possible to set the absolute position of a view in Android? (I know that there is an AbsoluteLayout, but it's deprecated...) For example, if I have a 240x320px screen, how could I add an ImageView which is 20x20px such that its center is at the position (100,100)?
You can use RelativeLayout. Let's say you wanted a 30x40 ImageView at position (50,60) inside your layout. Somewhere in your activity: // Some existing RelativeLayout from your layout xml RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout); ImageView iv = new ImageView(this); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 50; params.topMargin = 60; rl.addView(iv, params); More examples: Places two 30x40 ImageViews (one yellow, one red) at (50,60) and (80,90), respectively: RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout); ImageView iv; RelativeLayout.LayoutParams params; iv = new ImageView(this); iv.setBackgroundColor(Color.YELLOW); params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 50; params.topMargin = 60; rl.addView(iv, params); iv = new ImageView(this); iv.setBackgroundColor(Color.RED); params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 80; params.topMargin = 90; rl.addView(iv, params); Places one 30x40 yellow ImageView at (50,60) and another 30x40 red ImageView <80,90> relative to the yellow ImageView: RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout); ImageView iv; RelativeLayout.LayoutParams params; int yellow_iv_id = 123; // Some arbitrary ID value. iv = new ImageView(this); iv.setId(yellow_iv_id); iv.setBackgroundColor(Color.YELLOW); params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 50; params.topMargin = 60; rl.addView(iv, params); iv = new ImageView(this); iv.setBackgroundColor(Color.RED); params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 80; params.topMargin = 90; // This line defines how params.leftMargin and params.topMargin are interpreted. // In this case, "<80,90>" means <80,90> to the right of the yellow ImageView. params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id); rl.addView(iv, params);
In general, you can add a View in a specific position using a FrameLayout as container by specifying the leftMargin and topMargin attributes. The following example will place a 20x20px ImageView at position (100,200) using a FrameLayout as fullscreen container: XML <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="#+id/root" android:background="#33AAFF" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> Activity / Fragment / Custom view //... FrameLayout root = (FrameLayout)findViewById(R.id.root); ImageView img = new ImageView(this); img.setBackgroundColor(Color.RED); //..load something inside the ImageView, we just set the background color FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20); params.leftMargin = 100; params.topMargin = 200; root.addView(img, params); //... This will do the trick because margins can be used as absolute (X,Y) coordinates without a RelativeLayout:
Just to add to Andy Zhang's answer above, if you want to, you can give param to rl.addView, then make changes to it later, so: params = new RelativeLayout.LayoutParams(30, 40); params.leftMargin = 50; params.topMargin = 60; rl.addView(iv, params); Could equally well be written as: params = new RelativeLayout.LayoutParams(30, 40); rl.addView(iv, params); params.leftMargin = 50; params.topMargin = 60; So if you retain the params variable, you can change the layout of iv at any time after adding it to rl.
A more cleaner and dynamic way without hardcoding any pixel values in the code. I wanted to position a dialog (which I inflate on the fly) exactly below a clicked button. and solved it this way : // get the yoffset of the position where your View has to be placed final int yoffset = < calculate the position of the view > // position using top margin if(myView.getLayoutParams() instanceof MarginLayoutParams) { ((MarginLayoutParams) myView.getLayoutParams()).topMargin = yOffset; } However you have to make sure the parent layout of myView is an instance of RelativeLayout. more complete code : // identify the button final Button clickedButton = <... code to find the button here ...> // inflate the dialog - the following style preserves xml layout params final View floatingDialog = this.getLayoutInflater().inflate(R.layout.floating_dialog, this.floatingDialogContainer, false); this.floatingDialogContainer.addView(floatingDialog); // get the buttons position final int[] buttonPos = new int[2]; clickedButton.getLocationOnScreen(buttonPos); final int yOffset = buttonPos[1] + clickedButton.getHeight(); // position using top margin if(floatingDialog.getLayoutParams() instanceof MarginLayoutParams) { ((MarginLayoutParams) floatingDialog.getLayoutParams()).topMargin = yOffset; } This way you can still expect the target view to adjust to any layout parameters set using layout XML files, instead of hardcoding those pixels/dps in your Java code.
Just in case it may help somebody, you may also try this animator ViewPropertyAnimator as below myView.animate().x(50f).y(100f); myView.animate().translateX(pixelInScreen) Note: This pixel is not relative to the view. This pixel is the pixel position in the screen. credits to bpr10 answer
Place any view on your desire X & Y point layout file <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test.MainActivity" > <AbsoluteLayout android:id="#+id/absolute" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="#+id/rlParent" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="#+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#drawable/btn_blue_matte" /> </RelativeLayout> </AbsoluteLayout> </RelativeLayout> Java Class public class MainActivity extends Activity { private RelativeLayout rlParent; private int width = 100, height = 150, x = 20, y= 50; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(width, height, x, y); rlParent = (RelativeLayout)findViewById(R.id.rlParent); rlParent.setLayoutParams(param); } } Done
Try below code to set view on specific location :- TextView textView = new TextView(getActivity()); textView.setId(R.id.overflowCount); textView.setText(count + ""); textView.setGravity(Gravity.CENTER); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); textView.setTextColor(getActivity().getResources().getColor(R.color.white)); textView.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { // to handle click } }); // set background textView.setBackgroundResource(R.drawable.overflow_menu_badge_bg); // set apear textView.animate() .scaleXBy(.15f) .scaleYBy(.15f) .setDuration(700) .alpha(1) .setInterpolator(new BounceInterpolator()).start(); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); layoutParams.topMargin = 100; // margin in pixels, not dps layoutParams.leftMargin = 100; // margin in pixels, not dps textView.setLayoutParams(layoutParams); // add into my parent view mainFrameLaout.addView(textView);
My code for Xamarin, I am using FrameLayout for this purpose and following is my code: List<object> content = new List<object>(); object aWebView = new {ContentType="web",Width="300", Height = "300",X="10",Y="30",ContentUrl="http://www.google.com" }; content.Add(aWebView); object aWebView2 = new { ContentType = "image", Width = "300", Height = "300", X = "20", Y = "40", ContentUrl = "https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4" }; content.Add(aWebView2); FrameLayout myLayout = (FrameLayout)FindViewById(Resource.Id.frameLayout1); foreach (object item in content) { string contentType = item.GetType().GetProperty("ContentType").GetValue(item, null).ToString(); FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(Convert.ToInt32(item.GetType().GetProperty("Width").GetValue(item, null).ToString()), Convert.ToInt32(item.GetType().GetProperty("Height").GetValue(item, null).ToString())); param.LeftMargin = Convert.ToInt32(item.GetType().GetProperty("X").GetValue(item, null).ToString()); param.TopMargin = Convert.ToInt32(item.GetType().GetProperty("Y").GetValue(item, null).ToString()); switch (contentType) { case "web":{ WebView webview = new WebView(this); //webview.hei; myLayout.AddView(webview, param); webview.SetWebViewClient(new WebViewClient()); webview.LoadUrl(item.GetType().GetProperty("ContentUrl").GetValue(item, null).ToString()); break; } case "image": { ImageView imageview = new ImageView(this); //webview.hei; myLayout.AddView(imageview, param); var imageBitmap = GetImageBitmapFromUrl("https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"); imageview.SetImageBitmap(imageBitmap); break; } } } It was useful for me because I needed the property of view to overlap each other on basis of their appearance, e.g the views get stacked one above other.