Cenk Gün
Cenk Gün

Reputation: 21

How can I show different layouts according to the situation in Android?

I set the response after I request an API. I want to display my error screen on the screen according to the status of the response. For example, I want to show this screen when there is a problem with internet connection.

internet connection error

layout_connection_error.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yemek_hata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_below="@+id/weekCalendar">

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="fitXY"
    android:src="@drawable/ic_signal_wifi_off"
    android:tint="@color/colorGreyLight"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:text="@string/baglanti_hata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:id="@+id/textView8"
 android:textAppearance="@android:style/TextAppearance.Material.Caption"
    android:textSize="20sp"
    android:textAlignment="center" />


 </LinearLayout>

Or I want to show this screen when an unidentified error is encountered.

exception

layout_unidentified_error.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yemek_hata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_below="@+id/weekCalendar">

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="fitXY"
    android:src="@drawable/ic_bug_report"
    android:tint="@color/colorGreyLight"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:text="@string/bilinmeyen_hata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:id="@+id/textView8"
    android:textAppearance="@android:style/TextAppearance.Material.Caption"
    android:textSize="20sp"
    android:textAlignment="center" />


   </LinearLayout>

how can I do that? Thank you very much for anyone who replies

Upvotes: 0

Views: 97

Answers (1)

matip
matip

Reputation: 890

Since they look the same you can just change TextView's text and ImageView's image in code.

textView.setText(R.string.bilinmeyen_hata);
imageView.setImageResource(R.drawable.ic_bug_report);

Upvotes: 2

Related Questions