0

I see everywhere in my project code like this, when buttons are declared in background, instead of usual Button. Developer who did this - not working now. Why is it done like this? Are there some performance winning? Or what reasons? And how I can do the same effect with ConstraineLayout? Thanks.

enter image description here

<android.support.v7.widget.CardView
            android:id="@+id/confirm_phone_btn_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/confirm_phone_text_input_layout"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="22dp"
            android:layout_marginTop="32dp"
            android:background="@color/edit_info_btn_inactive"
            app:cardBackgroundColor="@color/blue"
            app:cardCornerRadius="3dp"
            app:cardElevation="2dp"
            app:cardPreventCornerOverlap="false"
            app:cardUseCompatPadding="false">

            <TextView
                android:id="@+id/confirm_phone_change_txt"
                android:layout_width="118dp"
                android:layout_height="36dp"
                android:layout_gravity="center_horizontal"
                android:background="@color/edit_info_btn_inactive"
                android:clickable="true"
                android:gravity="center"
                android:includeFontPadding="false"
                android:text="@string/change"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="14sp"
                android:textStyle="bold" />

        </android.support.v7.widget.CardView>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
yozhik
  • 4,644
  • 14
  • 65
  • 98
  • 4
    Firstly you shouldn't ask **us** why **they** did it. It was just their choice. I think that `CardView + TextView` works slower than `Button`. Probably it was easier for them to style view this way, but only they know for sure. – Vladyslav Matviienko Feb 12 '18 at 09:19
  • 3
    Tip of the day never ask a random developer to explain other developer's work it will be a disaster :P. Just like Vlad said a simple view would have worked fine instead of using cardView+ textview. and why you want to use constraintLayout? why not a simple view with a custom design. – Umair Feb 12 '18 at 09:24
  • You can also use a clickable TextView with a custom selector as a background. Less views, less pain :P – sunlover3 Feb 12 '18 at 09:24
  • 4
    Because he was angry with his manager and probably his intention was to slow down this app. Leave these things and only think about how you can improve these codes, does not matter why he did this. – Pankaj Kumar Feb 12 '18 at 09:31
  • @sunlover3 maby he wanted to make rounded corners? – yozhik Feb 12 '18 at 09:50
  • @yozhik What lets you think a Button or a TextView can't have rounded corners? – Phantômaxx Feb 12 '18 at 10:24
  • 1
    @yozhik You can do whatever you want. Look here - https://stackoverflow.com/a/7607130/5914345 – sunlover3 Feb 12 '18 at 10:42

1 Answers1

0

Add cardview in your activity xml file

 <androidx.cardview.widget.CardView
        android:id="@+id/cardview_btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="**onClickCardview**">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@drawable/image" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text test"/>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

add below code in your activity java file

public void onClickCardview(View view){
        //your code here
    }