TextView has different top margin on different Android versions.
I created a simple application with a TextView with a red background placed in a RelativeLayout.On Android API 28, the text is displayed without top margin, as I expect. On API 33, the text has top margin. How can I get rid of it and make the text look like API 28?
API 28
API 33
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN);
RelativeLayout mainLayout = findViewById(R.id.main);
TextView textView = new TextView(this);
mainLayout.addView(textView);
textView.setTypeface(Typeface.createFromAsset(getAssets(), "GameFont.ttf"));
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 50);
textView.setBackgroundColor(Color.RED);
textView.setText("Test");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>