Slavic the Slavic
Slavic the Slavic

Reputation: 411

onConfigurationChanged isn't working properly

So all I'm trying to do is check orientation and change an image based on that. The problem is that neither image change, nor the toast message is working. Not sure why.. I have this same code working elsewhere.

Java:

public class Test extends AppCompatActivity {

private ArrayList<String> mNames = new ArrayList<>();

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

    getText();

}

private void getText() {

    mNames.add("Menu");
    mNames.add("Definitions");
    mNames.add("Steps");
    mNames.add("Examples");
    mNames.add("Related");
    mNames.add("Videos");

    initRecyclerView();
}

private void initRecyclerView() {

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(layoutManager);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames);
    recyclerView.setAdapter(adapter);

}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        ImageView layout = findViewById(R.id.background);
        layout.setBackgroundResource(R.drawable.background_l);

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        ImageView layout = findViewById(R.id.background);
        layout.setBackgroundResource(R.drawable.background_p);
    }
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY" />

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/recyclerView"
        android:paddingTop="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/open_sans_bold"
                android:text="@string/definition"
                android:textColor="@color/backgroundBlue"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView2"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef1"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView3"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans_bold"
                android:text="@string/uses"
                android:textColor="@color/backgroundBlue"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView4"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef2"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView5"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef3"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

        </RelativeLayout>
    </ScrollView>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/backgroundBlue"
        android:fontFamily="@font/open_sans_bold"
        android:text="@string/fiveS"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="29sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="0dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/button2"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" />
</RelativeLayout>

Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ToolTables.1"></activity>
    <activity android:name=".ToolTables.2"
        android:windowSoftInputMode="adjustPan">
    </activity>
    <activity android:name=".ToolTables.3"></activity>
    <activity android:name=".ToolTables.4"></activity>
    <activity android:name=".ToolTables.5"></activity>
    <activity
        android:name=".Test"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">

    </activity>

    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />
</application>

Edit: I've added the rest of the code. Including the XML that couples with the Java. It's not real complicated, mostly text fields and a recyclerView.

Upvotes: 2

Views: 429

Answers (1)

Heraldo
Heraldo

Reputation: 141

onConfigurationChanged() callback only will be called if we specify that we will manage this changes manually on the manifest.xml inside activity tag:

android:configChanges="orientation|screenSize|keyboardHidden"

This indicates that the activity will not be recreated when the device rotates, this behavior is not recomended unless you have a valid reason to do that, for example youtube API recomends this to avoid recreating and reinitializing YoutubePlayerFragment.

Caution: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications. https://developer.android.com/guide/topics/resources/runtime-changes

If its not neccesary and only you want to have a diferent image on each orientation you can:

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

    boolean isLandscape = this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    ((ImageView)findViewById(R.id.background)).setImageResource(isLandscape ? R.drawable.background_l : R.drawable.background_p);

    getText();
}

Upvotes: 2

Related Questions