I implemented onConfigurationChnage(..) to read value of view height and width on orientation configuration chnage
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Log.i(TAG, "onConfigurationChanged: "+newConfig.orientation+", "+height+", "+width + ", "+mTempeFrameLayout.getWidth());
}
but it always reply old value, if orientation get change it gave last orientation (probably)value.
Screen width/height using displaymetrics is coming right properly but same is not working for any view.
Any one have idea..how can get correct value on orientation Change?
getWidth() returns the width as the View is laid out, meaning you need to wait until it is drawn to the screen. onConfigurationChanged is called before redrawing the view for the new configuration and so I don't think you'll be able to get your new width until later.
#Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
final View view = findViewById(R.id.scrollview);
ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.v(TAG,
String.format("new width=%d; new height=%d", view.getWidth(),
view.getHeight()));
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
I had to use
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
configHome();
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
because the above answer was not working for me
Related
I set OnGlobalLayoutListener to detect softkeyboard event (hide or show softkeyboard) in fragment in onActivityCreated(). It was ok if i didn't go to fragment.
private void setGlobalLayoutListener() {
View rootView = getView().findViewById(R.id.root_view);
onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout(){
//do st here
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
Then I go to another fragment to do something, and comeback, i call setGlobalLayoutListener() again in onActivityCreated() but it doesn't run to onGlobalLayout() when i show or hide softkeyboard. Any suggestion? Thanks!
try this,
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > dpToPx(this, 200)) { // if more than 200 dp, it's probably a keyboard...
// ... do something here
}
}
});
Using a utility such as:
public static float dpToPx(Context context, float valueInDp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
Note: You must set this flag in Android Manifest android:windowSoftInputMode="adjustResize"
otherwise this solution will not work.
Hope it works for you.
I have multiple FrameLayout inside a ScrollView. In one of the FrameLayout I'm loading a PreferenceFragment. Now I need to set the height of the FrameLayout to the height of the PreferenceFragment to avoid nested scrolling (the PrefrenceFragment is scrollable too).
How can I get the height of the PreferenceFragment?
I tried to override onCreateView() in the PreferenceFragment class, and call View.getHeight(), but its just returning 0.
Setting the FrameLayout to wrap_content, isn't working as well, its just taking the space of a single preference then...
edit:
I tried now to get the height by using addOnGlobalLayoutListener() but the returned value is just the displayed height, not the full height of the PreferenceFragment. So I'm just getting the height of one preference for wrap_content or e.g. 300 if I set the height of the FrameLayout to 300 in the xml. I need the full height of all preferences (visibile and not).
#Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
final View v = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
if (v != null) {
ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + v.getHeight() + " Width = " + v.getWidth());
ViewTreeObserver obs = v.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
}
return v;
}
You should wait for WindowFocusChange in your activity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
//LAYOUT FRAGMENT NOW
}
try this
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
height = parentLayout.getheight();// the root layout of the fragment
//TODO:
}
}, 200);
I have a custom View, in which I need to perform treatment that require that the size of the view is set (so onCreate doesn't works), but before displaying anything. So I wondered if there is a callback function tat I could override for that ?
Right now I'm using onLayout, but it seems to be called twice, so I'm assuming it's probably not the right use for this. I also tried doing it in onMeasure but it seems that getWidth() and getHeight still returns 0 in it.
Thkanks,
Robin.
Try to use OnGlobalLayoutListener inside onResume()
#Override
public void onResume(){
super.onResume();
ViewTreeObserver viewTreeObserver = yourView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
viewWidth = yourView.getWidth();
viewHeight = yourView.getHeight();
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
System.out.println(viewHeight + " on resume x" + yourView.getX());
}
});
}
}
You can use the callback OnSizeChanged.
use onSizeChanged,
#Override
protected void onSizeChanged(int width, int height, int oldwidth, int oldheight) {
super.onSizeChanged(width, height, oldwidth, oldheight);
// your code here.
}
Try those methods
1.
ViewTreeObserver vto2 = imageView.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new
OnGlobalLayoutListener() {
#Override
public void onGlobalLayout () {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = imageView.getHeight();
int width = imageView.getWidth();
textView.append("\n\n" + imageView.getHeight() + "," + imageView.getWidth());
}
}
);
2.
imageView.post(new Runnable() {
#Override
public void run () {
int h = imageView.getHeight();
int w = imageView.getWidth();
Log.i(TAG, "Height=" + h);
}
};
I have looked at the similar questions and tried their solutions, but it didn't work for me. I'm trying to read width of an imageView, but it is returning 0. Here is the code:
public class MainActivity extends Activity {
private ImageView image1;
float centerX,centerY;
private ImageView image2;
private boolean isFirstImage = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate);
image1 = (ImageView) findViewById(R.id.ImageView01);
image2 = (ImageView) findViewById(R.id.ImageView02);
image2.setVisibility(View.GONE);
if (isFirstImage) {
applyRotation(0, 90);
isFirstImage = !isFirstImage;
}
}
private void applyRotation(float start, float end) {
centerX=image1.getWidth()/2.0f;
centerY=image1.getHeight()/2.0f;
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end,centerX , centerY);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1,
image2));
if (isFirstImage)
{
image1.startAnimation(rotation);
} else {
image2.startAnimation(rotation);
}
}
}
Can anyone help me with this? Thanks
You need to use the onSizechanged() method.
This is called during layout when the size of this view has changed
You should use: image1.getLayoutParams().width;
You are either accessing the ImageViews width and height before it has been measured, or after you set its visibility to GONE.
image2.setVisibility(View.GONE);
Then getWidth() or getHeight() will return 0.,
You can also have a look at onWindowFocusChanged(), onSizeChanged() and onMeasure():
#Override
public void onWindowFocusChanged(boolean focus) {
super.onWindowFocusChanged(focus);
// get the imageviews width and height here
}
public class GETImageView extends ImageView {
public GETImageView (Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
The ImageView have not yet draw the image, so no width or height return.
You may use a subclass to get the imageview width.
Here is some link provide you another easy methods:
How to get the width and height of an android.widget.ImageView?
How to get the width and height of an Image View in android?
Android ImageView return getWidth, getHeight zero
Hope this help you.
you can use OnGlobalLayoutListener for the ImageView image1 so you can get exact width and height occupied by the view in the layout...as
image1 = (ImageView) findViewById(R.id.ImageView01);
ViewTreeObserver viewTreeObserver = image1.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (isFirstImage) {
applyRotation(0, 90);
isFirstImage = !isFirstImage;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
image1.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
else
image1.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
By the way, views don't get sized until the measurement steps have been taken. See this link:
getWidth() and getHeight() of View returns 0
Maybe you should call View.measure(int,int) first.
Since the width and height is not defined until this view is draw. You can call measure by hand to enforce this view to calculate it's size;
Please using a Handler and get height and width on this with delay time >= 10
This may be useful for use:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
getHeight();
}
private void getHeight() {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Height:" + mAnimationView.getMeasuredHeight(),
Toast.LENGTH_SHORT).show();
}
}, 10L);
}
I need to know ImageView width and height. Is there a way to measure it in fragment ? In standard Activity I use this :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
image.getWidth();
image.getHeight();
}
But where can I use image.getWidth(); and image.getHeight(); in fragment ?
Use the GlobalLayoutListener. You assign the listener to your ImageView in onCreateView() just like you do in that answer in the link. It also is more reliable than onWindowFocusChanged() for the main Activity so I recommend switching strategies.
EDIT
Example:
final View testView = findViewById(R.id.view_id);
ViewTreeObserver vto = testView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + testView.getHeight() + " Width = " + testView.getWidth());
ViewTreeObserver obs = testView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});