android - To Show a ViewGroup only during a certain orientation -
android - To Show a ViewGroup only during a certain orientation -
i build kind of news app there list of news. when tapped, detailed news should shown. 1. if phone in portrait, details shown in activity 2. if phone in landscape, details shown alongside news list in details layout fragment in same activity.
(there fragment of 'news list' , of 'details')
what cannot accomplish rid of space details fragment take - if in landscape mode - in portrait mode.
this because have mentioned 2 empty viewgroups container of fragments in activity layout file.
i don't want create separate layout each orientation.
is there way hide or lean out viewgroup if empty?
relevant codes:
main activity xml:
<linearlayout android:id="@+id/news_list_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.4" > </linearlayout> <linearlayout android:id="@+id/details_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.6" > </linearlayout> </linearlayout>
main activity class - adding fragments according orientation:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //get orientation first if(this.getresources().getconfiguration().orientation==1) { //portrait newslist_fragment newsfrag = new newslist_fragment(); //init fragments may used fragmentmanager fmanager = getfragmentmanager(); //prepare fragment manager fragmenttransaction ftransaction = fmanager.begintransaction(); // add together fragments using ftransaction , commit ftransaction.add(r.id.news_list_container, newsfrag); ftransaction.commit(); } else if (this.getresources().getconfiguration().orientation==2) { //landscape newslist_fragment newsfrag = new newslist_fragment(); //init fragments may used details_fragment detailsfrag = new details_fragment(); fragmentmanager fmanager = getfragmentmanager(); //prepare fragment manager fragmenttransaction ftransaction = fmanager.begintransaction(); // add together fragments using ftransaction , commit ftransaction.add(r.id.news_list_container, newsfrag); ftransaction.add(r.id.details_container, detailsfrag); ftransaction.commit(); }
thank in advance
in case, need set visibility of layout containing details fragment view.gone. view.gone implies layout invisible , not take space. view.invisible means view invisible space still taken.
linearlayout detailscontainer = (linearlayout) findviewbyid(r.id.details_container); int orientation = getresources().getconfiguration.orientation; if (orientation == 1) // means orientations portrait detailscontainer.setvisibility(view.gone);
android android-layout android-fragments android-ui
Comments
Post a Comment