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?

  • Shared elements and content transitions in Fragments
  • how to add listener to autocompletetextview, android?
  • Android Google Maps v2 - Debug key vs Release key
  • how to set height of gridview programmatically android
  • Cannot resolve symbol GooglePlayServicesClient on new Android Studio Project
  • Is it possible to have a “Profile Owner” app in Android Lollipop that is not co-present
  • 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!

    Related posts:

    How to restrict android app to specific device make?
    In-App Payments for Android Amazon AppStore
    When to shutdown executorservice in android application
    Actionbarsherlock back button doesn&#039;t go back
    How to open a new fragment from the navigation drawer?
    How do I access the camera on Android phones?
  • ActivityCompat.requestPermissions not working
  • Extract substring from a string
  • Server-side verification of Google Play In-app billing version 3 purchase
  • Android: Failed to ensure directory
  • Android Studio can't find library classes after Gradle build
  • Parallel HTTP requests with Retrofit
  • 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.