Skip to content

RanaRanvijaySingh/AdjustableLayout

Repository files navigation

AdjustableLayout

By Extending Linear Layout and overriding addView() function , created such a layout where any view can be added horizontally. When view does not fit in the screen , it automatically occupies space from next line auto adjusting it's height.

Screen shots

screenshot from 2015-07-22 14 28 02 screenshot from 2015-07-22 14 54 01 screenshot from 2015-07-22 15 11 46 screenshot from 2015-07-22 15 14 55 screenshot from 2015-07-22 15 25 37

Instructions

Using Adjustable layout.
In your activity_main.xml

<rana.com.adjustablelayout.AdjustableLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Create your own layout eg. view_chip_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/chips"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="3dp">

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="name"/>

    <ImageView
        android:id="@+id/ivRemove"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_clear_mtrl_alpha" />
</LinearLayout>

Drawable file chips.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="5dp"/>
</shape>

###In your java code

Now there are two ways you can add view. ####1. Adding one view at a time. ####2. Adding multiple view in a loop.

###1. Adding one view at a time.

 adjustableLayout = (AdjustableLayout)findViewById(R.id.container);
 addChipsView();
 /**
     * Using view_chip_text layout
     */
    private void addChipsView() {
        String name = etEnterName.getText().toString();
        if (!TextUtils.isEmpty(name)){
            final View newView = LayoutInflater.from(this).inflate(R.layout.view_chip_text,null);
            TextView tvName = (TextView)newView.findViewById(R.id.tvName);
            ImageView ivRemove = (ImageView)newView.findViewById(R.id.ivRemove);
            ivRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    adjustableLayout.removeView(newView);
                }
            });
            tvName.setText(name);
            adjustableLayout.addView(newView);
        }else {
            Toast.makeText(this,"Enter some text",Toast.LENGTH_SHORT).show();
        }
    }

###2. Adding multiple view in a loop.
You need to use these two steps

  1. To add multiple view
    adjustableLayout.addingMultipleView(newView);
  2. Call invalidate function.
    adjustableLayout.invalidateView();


Example

     * Function to add view in a loop
     */
    private void addViewInALoop() {
        List<String> listString = new ArrayList<String>();
        listString.add("this");
        listString.add("is");
        listString.add("how");listString.add("it");listString.add("is");listString.add("done");listString.add("this");
        listString.add("issue");listString.add("is ");listString.add("that i am not ");listString.add("able to give");
        listString.add("layout margin in xml");listString.add("rest");
        listString.add("");listString.add("is good");
        for (int i=0;i<15;i++){
            final View newView = LayoutInflater.from(this).inflate(R.layout.view_chip_text,null);
            TextView tvName = (TextView)newView.findViewById(R.id.tvName);
            ImageView ivRemove = (ImageView)newView.findViewById(R.id.ivRemove);
            ivRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    adjustableLayout.removeView(newView);
                }
            });
            tvName.setText(listString.get(i));
            adjustableLayout.addingMultipleView(newView);
        }
        adjustableLayout.invalidateView();
    }```

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published