Search code examples
androidandroid-custom-view

Initialize custom view elements


I started Android recently and I'm creating a dialogue with a personalized carousel.

I have created an xml that has the two elements that will be seen in each view of the carouser (ImageView and TextView), but when I create my View object I do not know how to access each element to initialize it.

XML custom view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView_carousel_presentacion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/com_facebook_button_icon_blue" />

    <TextView
        android:id="@+id/textView_carouser_presentacion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Dialog class

public class CarouserInicialDialogo {

    private Context contexto;

    private Dialog dialogo;
    private Button btn;

    private CarouselView carousel;
    private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};

    public CarouserInicialDialogo(final Context contexto) {
        dialogo = new Dialog(contexto);
        dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogo.setCancelable(true);
        dialogo.setContentView(R.layout.dialogo_carousel_inicial);

        this.contexto = contexto;

        carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
        carousel.setPageCount(imagenes.length);
        carousel.setViewListener(viewListener);

        dialogo.show();
    }

    ViewListener viewListener = new ViewListener() {
        @Override
        public View setViewForPosition(int position) {
            LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);

            //set view attributes here
            //
            //
            //

            return customView;
        }
    };
}

Solution

  • You can access views like this

    ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
    TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);
    

    Once you add this code, your class look like this:

    public class CarouserInicialDialogo {
    
    private Context contexto;
    
    private Dialog dialogo;
    private Button btn;
    
    private CarouselView carousel;
    private int[] imagenes = {R.drawable.default_profile, R.drawable.felicitades, R.drawable.logo_app};
    
    public CarouserInicialDialogo(final Context contexto) {
        dialogo = new Dialog(contexto);
        dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogo.setCancelable(true);
        dialogo.setContentView(R.layout.dialogo_carousel_inicial);
    
        this.contexto = contexto;
    
        carousel = (CarouselView) dialogo.findViewById(R.id.carousel_view_presenacion);
        carousel.setPageCount(imagenes.length);
        carousel.setViewListener(viewListener);
    
        dialogo.show();
    }
    
    ViewListener viewListener = new ViewListener() {
        @Override
        public View setViewForPosition(int position) {
            LayoutInflater inflater = (LayoutInflater) contexto.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View customView = inflater.inflate(R.layout.custom_view_carouser_presentacion, null);
    
                ImageView imageView = (ImageView) customView.findViewById(R.id.imageView_carousel_presentacion);
                TextView textView = (TextView) customView.findViewById(R.id.textview_carousel_presentacion);
    
                //Views are initialized with layout.
    
            return customView;
        }
    };
    }