0

I have a RelativeLayout displaying an app name with its icon below. I want to be able from the java side to switch icon and text positions, so the app name is displayed below the icon without having to inflate a new view. How can this be possible?

here is my code for that

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:paddingBottom="27dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/appIcon"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_margin="2dp"
        android:layout_below="@+id/appName"
        android:layout_centerHorizontal="true"
        android:contentDescription="icon"
        android:src="#80000000" />

    <TextView
        android:id="@+id/appName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:fontFamily="sans-serif-condensed"
        android:gravity="center"
        android:singleLine="true"
        android:text="name"
        android:textSize="13dp"/>

</RelativeLayout>
Mo Dev
  • 475
  • 6
  • 17

1 Answers1

1

You can change the layout_below rule programmatically, like this:

TextView tvAppName = findviewById(R.id.appName)

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.BELOW, R.id.appIcon);
    tvAppName .setLayoutParams(params);
Shermano
  • 1,100
  • 8
  • 31