How can I get the height / width of any kind (Button, TextView, RelativeLayout) in a fragment, I try something like this
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
<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 this, I found the Width / Height Button in Activity using this method.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
relMain = (RelativeLayout) findViewById(R.id.layoutMain);
width = relMain.getMeasuredWidth();
height = relMain.getMeasuredHeight();
width = relMain.getWidth();
height = relMain.getHeight();
}
but onWindowFocusChanged () is not available in android fragment?
source
share