0

There was no option for centering a AdMob ad so i added the last bit of code, is this code correct? My compiler throws no error and the ad does not move to the center. I cannot find any information about relative layout in reference to centering.

  if(x == 0) {
                    adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}else if(x == -1) {
                    adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}else if(x == 2) {
                    adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_CENTER);
}
jjwallace
  • 168
  • 2
  • 16

1 Answers1

0

Centering is handled by the layout config for your View and depends upon it's parent ViewGroup. You really needed to provide a lot more info than you have, including the config for AdView and it's containing ViewGroup.

But generally speaking you probably want to specify a gravity of center_horizontal

  <AdView ..
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
  />
William
  • 20,150
  • 8
  • 49
  • 91