Search code examples
androidandroid-layoutreact-nativewix-react-native-navigation

Align image to to center LinearLayout from code


I am using react-native-navigation from wix in my react-native application, So for showing the splash screen in my app I have to write the android layout in code in this class.

Since I have not worked in android I am unable to align image and a some text to center in LinearLayout.

enter image description here

This is how the mockup looks like.

Here is the code I have written so far.

import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;

 public class MainActivity extends SplashActivity {
    @Override
    public LinearLayout createSplashLayout() {
        LinearLayout view = new LinearLayout(this);
        FrameLayout frame = new FrameLayout(this);
        ImageView imageView = new ImageView(this);
        ImageView logoView = new ImageView(this);
        logoView.setImageResource(R.mipmap.icon);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
        logoView.setLayoutParams(layoutParams);
        logoView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.drawable.welcome);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        frame.addView(imageView);
        frame.addView(logoView);
        view.addView(frame);
        return view;
    }
 }

If someone could help me figure out how to achieve this. Thanks.


Solution

  • Try like this:

    LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        linearLayout.setLayoutParams(llParams);
        RelativeLayout view = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout
                .LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        view.setLayoutParams(params);
    
        ImageView bgView = new ImageView(this);
        RelativeLayout.LayoutParams bgParam =
                new RelativeLayout.LayoutParams(RelativeLayout
                        .LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        bgView.setLayoutParams(bgParam);
        bgView.setImageResource(R.drawable.welcome);
        bgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.addView(bgView);
    
        ImageView logoView = new ImageView(this);
        RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(RelativeLayout
                .LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        logoView.setLayoutParams(layoutParams);
        logoView.setImageResource(R.mipmap.icon);
        view.addView(logoView);
        linearLayout.addView(view);
        return linearLayout;