tools:title=”LayoutPreview” Android Development

tools:title=”LayoutPreview” Android Development

As every Android developer, I also started building my layouts by hard-coding values into them. Later on, I have realized that I should probably extract them into resources. Next step was not to have values in layouts if they are going to be changed from the code (always keep the state of your view in code). But at that point, my layout preview got broken. The layouts were full of TextViewswithout texts, ImageViews without images, etc. And then I discovered the power of tools:. Fast forward one year and I had layouts full of tools:text="Test text", tools:text="Jake Wharton", tools:src="@drawable/ic_random_res" â€¦ you get the point. Is there a better way to do this, you may ask?

Actually, there is. And it’s pretty easy. There are predefined sample resources that you can use to make your layout preview nice and clean. By using these, we can go from this:

<TextView
    android:id="@+id/text_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:drawableLeft="@drawable/ic_random_res"
    tools:text="Jake Wharton"/>
 
  

And why is this better? If your TextView is inside a repeatable element like, for example, an item inside RecyclerView, and you set up your preview correctly, then your TextView will have a different text in every item. Speaking of RecyclerView, let’s make a deep dive into making a preview of your RecyclerView as sleek as possible, using this and some other tricks.

It all starts with a normal and rather ugly RecyclerView in your layout XML file. In a preview, it looks like this:

And why is this better? If your TextView is inside a repeatable element like, for example, an item inside RecyclerView, and you set up your preview correctly, then your TextView will have a different text in every item. Speaking of RecyclerView, let’s make a deep dive into making a preview of your RecyclerView as sleek as possible, using this and some other tricks.

It all starts with a normal and rather ugly RecyclerView in your layout XML file. In a preview, it looks like this:

<FrameLayout 
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:menu="menu_test">

    <RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/item_test"
        tools:layoutManager="GridLayoutManager"
        tools:spanCount="2"/>
        
</FrameLayout>
 
  

It looks much better, almost like a real application. The only problem is that our application in horizontal orientation has GridLayout with two spans. Can we do something in that regard? Of course, we can. By adding tools:layoutManager=”GridLayoutManager” and tools:spanCount=”2", we are getting exactly what we want.

Well, almost. Our Activity has some menu items in the Toolbar, and if we could display these as well, it would be perfect. Accomplishing this isn’t really that difficult. Simply adding tools:menu=”menu_test” to the root element of your layout does the trick. It’s important to know that for this to work you need to have predefined menu item @menu/menu_test.xml, and your Toolbarneeds to be defined in a theme and not added in layout’s XML. The final result looks like this:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_normal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="@tools:sample/lorem"/>

        <TextView
            android:id="@+id/text_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="@tools:sample/lorem/random"
            tools:lines="2"/>
            
    </LinearLayout>
</android.support.v7.widget.CardView>
 
  

As you can see, tools: gives you great power to prototype and experiment with your layout without writing a single line of Java or Kotlin code or adding unnecessary states to your view’s XML. If you start using it now, you will have a much more pleasant time developing UI on your new project, and it will be a breeze to modify it later on.

要查看或添加评论,请登录

Sanjoy Kumar的更多文章

  • Ways to Troubleshoot of Electronics Projects

    Ways to Troubleshoot of Electronics Projects

    Stuck with a project that isn't working? Try these troubleshooting techniques to help efficiently fix your projects!…

  • Flutter Layout Cheat Sheet

    Flutter Layout Cheat Sheet

    Do you need simple layout samples for Flutter? I present you my set of Flutter layout code snippets. I will keep it…

  • Flutter ListView and ScrollView

    Flutter ListView and ScrollView

    Exploring the types of ListView We’ll start with looking at the types of ListViews and later look at the other features…

  • Virtual GPS Engine for simulation in Proteus VSM.

    Virtual GPS Engine for simulation in Proteus VSM.

    This post describes a method for reading a text file into Proteus VSM. The text file is comprised of unprocessed GPS…

  • Flutter : Build beautiful native apps

    Flutter : Build beautiful native apps

    Today, as part of Mobile World Congress 2018, we are excited to announce the first beta release of Flutter. Flutter is…

  • Solar Inverter for Home

    Solar Inverter for Home

    Introduction to Solar Inverter: We see many people using Solar inverters these days which proves that its necessity has…

  • Metal Detectors and its Advantages?—?Post by Indexel Engineering

    Metal Detectors and its Advantages?—?Post by Indexel Engineering

    Metal Detectors as the name suggests are using electronic instruments that help to detect metal presence immediately by…

  • Design and Implementation of Motor Speed control and Temperature sensing unit using PIC Controller

    Design and Implementation of Motor Speed control and Temperature sensing unit using PIC Controller

    This Article aims to design and implementation of DC motor speed, temperature sensing of material. This project is…

社区洞察

其他会员也浏览了