spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Get ImageView width in getView


The problem is that when you try to getWidth, your ImageView have not been appeared on the screen, so it always returns 0. So there are two ways, you can get the width:

First way

You need to measure your ImageView and then get the measuredWidth:

holder.photoPost = (ImageView)
convertView.findViewById(R.id.photoPost);
holder.photoPost.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);

int targetHeight =
holder.photoPost.getMeasuredWidth() * (9/16);

Second way

Add onGlobalLayoutListener to your ImageView and then, you will be able to get the width:

holder.photoPost = (ImageView)
convertView.findViewById(R.id.photoPost);
holder.photoPost.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.JELLY_BEAN) {
               
holder.photoPost.getViewTreeObserver()
                       
.removeOnGlobalLayoutListener(this);
            } else {
               
holder.photoPost.getViewTreeObserver()
                       
.removeGlobalOnLayoutListener(this);
            }

            int targetHeight =
holder.photoPost.getWidth() * (9/16)
        }
});

Categories : Android

Related to : Get ImageView width in getView
Setting same Height and width of ImageView in percentage that is in Listview
Try to properly use weight in LinearLayout then no need to change any height or width value at runtime : <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" a

Categories : Android
For a ListView adapter, how is getView() called?
Firstly, do I need to call getView() manually or does it get called automatically? No, it get called automatically. If so, when is it actually called? It will be called when user do something to your ListView, like when the ListView displaying a new item when you scrolling it. Lastly, I noticed there's a parameter for "position" with in the list. Is that something I have to incr

Categories : Android
getView of element that you're swiping (onFling) for android?
You may refer to this sample application Swipe_To_Delete You may use ListView to list your data items which are got from the db. Then modify the getSwipeItem() method in the MainActivity of the example, as follows. @Override public void getSwipeItem(boolean isRight, int position) { [List_Of_Items].remove(position); [Your_Adapter].notifyDataSetChanged(); }

Categories : Java
Nullpointerexception custom Adapter at getview no inflater
Change you code by this, private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Override public View getView(int position, View convertView, ViewGroup parent) { //trying to reuse a recycled view ViewHolder holder;

Categories : Java
fragment - getView() method return NULL
You should notice that commit() will run on Handler.post(). That means that your fragment's view will be created AFTER onNavigationDrawerItemSelected. Better if you call view-related-methods from inside fragment to constrain view's lifecycle.

Categories : Android
Recently Add
I'm trying to create an onListItemClick even for a customized adapter. The array of items is found in res/values folder
No navigation/status bar touch listener exists, why?
z-index in Relative layout
java.lang.SecurityException: Permission Denial: getIntentSender() when using UiAutomation in a test
mvvmcross videoview URL binding
Android Emulator Unfortunately dev tools has stopped
Square Dagger error: class is exclusively members injected / cannot be scoped required
Installing a widget from an APP
How to stop onDraw() methods causing jerky scrolling of a ListView
How to find my Google Play Services Android BASE64_PUBLIC_KEY
Set style programatically does not work
How to get contacts from phonebook and sim but skype, facebook etc. in android
Where to put JUnit test in Android project
My Android Studio project keeps running my old project/application
how to avoid OUtOfMemory problems with create Bitmap
What tools use and how do continuous integration and test in Android with eclipse?
Converting drawable resource to Bitmap Android
Fragment Transaction show() does not show fragment on second time
Espresso - How to match ActionBar visibility?
Viewport setting ignored in Phonegap Android app, but only on certain phones?
how to open an activity with url scheme android
Android AsyncTask and Thread different behavior
How can one ellicit a Toast from a Service to a Fragment
GCMIntentService onRegistered not called
LinearLayout cropping but not expanding in Scrollview
Get a file from the application directory
How to set layout params using view's on touch listener?
How to disable Google Fit and revoke permissions from the app itself
Android Wear - receiving message from Wear without WearableListenerService?
How can I start new activity from another package?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.