You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
10 Hrs
50 Min
48 Seg

Creando Custom Views

31/32
Resources

How to optimize the use of views by creating a custom view in Android?

Creating efficient and manageable applications is essential in mobile development. Duplication of code not only fragments our application; it also complicates maintenance and future adaptation. To solve this, it is possible to create "custom views" that allow us to reuse code and make changes in a simple and global way.

What is the basic structure of a custom view?

A custom view is created from the View class, which is fundamental to render elements on screen. This custom view could include, as in our example, a TextView and a CircularProgressbar.

  1. Inherit from View: Starts a new class inheriting from View.
  2. Constructors: Implements several constructors to ensure compatibility with different initializations:
    • Constructor with Context.
    • Constructor with Context and AttributeSet.
    • Constructor with Context, AttributeSet and int defStyle.
  3. Initialization: It implements a private method as initView() to configure the view each time a constructor is initialized.
public class PresentasView extends View { public PresentasView(Context context) { super(context); initView(); }
 public PresentasView(Context context, AttributeSet attrs) { super(context, attrs); initView();
    }
 public PresentasView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); }
 private void initView() { // Initialization of components } }}

How to inflate and add the layout in the custom view?

The layout elements must be inflated and added to the appropriate container, using FrameLayout which allows you to add views easily. By following these steps you can configure the view:

  • Inflate layout: Use LayoutInflater to inflate the XML of the layout.
  • Assignment: Use addView() to add views to FrameLayout.
private void initView() { LayoutInflater inflater = LayoutInflater.from(getContext()); View view view = inflater.inflate(R.layout.my_custom_view, this, false); addView(view);}

How to use the custom view in XML?

To include the custom view in your XML layouts, simply declare the new custom view as you would a standard widget. Define sizes and positions as needed.

<your.package.PresentasView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" />

What are the benefits and challenges of implementing a custom view?

Benefits:

  • Code reuse: the same logic is used in multiple views.
  • Easy maintenance: A single change is reflected in all instances.
  • Consistency: Ensures uniformity in design and functionality across the app.

Challenges:

  • Learning curve: Understanding and configuring it initially can be complicated for beginners.
  • Compatibility: Requires ensuring compatibility with different versions and devices.

Finally, the development and use of custom views not only make the application more efficient, but are also a powerful tool to ensure that any changes to your components are propagated accurately and effectively in your application. It's your turn to experiment and implement this technique!

Contributions 3

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

CustomViews este se crea heredando desde View
Recordatorio: View es el elemento principal para renderizar dentro de una pantalla

Como puedo hacer que se vea reflejado en los dos activitys el progres ?, hablo del reto

En los observadores no se debe eliminar , si no llamar al customVIew

homePresenter.getPercentageLiveData().observe(viewLifecycleOwner, androidx.lifecycle.Observer {

            percentageValue -> circularProgress.percentageText.text = percentageValue
        })