Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set margin for a fragment programmatically?

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>
like image 691
Daniil Orekhov Avatar asked Jun 29 '16 13:06

Daniil Orekhov


1 Answers

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);
    }
}
like image 144
Gaston Flores Avatar answered Sep 27 '22 18:09

Gaston Flores