Android - add fragment with custom dimensions -
currently, adding fragment using following code.
private void addfragment(fragment fragment, string fragmentname) { log.i("adder", "adding fragment " + fragmentname); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.fragment_container, fragment, fragmentname); fragmenttransaction.commit(); }
following code resizing added fragment.
private boolean readjustfragment(fragment fragmenttobeadjusted, fragmentcoordinates fragmentcoordinates) { if (fragmenttobeadjusted != null) { view view = fragmenttobeadjusted.getview(); relativelayout.layoutparams params = new relativelayout.layoutparams(fragmentcoordinates.getwidth(), fragmentcoordinates.getheight()); params.leftmargin = fragmentcoordinates.getx0(); params.bottommargin = fragmentcoordinates.gety0(); view.setlayoutparams(params); view.requestlayout(); return true; } return false; }
but instead of adding , resizing, how add fragment custom dimensions (height, width , margins) ?
you can pass desired width height , margin fragment , assign value while inflating view
for example if using frame layout container fragments pass values new instance method
/** * * @param widthindp * @param heightindp * @param leftmargindp * @param topmargindp * @param rightmargindp * @param bottommargindp * @param fragindex * @return */ public static placeholderfragment newinstance(int widthindp, int heightindp, int leftmargindp, int topmargindp, int rightmargindp, int bottommargindp, int fragindex) { placeholderfragment resizablefragment = new placeholderfragment(); bundle args = new bundle(); // test purpose args.putint(index, fragindex); // set width height dynamically args.putint(width, widthindp); args.putint(height, heightindp); // setmargin args.putint(left_margin, leftmargindp); args.putint(right_margin, rightmargindp); args.putint(top_margin, topmargindp); args.putint(bottom_margin, bottommargindp); resizablefragment.setarguments(args); return resizablefragment; }
and apply them in oncreateview
method. point note here if using frame layout container fragments use framelayout
param else exception.
framelayout.layoutparams params = (framelayout.layoutparams) rootview .getlayoutparams(); // set width height params.height = dptopx(getarguments().getint(height)); params.width = dptopx(getarguments().getint(width)); // substitute // parameters left,top, right, bottom params.setmargins(dptopx(getarguments().getint(left_margin)), dptopx(getarguments().getint(top_margin)), dptopx(getarguments().getint(right_margin)), dptopx(getarguments().getint(bottom_margin))); rootview.setlayoutparams(params);
output :-
orange background frame layout
container , other resized fragments
custom margins in same container. can play around layoutparam.gravity
property make them go left right, top, bottom , center, bottom- center, top center , on
complete code used in sample
package com.example.fragment; import android.annotation.targetapi; import android.graphics.color; import android.os.build; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.util.displaymetrics; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.framelayout; import android.widget.textview; public class mainactivity extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager() .begintransaction() .add(r.id.container, placeholderfragment.newinstance(400, 400, 50, 50, 50, 50, 1)).commit(); getsupportfragmentmanager() .begintransaction() .add(r.id.container, placeholderfragment.newinstance(300, 300, 30, 30, 30, 30, 2)).commit(); getsupportfragmentmanager() .begintransaction() .add(r.id.container, placeholderfragment.newinstance(200, 200, 20, 20, 20, 20, 3)).commit(); getsupportfragmentmanager() .begintransaction() .add(r.id.container, placeholderfragment.newinstance(100, 100, 10, 10, 10, 10, 4)).commit(); } } /** * placeholder fragment containing simple view. */ @targetapi(build.version_codes.honeycomb) public static class placeholderfragment extends fragment { private static final string height = "height"; private static final string width = "width"; private static final string left_margin = "left"; private static final string right_margin = "right"; private static final string top_margin = "top"; private static final string bottom_margin = "bottom"; // test data private static final string index = "fragnumber"; /** * * @param widthindp * @param heightindp * @param leftmargindp * @param topmargindp * @param rightmargindp * @param bottommargindp * @param fragindex * @return */ public static placeholderfragment newinstance(int widthindp, int heightindp, int leftmargindp, int topmargindp, int rightmargindp, int bottommargindp, int fragindex) { placeholderfragment resizablefragment = new placeholderfragment(); bundle args = new bundle(); // test purpose args.putint(index, fragindex); // set width height dynamically args.putint(width, widthindp); args.putint(height, heightindp); // setmargin args.putint(left_margin, leftmargindp); args.putint(right_margin, rightmargindp); args.putint(top_margin, topmargindp); args.putint(bottom_margin, bottommargindp); resizablefragment.setarguments(args); return resizablefragment; } public placeholderfragment() { } public int dptopx(int dp) { displaymetrics displaymetrics = getactivity().getresources() .getdisplaymetrics(); int px = math.round(dp * (displaymetrics.xdpi / displaymetrics.density_default)); return px; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); framelayout.layoutparams params = (framelayout.layoutparams) rootview .getlayoutparams(); // set width height params.height = dptopx(getarguments().getint(height)); params.width = dptopx(getarguments().getint(width)); // substitute // parameters left,top, right, bottom params.setmargins(dptopx(getarguments().getint(left_margin)), dptopx(getarguments().getint(top_margin)), dptopx(getarguments().getint(right_margin)), dptopx(getarguments().getint(bottom_margin))); rootview.setlayoutparams(params); // test purpose switch (getarguments().getint(index)) { case 1: rootview.setbackgroundcolor(color.magenta); ((textview) rootview.findviewbyid(r.id.frag_index)) .settext("fragment # " + getarguments().getint(index)); break; case 2: rootview.setbackgroundcolor(color.green); ((textview) rootview.findviewbyid(r.id.frag_index)) .settext("fragment # " + getarguments().getint(index)); break; case 3: rootview.setbackgroundcolor(color.blue); ((textview) rootview.findviewbyid(r.id.frag_index)) .settext("fragment # " + getarguments().getint(index)); break; default: rootview.setbackgroundcolor(color.white); ((textview) rootview.findviewbyid(r.id.frag_index)) .settext("fragment # " + getarguments().getint(index)); break; } return rootview; } } }
so technically doable didn't understand why trying way ?
Comments
Post a Comment