2

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.

1 Answers1

2

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();
}
Heraldo
  • 141
  • 6
  • Good answer, except for the comment "this behavior is not recomended unless you have a valid reason to do that". Not recommended by who? And what is "a valid reason"? – Eugene Kartoyev Jul 11 '18 at 18:38
  • I'd rather recommend doing this all the time, unless you really have all good reasons to recreate the activities on each orientation change, and submit to all the consequences related to that. – Eugene Kartoyev Jul 11 '18 at 18:42
  • 1
    @EugeneKartoyev configChanges is widely known as "last resort" method: "...It is not recommended to handle configuration changes yourself due to the hidden complexity of handling the configuration changes..." https://developer.android.com/guide/topics/resources/runtime-changes. There are ways to retain info, fragment, bundle, etc... Destroying and recreating activities is the normal behavior, and we must preserve this as long as we can. – Heraldo Jul 11 '18 at 20:21
  • @Heraldo, so I did end up implementing the bottom section of your answer. And I think I prefer that as the solution over configChanges, so thanks for that! Although I did end up tweaking the code just a bit, seperating .setImageResource to rid of an error – Slavic the Slavic Jul 12 '18 at 15:28