I wanted to display the ImageView image on the full screen (with the same aspect ratio) and place the button directly below, but ... If you rotate it horizontally, the button will be hidden, and if you change the layout according to the horizontal screen, the position of the button will shift when you rotate it vertically. I wanted to have only one xml for the layout, so I decided to dynamically resize the ImageView in the source.
--Layout uses ConstraintLayout
--ImageView settings:
--Size (in portrait orientation):
- height: wrap_content
- width: 0dp(match constraint)
--Size (in landscape orientation):
- height: 0dp(match constraint)
- width: wrap_content
--Specify ʻandroid: adjustViewBounds = trueto maintain the aspect ratio of the drawing --Specify ʻapp: layout_constraintVertical_chainStyle = "packed"
to attach buttons
Activity
--Added ʻandroid: configChanges =" orientation | screenSize " to Activity in the manifest. Make it possible to detect vertical and horizontal rotation. --When the vertical and horizontal orientation changes, ʻonConfigurationChanged
is called. In that, the layout of ImageView was updated.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="8dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed"
app:srcCompat="@drawable/ic_launcher_background" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//UI acquisition
imageView = findViewById(R.id.imageView);
button = findViewById(R.id.button);
//For the time being, get the orientation at startup
int orientation = getResources().getConfiguration().orientation;
UpdateLayout(orientation); //Layout update
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation; //Get orientation
UpdateLayout(orientation); //Layout update
}
/**
*Layout update
* @param orientation orientation
*/
protected void UpdateLayout(int orientation){
//ImageView LayoutParam acquisition
LayoutParams param = imageView.getLayoutParams();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//In portrait orientation
Log.d("UpdateLayout", "Vertical orientation");
param.height = LayoutParams.WRAP_CONTENT;
param.width = 0;
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
//When facing sideways
Log.d("UpdateLayout", "Sideways");
param.height = 0;
param.width = LayoutParams.WRAP_CONTENT;
}
imageView.setLayoutParams(param);
}
}
Determine whether it is landscape or portrait on Android Display the full screen width on Android while maintaining the aspect ratio of the image https://techblog.zozo.com/entry/constraint_layout
Recommended Posts