Skip to main content

set the absolute position of a view in 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...) Lets say I have a screen 240x320px, and I want to put an ImageView which is 20x20px with its center at the position (100,100). What do I have to do? Thanks for the help, this is driving me crazy.




Comments

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. when you use arrays to set the positions of many ImageView, you should keep calling "new Relayout.LayoutParams( int , int ),or you will make all ImageViews the same position!

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.