Nexussim Lements
Nexussim Lements

Reputation: 787

Android Linear Layout adapting height to elements

I have the following Horizontal Linear Layout inside a Vertical Linear Layout :

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:background="@color/White"
    android:orientation="vertical">

    <TextView
        android:id="@+id/pokemonTeambuilderTitleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top|start"
        android:paddingStart="10dp"
        android:paddingTop="10dp"
        android:paddingEnd="10dp"
        android:paddingBottom="5dp"
        android:text="@string/placeholder"
        android:textColor="@color/Black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/pokemonTeambuilderSpritesLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

    </LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

And programmatically I set the elements this way in my adapter :

 for (Pokemon pokemon : pokemonTeam.getPokemonList()) {
        String pokemonNickname = pokemon.getNickname();
        String pokemonName = pokemon.getName();
        ImageView tvPokemonSprite = new ImageView(mContext);
        int color = PokemonUtils.getDominantColorFromPokemon(pokemon.get_id(),mContext);
        tvPokemonSprite.setImageResource(PokemonUtils.getPokemonSugimoriImageById(pokemon.get_id(), mContext));
        tvPokemonSprite.setScaleType(ImageView.ScaleType.FIT_CENTER);
        PokemonUtils.setResourceAndBorderBackgroundColorToElementFromColor(tvPokemonSprite,R.drawable.circle_shape,color,PokemonUtils.lighterColor(color, DARK_FACTOR));
        TableLayout.LayoutParams layout = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        tvPokemonSprite.setLayoutParams(layout);
        holder.teamSpritesLinearLayout.addView(tvPokemonSprite);
    }

However , if I rotate my phone , the elements don't fit completly in the Linear Layout , the bottom and the top is half cutted , also if the horizontal linear layout only have 1 Pokemon , instead of adapting to 1 slot it expands the WHOLE view (as you can see with charizard)

enter image description here

Upvotes: 0

Views: 58

Answers (1)

mahdi sadeghi
mahdi sadeghi

Reputation: 68

Add below code to imageview:

tvPokemonSprite.setAdjustViewBounds(true)

i hope work for you.

Upvotes: 1

Related Questions