Reputation: 115
Is it possible to set margins programmatically to a Tag Fragment in Android?.
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_marginBottom="?attr/actionBarSize"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav_graph" />
Thanks in advance
Upvotes: 0
Views: 761
Reputation: 31
It would be better if you can use FrameLayout instead of Fragment Tag. Here is an example of that.
getSupportFragmentManager()
.beginTransaction()
.add/replace(R.Id.your_frame_layout_id, new YourFragment())
.commit();
Then you can get the layout params for the parent layout of your FrameLayout. For example, if your frame is inside RelativeLayout then get params for Relative layout.
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams)fl.getLayoutParams();
rlp.setMargins(0,0,0,0); //Set your margins here
fl.setLayoutParams(rlp);
I wrote the answer in terms of java. If you are looking for kotlin then the approach might be similar.
Upvotes: 1