Pankaj Negi Pankaj Negi -4 years ago 84
Android Question

setContentview() change layout in onConfigurationChanged() but not set the Data in android

I have created two layouts for portrait and landscape mode, My scenario is I want to change the Text of TextView based on the orientation changes. here is my code:-

AndroidManifest.xml:

<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


activity_main.xml (portrait)

<LinearLayout 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="com.demo.textdemo.MainActivity">
<TextView
android:id="@+id/textViewHello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20dp" />




activity_main.xml (landscape):

<LinearLayout 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="com.demo.textdemo.MainActivity">

<TextView
android:id="@+id/textViewHello"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20dp"
/>




MainActivity:

public class MainActivity extends AppCompatActivity {

TextView textViewHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textViewHello = (TextView)findViewById(R.id.textViewHello);
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
textViewHello.setText("ORIENTATION LANDSCAPE");
}else {

textViewHello.setText("ORIENTATION PORTRAIT");
}

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);

textViewHello.setText("ORIENTATION LANDSCAPE");

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
textViewHello.setText("ORIENTATION PORTRAIT");
}
}


}

For the First Time, it shows the change in TextView according to the Orientation but when I change the Orientation it shows "Hello World!" , which define in activity_main.xml, I don't know why ??

Answer Source

You need to find id for each widget . when orientation changes .

eg. Textview t= (Textview)findViewById(R.id.text_id);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download