1

EDITED

The examples in google searches and the android documentation are basic, and my question is more complex.

What I do not know, is how to put multiple components together and turn it into a single component. CardView + LinearLayout + TextView + ImageView = Custom View.

If I have a basic example, of cardview with at least one button and a space for external content. It would be enough for me to figure out how to do the rest. This within the cardview extension class, because I also do not know how the extension will understand where to create the components

EDITED 2

Example

QUESTION

I was trying to create a method of using Cardview with an expandable button, and I was able to do this in XML. But I wanted to be able to create a library so I could reuse it in other cases.

I'll show you the source code of what I did.

Layout XML

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/cwlltitulobtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="#### TITLE OF BUTTON ####"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <ImageView
                android:id="@+id/imgarrowdown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:scaleType="fitEnd"
                android:visibility="visible"
                app:srcCompat="@drawable/ic_keyboard_arrow_down_black_24dp" />

            <ImageView
                android:id="@+id/imgarrowup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:scaleType="fitEnd"
                android:visibility="gone"
                app:srcCompat="@drawable/ic_keyboard_arrow_up_black_24dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/cwllconteudo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:orientation="vertical"
            android:visibility="gone">

         <!-- ########################################
              ######## CARDVIEW CONTENT HERE #########
              ########################################-->

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

Java code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.cwlltitulobtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CVOpenCloseBTN();
            }
        });


    }

    public void CVOpenCloseBTN(){
        ImageView arrowdown = (ImageView) findViewById(R.id.cvimgarrowdown);
        ImageView arrowup = (ImageView) findViewById(R.id.cvimgarrowup);
        LinearLayout pagecontent = (LinearLayout) findViewById(R.id.cwllconteudo);

        if (conteudopage .getVisibility() == View.GONE) {
            // it's collapsed - expand it
            pagecontent.setVisibility(View.VISIBLE);
            arrowdown.setVisibility(View.GONE);
            arrowup.setVisibility(View.VISIBLE);
        } else {
            // it's expanded - collapse it
            pagecontent.setVisibility(View.GONE);
            arrowdown.setVisibility(View.VISIBLE);
            arrowup.setVisibility(View.GONE);
        }
    }
}

My idea is that I can transform all this part of the xml and java code to be used in this way and faster, than to have to always repeat again:

<br.com.samantabiblioteca.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        app:titulo="#### TITLE OF BUTTON ####">

             <!-- ########################################
                  ######### CARDVIEW CONTENT HERE ########
                  ########################################-->

</br.com.samantabiblioteca.CardView>

Is it possible to do this?

Samanta Silva
  • 267
  • 3
  • 17
  • https://developer.android.com/training/custom-views/create-view.html – OneCricketeer Mar 15 '18 at 05:23
  • create your attribute, this will help https://stackoverflow.com/questions/7608464/android-custom-ui-with-custom-attributes – KuLdip PaTel Mar 15 '18 at 05:24
  • @cricket_007 You did not understand my doubt. Create a view of a single component, I know. An extension of a cardview, I know. What I do not know, is how to put multiple components together and turn it into a single component. `CardView + LinearLayout + TextView + ImageView = Custom View`. The basics, I understood, I did google searches. My question is about something more complex. You see? – Samanta Silva Mar 15 '18 at 05:35
  • @KuLdipPaTel You did not understand my doubt. Create a view of a single component, I know. An extension of a cardview, I know. What I do not know, is how to put multiple components together and turn it into a single component. `CardView + LinearLayout + TextView + ImageView = Custom View`. The basics, I understood, I did google searches. My question is about something more complex. You see? – Samanta Silva Mar 15 '18 at 05:35
  • 1
    You extend a CardView. You can add fields of the other elements. You can build a layout in Java rather than XML. I think I understand – OneCricketeer Mar 15 '18 at 05:36
  • While this section of the documentation is vague with no examples, it talks about compound views https://developer.android.com/guide/topics/ui/custom-components.html#compound – OneCricketeer Mar 15 '18 at 05:38
  • Yeah @cricket_007, but how to do this? It is precisely because I have no better examples that I have come to ask this question. I can not imagine how to do that. – Samanta Silva Mar 15 '18 at 05:46
  • @cricket_007 If I have a basic example, cadview with at least one button and a space for external content. It would be enough for me to figure out how to do the rest. This within the cardview extension class, because I also do not know how the extension will understand where to create the components. – Samanta Silva Mar 15 '18 at 05:49
  • I personally have no experience with this, but I know of let's say MPAndroidChart library that uses complex views for graphs and such . You can see a Chart is a ViewGroup of the data view, and a legend view. https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java – OneCricketeer Mar 15 '18 at 05:52
  • I think you have to use custom view that is implementing card view. den write your all logic there & reuse it every where. – Sandeep Sankla Mar 15 '18 at 05:53
  • @cricket_007 This example you sent is about drawing on a canvas, it is ideal for games. It's way beyond what I need, because in that way I would have to create all the components again. I think should have a way to join the native components of the android without having to redraw them. Thanks for the help, this is still not what I'm looking for. – Samanta Silva Mar 15 '18 at 06:08
  • Okay, so it's a Canvas, my bad. I'm getting plenty of search results for compound views.... https://medium.com/@Sserra90/android-writing-a-compound-view-1eacbf1957fc – OneCricketeer Mar 15 '18 at 06:10
  • It's more or less this @SandeepSankla I did not understand what you meant by writing all logic to reuse them. My idea would be to create a new component by joining several other components into one. This is what I do not know how to do. – Samanta Silva Mar 15 '18 at 06:10
  • @cricket_007 About this link you sent me. And the LinearLayout extension it created is different from what I'm looking for as it does not have the option of putting the external content. And also he use of an xml layout, I think there is no how to do this in module library. I've done a lot of research, and I have not really found anything explaining this. I'm starting to read the source codes of native components to understand better, but it's still very complicated for me. I did not imagine it would be so difficult. – Samanta Silva Mar 15 '18 at 06:22
  • 1
    @cricket_007 Reading the source codes of the native components of android, I discovered that the way to do what I want, is through an extension of FrameLayout. I think I'm almost finding a solution. – Samanta Silva Mar 15 '18 at 06:31
  • A `CardView` is a `FrameLayout`, which is a `ViewGroup`. You can extend whatever makes the most sense for your purpose. In that link I sent, if you implement that LinearLayout, you could place it within a `CardView`, for example – OneCricketeer Mar 15 '18 at 16:42
  • @cricket_007 Thanks for the help my friend, but still is not what I look for. The link you sent me can put the fixed components inside the layout, but it does not have support for external content. I made a gif to help understand what I'm trying to do. https://i.stack.imgur.com/gZnly.gif Where it is in blue, it will be the fixed components (Textview + Imageview), and where it is green, it will be the external content that will depend on the programmer to choose what it should contain. – Samanta Silva Mar 15 '18 at 18:09
  • @cricket_007 I'm giving it up already. Thank you anyway. Sorry for bothering. – Samanta Silva Mar 15 '18 at 18:11

0 Answers0