getHeight() of PreferenceFragment - android

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);

Related

Cannot set OnGlobalLayoutListener again in fragment after coming bac from another fragment Android

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.

How to get Width/Height of Button in Fragment

How can i Get Height/Width of Any View (Button , TextView , RelativeLayout) in fragment, i try something like that
public static class FirstDemoFragment extends Fragment {
int width,height;
private Button button;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
button = (Button) view.findViewById(R.id.fragment_demo_button);
width = button.getWidth();
height = button.getHeight();
System.out.println("==== View Width : " + width);
System.out.println("==== View height : " + height);
width = button.getMeasuredWidth();
height = button.getMeasuredHeight();
System.out.println("==== View Width : " + width);
System.out.println("==== View height : " + height);
return view;
}
my layout.xml file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/does_really_work_with_fragments"
android:id="#+id/fragment_demo_button"
android:layout_centerInParent="true" />
i have a little research on that, I have found Width/Height of Button in Activity using this method.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
relMain = (RelativeLayout) findViewById(R.id.layoutMain);
width = relMain.getMeasuredWidth();
height = relMain.getMeasuredHeight();
width = relMain.getWidth();
height = relMain.getHeight();
//System.out.println("=== Width : " + width);
//System.out.println("=== height : " + height);
}
but onWindowFocusChanged() is not available in android Fragment ?
In Fragment try below code in Fragment's onCreateView method:
button = (Button) view.findViewById(R.id.fragment_demo_button);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
button.getViewTreeObserver().removeOnPreDrawListener(this);
Log.d("", "Height: " + button.getMeasuredHeight()+ " Width: " + button.getMeasuredWidth());
return true;
}
});
In Fragment use below code in Fragment's onCreateView method
ViewTreeObserver mviewtreeobs = button.getViewTreeObserver();
mviewtreeobs .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = button.getWidth();
height = button.getHeight();
}
});
You could add a layout listener to your button like so:
button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int width = button.getWidth();
if(width > 0){
int height = button.getHeight();
// do something with your width and height
....
// remove layout listener
button.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}

get width, height from Layout

I need to get width and height of Linear layout, however I do not know when the Relative Layout is ready to provide me these two attributes.
but it only returns 0.
my screen
please check my code, i have no idea.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
LinearLayout container = (LinearLayout)findViewById(R.id.container);
int width = container.getWidth();
int height = container.getWidth();
Log.d("MainActivity onCreate", "width #" + width + ", height #" + height);
}
}
and activity_main.xml layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:background="#FFFFFFAA"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
how do i fix this?
Whenever you try to get dimensions on onResume() or on onCreate() method, it always returns 0. So you should use ViewTreeObserver
ViewTreeObserver vto = container.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
container.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = container.getMeasuredHeight();
finalWidth = container.getMeasuredWidth();
return true;
}
});
Also you can customize your View which will extends with LinearLayout and in onMeasure() method you can get width and height.
First of all replace
int height = container.getWidth();
by
int height = container.getHeight();
container.getHeight() will return 0 (as well as container.getWidth()) if it's not drawed yet.
Solution would be to get the height after your view is layouted:
container.post(new Runnable(){
public void run(){
int height = container.getHeight();
}
});
You can try and mesaure it first. But make sure you do so only after every other view as been added beacuse your LinearLayout is depended on other views.
LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final LinearLayout container = (LinearLayout)findViewById(R.id.container);
container.post(new Runnable()
{
#Override
public void run()
{
Log.i("TEST", " Layout width : "+ container.getWidth() +" Layout height "+ container.getHeight()) ;
}
});
First, you are calling getWidth twice - for width and height.
You can try it with measureSpecs, like this:
int matchParentMeasureSpecWidth = View.MeasureSpec.makeMeasureSpec(((View) container.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
int matchParentMeasureSpecHeight = View.MeasureSpec.makeMeasureSpec((View) container.getParent()).getHeight(), View.MeasureSpec.EXACTLY);
v.measure(matchParentMeasureSpecWidth, matchParentMeasureSpecHeight);
int height = container.getMeasuredHeight();

Wrong View Height/Width value on Orientation change?

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

Measure view in fragment

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);
}
});

Categories

Resources