Sunday, 21 May 2017

Android: Layout as background of view

In this post I will show you how to set your custom layout as the background of your item of your listview or gridview or any other view that contains long list of data, through your adapter.
This is very simple and can be done as follows:

1. Make a file custom_placeholder.xml of your custom layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="6dp"
        android:background="@drawable/shape_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Downloading..."
            android:textAlignment="center"
            android:textSize="14sp" />

    </RelativeLayout>

</LinearLayout>


2. Now in the getView method of your adapter just replace your code with below code:

public View getView(final int position, View convertView, ViewGroup parent) {
        CustomViewHolder mViewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.item_gridview_assets, parent, false);
            mViewHolder = new CustomViewHolder(convertView);  // set your layout (convertView) as your viewHolder
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (CustomViewHolder) convertView.getTag();
        }
          //Now check the condition whether your image is available or not  
        String value = imageList.get(position);
        if (!value.equalsIgnoreCase("")) {
             // case when image is available  
           mViewHolder.imgAssets.setBackgroundResource(R.drawable.sample_img);
        } else {
             // case when image is not available  
           // This will inflate your custom view that you created above
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.custom_placeholder, parent, false);
        }
        return convertView;
    }

That's all.
Share and comment if any issues.



2 comments:

  1. SuperBear APK is a handy app for streaming movies, shows, and live content on mobile devices. Its smooth interface makes it easy to find and enjoy your favorite programs. To get started and use all features efficiently, followhow to superbear apk app tutorial for step-by-step guidance. The app also provides fast streaming, organized categories, and personalized recommendations. Overall, SuperBear APK is ideal for anyone looking for a simple and reliable entertainment experience.

    ReplyDelete
  2. The car parking apk touch controls tutorial is designed to help players master the game’s intuitive touch-based navigation system. With this tutorial, users can easily understand how to steer, accelerate, brake, and reverse their vehicles using simple on-screen controls. The tutorial covers essential maneuvers such as precise parking, sharp turns, and smooth handling on different terrains, ensuring a realistic driving experience. Step-by-step guidance and interactive demonstrations make it easy for beginners to get comfortable with the controls, while advanced tips help experienced players improve their parking accuracy and driving skills. Whether you’re new to Car Parking APK or looking to refine your technique, this tutorial makes controlling your car simple and enjoyable.

    ReplyDelete