So, I need to set up left margin for a fragment container (which is a FrameLayout inside a RelativeLayout) such that it scales the way I need it to. Thus, I want to do this in java (although the rest of view-related stuff is done in xml), but I'm not sure how to approach it. Should I create a new generalized class for my fragments? Or should I do it in the activity class? If so, does it have to be in a specific place?
Here is my activity class:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null)
return;
AuthFragment firstFragment = new AuthFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
}
}
And activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/navy">
<com.kupol24.app.view.MyImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"/>
<FrameLayout
android:id="@+id/container_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/fragment_margin"
android:layout_centerVertical="true">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</RelativeLayout>
Maybe this will be be useful, try to set margins to the Framelayout (or Relative):
final FrameLayout frameLayout = (FrameLayout)findViewById(R.id.fragment_container);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) frameLayout.getLayoutParams();
//left, top, right, bottom
params.setMargins(10, 10, 10, 10);
frameLayout.setLayoutParams(params);
(Edited) I do not know that to do your code but I would do so:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fragment_container);
if ((frameLayout != null) && (savedInstanceState == null)) {
final AuthFragment firstFragment = new AuthFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) frameLayout.getLayoutParams();
//left, top, right, bottom
params.setMargins(10, 0, 0, 0); //setting margin left to 10px
frameLayout.setLayoutParams(params);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With