0

I am currently trying to create cash register based app with 5x5 grid to display all the menu. I used GridLayout with CardView to display them. 1 CardView with linearLayout+ Imageview + TextView resulted in total 4 views, so 5x5 cardview made my xml end up with 100 views...

I tried to use include tags for the linearlayout+ imageview+textview inside the cardview, which brought the views down to 50 views only. But the problem now is: I'd like to change the text and image src inside the include tags, otherwise I cant display all the menu images. Is this even possible?

Any suggestion for my problems?

            <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"
            app:cardPreventCornerOverlap="true"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp">
            <LinearLayout android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:weightSum="100"
                android:orientation="vertical"
                >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:layout_gravity="center"
                    android:layout_weight="70"
                    android:src="@drawable/noimage"
                    android:contentDescription="@string/logo" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:textColor="#A9A9A9"
                    android:layout_weight="30"
                    android:textAlignment="center"
                    android:autoSizeTextType="uniform"
                    android:text="@string/logo"/>
            </LinearLayout>


        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"
            app:cardPreventCornerOverlap="true"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp">
            <include layout="@layout/menu_cardview"/>

        </android.support.v7.widget.CardView>
d.popov
  • 4,175
  • 1
  • 36
  • 47
leth
  • 1
  • See my answer for https://stackoverflow.com/questions/61477058/how-to-set-a-layout-width-percentage-of-one-third-on-constraintlayout/61478206#61478206 – MMG May 05 '20 at 09:21
  • You can use Recyclerview with GridLayoutManager for this type of view. To create a single item you can also use Constraint Layout instead of nested LinearLayouts. – Rehan Sarwar May 05 '20 at 09:23
  • linked answer is a really bad answer, and also not applicable for this situation. – Vucko May 05 '20 at 09:35
  • @RehanSarwar that might actually works for me, i will try it for now – leth May 05 '20 at 09:38

1 Answers1

0

I think it's currently not possible but what you can do to reduce/optimize your xml code is First Option:

use only TextViews in GridLayout and for images above them don't use separate ImageViews use this in TextViews android:drawabkeTop="drawable/image

Second Option :

Second option is which you can also use along with first option. Use Styles to style your layout Example :

 <style name="gridImageStyle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:src">@drwable/image</item>
</style>

<style name="gridTextStyle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">20sp</item>
    <item name="android:textColor">20sp</item>
</style>

and use it in your TextView and ImageView like this

              <TextView
                    style="@style/gridTextStyle"
                    android:text="EnterTextHere"/>

              <ImageView
                    style="@style/gridImageStyle"
                    android:src="Enter Image Source here"/>

But, I would recommend to you to use android:drawableTop:"ImageSource" in TextView instead of ImageView

gagangamer
  • 14
  • 1
  • 6