Change View position

My View is not generated programmatically, but it is found with findbyid function. After getting view I like to position it properly.

To do that I am using layout function to change position of view in onCreate with given parameters. Problem is that is not working properly and view stays in top left corner.

Function works fine while using it onTouch after activity is loaded. What I am doing wrong, is there another to set view based on these four parameters that layout uses.


Params are loaded from DB. this is what I use in onCreate:

setContentView(R.layout.baseview); View view=(ImageView) findViewById(R.id.blah); int top=cursor.getInt(cursor.getColumnIndex(CallerViewDB.TOP)); int bottom=cursor.getInt(cursor.getColumnIndex(CallerViewDB.BOTTOM)); int left=cursor.getInt(cursor.getColumnIndex(CallerViewDB.LEFT)); int right=cursor.getInt(cursor.getColumnIndex(CallerViewDB.RIGHT)); view.layout(left, top, right, bottom);

Loaded values are right, checked it w sqllite browser. It seems that calling layout function in onCreate does not anything. Is there any alternative?


Ok after some reasearch this is how it is done:

RelativeLayout.LayoutParams parms=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); parms.leftMargin=left; parms.rightMargin=right; parms.topMargin=top; parms.bottomMargin=bottom; view.setLayoutParams(parms);


/** onPreDraw: Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs. */ ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); OnPreDrawListener onPreDrawListener = new OnPreDrawListener() { @Override public boolean onPreDraw() { view.layout(....); return true; } }; viewTreeObserver.addOnPreDrawListener(onPreDrawListener);


This is a very bad idea in most scenario. Android devices vary in screen size, orientation and resolution. It is recommended that you do not use absolute positioning, and instead using relative positioning.

Read up on RelativeLayout, and I'm sure it will do what you need.


  • Android sqlite log reports and db.close()
  • What is meant by ?? in Dart
  • Progress Bar of DownloadManager with percentage
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (in jre's libs)
  • How to do popup on new site for 301 redirected users?
  • $this->params returns null in cakephp model
  • How to get a android ListView item selector to use state_pressed
  • Query timeout expired in django-mssql when executing custom SQL directly
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Android: How do you create an EditText field in java class
  • Picasso Taking time to load images
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • Pre-populated SQLite Database not reading properly in Android Studio
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Converting query results into DataFrame in python
  • Possible to get mouse events fired when cursor is outside page?
  • Rails Find when some params will be blank
  • Use of this Javascript
  • Test if a set exists before trying to drop it
  • C++ Partial template specialization - design simplification
  • Fetching methods from BroadcastReceiver to update UI
  • How to set my toolbar fixed while scrolling android
  • Symfony2: How to get request parameter
  • How to get next/previous record number?
  • GridView Sorting works once only
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • Can't mass-assign protected attributes when import data from csv file
  • Sorting a 2D array using the second column C++
  • Android Heatmap on canvas or ImageView
  • java string with new operator and a literal