Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a Imageview on the screen with a FrameLayout? (with java code)

I need to center a imageview on the screen, but programatically, without using XML layouts.

I am actually using a FrameLayout, but i dont know how to center it on the FrameLayout.

This is the code i have until now:

        FrameLayout fl = new FrameLayout(this.getApplicationContext());
    splash = new ImageView(getApplicationContext());
    splash.setImageResource(R.drawable.logo);
    splash.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    fl.addView(splash);
    fl.setBackgroundColor(Color.BLACK);
    setContentView(fl);
like image 715
NullPointerException Avatar asked Dec 12 '22 08:12

NullPointerException


1 Answers

I would think something like this would get it done for you:

    LayoutParams centerParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

    centerParams.gravity =  Gravity.CENTER;
    fl.setLayoutParams(centerParams);

check out this page for more info

like image 57
FoamyGuy Avatar answered Jan 04 '23 23:01

FoamyGuy