How to center the image on the screen using FrameLayout? (with Java code)

I need to center the image on the screen, but programmatically, without using XML layouts.

I actually use FrameLayout, but I don’t know how to focus it on FrameLayout.

This is the code that I still have:

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); 
+4
source share
3 answers

I think this should do it:

 fl.setForegroundGravity(Gravity.CENTER); 
+4
source

I would think something like this would do for you:

  LayoutParams centerParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); centerParams.gravity = Gravity.CENTER; fl.setLayoutParams(centerParams); 

read this page for more information

+5
source

Image has no gravity. It has layout_gravity.

+2
source

All Articles