Android Layouts Design 101

By pjain      Published Dec. 17, 2019, 7:30 a.m. in blog Programming   

Optimizing View Hierarchies

Layouts

ConstraintLayout is better

  1. use ConstraintLayout not linear or relative layouts!
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

ConstraintLayout

ConstraintLayout | Android Developers

KT ListView

https://www.raywenderlich.com/186976/android-listview-tutorial-2 * ConstraintLayout with <ListView

KT RecyclerView

antoniolg/diffutil-recyclerview-kotlin: An example on how to implement DiffUtil for RecyclerViews in Kotlin, and what to do to make code simpler.

EX dn>mypass/kotlin/1andr/tute ex yt

https://www.simplifiedcoding.net/ k...

data class User(val name: String, val address: String)
//static populat it! or how to do listOf(obj?..)
    users.add(User("Belal Khan", "Ranchi, India"))
    users.add(User("Belal Khan", "Ranchi, India"))
    users.add(User("Belal Khan", "Ranchi, India"))
    users.add(User("Belal Khan", "Ranchi, India"))
    users.add(User("Belal Khan", "Ranchi, India"))
    users.add(User("Belal Khan", "Ranchi, India"))

onCreate .. 
    rv.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
    val users = ArrayList&lt;User&gt;()  //todo&gt; .. 
    val adapter = CustomAdapter(users)
    recyclerView.adapter = adapter

class CustomAdapter(val userList: ArrayList&lt;User&gt;) : RecyclerView.Adapter&lt;CustomAdapter.ViewHolder&gt;() {
    override fun onBindViewHolder(p0: ViewHolder?, p1: Int) {
        val user: User = userList[p1]
        p0?.textViewName?.text = user.name
        p0?.textViewAddress?.text = user.address
    }
    override fun onCreateViewHolder(p0: ViewGroup?, p1: Int): ViewHolder {
        val v = LayoutInflater.from(p0?.context).inflate(R.layout.list_layout, p0, false)
        return ViewHolder(v)
    }
    override fun getItemCount(): Int {
        return userList.size
    }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textViewName = itemView.findViewById(R.id.textViewName) as TextView
        val textViewAddress = itemView.findViewById(R.id.textViewAddress) as TextView
    }
}

* Main activity xml
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;android.support.constraint.ConstraintLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"   tools:context="net.simplifiedcoding.kotlinrecyclerviewexample.MainActivity"&gt;
    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"  android:layout_height="0dp"
        tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp"
        android:layout_marginTop="8dp"  android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="8dp"  android:layout_marginEnd="8dp" /&gt;
&lt;/android.support.constraint.ConstraintLayout&gt;

* Row layout
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"&gt;
    &lt;android.support.v7.widget.CardView  android:layout_margin="5dp"
        android:layout_width="match_parent"  android:layout_height="match_parent"&gt;
        &lt;LinearLayout  android:orientation="vertical"  android:padding="10dp"
            android:layout_width="match_parent"  android:layout_height="wrap_content"&gt;
            &lt;TextView  android:id="@+id/textViewName" android:text="Belal Khan"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:layout_width="match_parent" android:layout_height="wrap_content" /&gt;
            &lt;TextView android:id="@+id/textViewAddress" android:text="Ranchi, India"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" 
                android:layout_width="match_parent" android:layout_height="wrap_content" /&gt;
        &lt;/LinearLayout&gt;
    &lt;/android.support.v7.widget.CardView&gt;
&lt;/LinearLayout&gt;

Add a Edit search filtering

https://www.simplifiedcoding.net/search-functionality-recyclerview/

&lt;cl 
    ____eeditTextSearch___
    recycler shows in above .. if empty reverts to full ..

onCreate ..
    adapter ... customAdapter set originally with ALL names

    //adding a TextChangedListener to call a method whenever there is some change on the EditText
    editTextSearch.addTextChangedListener(new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        @Override public void afterTextChanged(Editable editable) {
            //after the change calling the method and passing the search input
            filter(editable.toString());
        }
    });

private void filter(String text) { //new array list that will hold the filtered data ArrayList filterdNames = new ArrayList<>(); for (String s : names) { //looping through existing elements if (s.toLowerCase().contains(text.toLowerCase())) { //if the existing elements contains the search input filterdNames.add(s); //adding the element to filtered list } } adapter.filterList(filterdNames); //calling a method of the adapter class and passing the filtered list }

// in CusomAdapter add function to get notified .. 
public void filterList(ArrayList&lt;String&gt; filterdNames) {
    this.names = filterdNames;
    notifyDataSetChanged();
}

0 comments

There are no comments yet

Add new comment

Similar posts

Serverless Computing

Drawing, Canvas in Kotlin

Custom Views

Animations in Kotlin