How read ImageView margin programmatically?

I have an ImageView which is written in a layout file and looks like this


        <ImageView android:id="@+id/news_image" 
            android:background="#aaaaaa" />


How can I read the android:layout_marginLeft attribute in my Activity?

  • ViewPager with FragmentPagerAdapter does not show content
  • How can I access my localhost through Android phone?
  • library resolve to a path with no file
  • How do I make the colon vertical center in TextView
  • Determine and bind click or “touch” event
  • DialogFragment in PreferenceActivity
  • I tried this following code but LayoutParams of my ImageView doesn’t have any margin members (like for example LinearLayout.LayoutParams has).

    ImageView imageView = (ImageView)findViewById(;
    LayoutParams lp = imageView.getLayoutParams();
    int marginLeft = lp.marginLeft; // DON'T do this! It will not work 
                                    // cause there is no member called marginLeft

    Any suggestions how to get the margin from a ImageView?

    Thank you!

  • Animate visibility modes, GONE and VISIBLE
  • Android Fragment lifecycle over orientation changes
  • List all the folder and files of Dropbox using Dropbox API
  • When to use actors in libgdx? What are cons and pros?
  • Is it possible to run R from a tablet using Honeycomb (Android 3.0)?
  • Use RxJava and Retrofit to iterate through list and augment results based on subqueries
  • 2 Solutions collect form web for “How read ImageView margin programmatically?”

    you have to cast the LayoutParams to the type that is specific to the layout the view is in. That is, being your view inside a LinearLayout, you’ll have to do the following:

    ImageView imageView = (ImageView)findViewById(;
    LinearLayout.LayoutParams lp =
                    (LinearLayout.LayoutParams) imageView.getLayoutParams();

    now lp will let you access margin* fields.

    Your sol for it is as below:

     LinearLayout. LayoutParams lp = (android.widget.LinearLayout.LayoutParams) imageView.getLayoutParams();
      int i=lp.leftMargin;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.