Manifest에 configChanges 속성을 설정한후

MainActivity에서 onConfigurationChanged() method를 overriding해준다

//manifests안에 AndroidManifest.xml파일에서 activity tag안에서 configChanges속석을 설정한다

AndroidManifest.xml

<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<!--android:screenOrientation = "landscape"
가로상태로 고정되어 변하지 않는다-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

activity_main.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="try to change the view direction"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChanged(Configuration configuration){
super.onConfigurationChanged(configuration);

if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
displayToast("landscape");
}else if(configuration.orientation == Configuration.ORIENTATION_PORTRAIT){
displayToast("portrait");
}
}
public void displayToast(String message){
Toast.makeText(context: this, message, Toast.LENGTH_LONG).show();
}
}

'android studio' 카테고리의 다른 글

custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12

+ Recent posts