Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic resize fragment android

I want to change the width of a fragment I'm using in my MaiActivity? But I can't find its LayoutParams for changing its default width:

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int widthFfragment = 300;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthTot = size.x;
        heightTot = size.y;

        findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
    }
}

This is my fragment's code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/f1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#330000"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Fragment1.java

public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        //---Inflate the layout for this fragment---
        return inflater.inflate( R.layout.fragment1, container, false);
    }
}

Many thanks.

like image 665
bill23 Avatar asked May 22 '12 09:05

bill23


2 Answers

Your right, the Fragment does not have LayoutParams because it is a conceptual container, you either need to adjust the layout params of the inflated view in the fragment, or to the container you attach the fragment to.

public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ViewGroup view = (ViewGroup) inflater.inflate( R.layout.fragment1, container, false);

    // TODO Adjust layout params of inflated view here

    return view;
}

The example assumes you are inflating a ViewGroup.

Hope that helps.

like image 30
Ian Warwick Avatar answered Oct 04 '22 20:10

Ian Warwick


You can do it by working with the LayoutParams instance of the View obtained from Fragment.getView(). My sample code:

private void resizeFragment(Fragment f, int newWidth, int newHeight) {
    if (f != null) {
        View view = f.getView();
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(newWidth, newHeight);
        view.setLayoutParams(p);
        view.requestLayout();       


    }
}

And may be used as follows:

resizeFragment(myFragment, 200, 500);
resizeFragment(otherFragment, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
like image 122
Miguel Avatar answered Oct 04 '22 18:10

Miguel