Custom List View and Grid View Dynamic



       

In this example we want to show you how to create custom Listview and GridView in runtime using Button Selection.  

package example.com.uidemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;


public class ListDemo extends AppCompatActivity {
    LinearLayout
    LinearLayout ll;

  

    @Override
  
    protected void onCreate(@Nullable Bundle savedInstanceState) {
      
        super.onCreate(savedInstanceState);
        setContentView(R.layout.
        setContentView(R.layout.listdemo);
      
        ll = findViewById(R.id.lin);
    }

  
    }
    public void listDemo(View v) {
      
        ll.removeAllViews();
        ListView lv =
        ListView lv = new ListView(this);
        lv.setLayoutParams(
        lv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        lv.setAdapter(
        lv.setAdapter(new SpnAdapter(this, getResources().getStringArray(R.array.names),"list"));
      
        ll.addView(lv);
    }

  
    }
    public void gridDemo(View v) {
      
        ll.removeAllViews();
        GridView gv =
        GridView gv = new GridView(this);
        gv.setLayoutParams(
        gv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        gv.setAdapter(
        gv.setAdapter(new SpnAdapter(this, getResources().getStringArray(R.array.names),"grid"));
        gv.setNumColumns(
        gv.setNumColumns(2);
      
        ll.addView(gv);
    }
}
    } }        package example.com.uidemo;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class SpnAdapter extends BaseAdapter implements View.OnClickListener{
    Context
    Context context;
    String
    String names[];
    String
    String opt;

    SpnAdapter(Context context, String names[], String opt) {
      
    SpnAdapter(Context context, String names[], String opt) {         this.opt = opt;
      
        this.names = names;
      
        this.context = context;

    }

  
    }     @Override
  
    public int getCount() {
      
        return names.length;
    }

  
    }     @Override
  
    public Object getItem(int i) {
      
        return names[i];
    }

  
    }     @Override
  
    public long getItemId(int i) {
      
        return i;
    }

  
    }     @Override
  
    public View getView(int i, View view, ViewGroup viewGroup) {

        View v = LayoutInflater.from(
        View v = LayoutInflater.from(context)
                .inflate(R.layout.
                .inflate(R.layout.custom_layout, null);

        LinearLayout ll = v.findViewById(R.id.
        LinearLayout ll = v.findViewById(R.id.lincust);

      
        if (opt.equalsIgnoreCase("list")) {
            ll.setOrientation(LinearLayout.
            ll.setOrientation(LinearLayout.HORIZONTAL);
        }
        } else {
            ll.setOrientation(LinearLayout.
            ll.setOrientation(LinearLayout.VERTICAL);
        }
        TextView t =
        }         TextView t = new TextView(context);
        t.setText(
        t.setText(names[i]);
        t.setTextColor(Color.parseColor(
        t.setTextColor(Color.parseColor("#ffffff"));
        t.setLayoutParams(
        t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
        ToggleButton tb =
        ToggleButton tb = new ToggleButton(context);
        tb.setLayoutParams(
        tb.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        tb.setTag(i);
        tb.setOnClickListener(
        tb.setTag(i);         tb.setOnClickListener(this);
        ll.addView(t);
        ll.addView(tb);
      
        ll.addView(t);         ll.addView(tb);         return v;
    }

  
    }     @Override
  
    public void onClick(View view) {
        Integer i=(Integer) view.getTag();
        Toast.makeText(
        Integer i=(Integer) view.getTag();         Toast.makeText(context, names[i], Toast.LENGTH_SHORT).show();
    }
}
    } }    xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  
    android:layout_width="match_parent"
  
    android:layout_height="match_parent"

  
    android:orientation="vertical">

    <
    <LinearLayout
      
        android:id="@+id/lin"
      
        android:layout_width="match_parent"
      
        android:layout_height="wrap_content"
      
        android:layout_weight="1"
      
        android:orientation="vertical" />

    <
    <LinearLayout
      
        android:layout_width="match_parent"
      
        android:layout_height="wrap_content"
      
        android:orientation="horizontal">

        <
        <Button
          
            android:layout_width="wrap_content"
          
            android:layout_height="wrap_content"
          
            android:text="ListView"
          
            android:onClick="listDemo"
          
            android:layout_weight="1"/>
        <
        <Button
          
            android:layout_width="wrap_content"
          
            android:layout_height="wrap_content"
          
            android:text="GridView"
          
            android:onClick="gridDemo"
          
            android:layout_weight="1"/>

    </
    </LinearLayout>


</
</LinearLayout>
    xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
  
    android:layout_height="wrap_content"
  
    android:orientation="horizontal"
  
    android:background="@color/colorAccent"
  
    android:id="@+id/lincust"
  
    xmlns:android="http://schemas.android.com/apk/res/android">


</
</LinearLayout>
 <resources>
    <
    <string name="app_name">UiDemo</string>
    <
    <string-array name="names">
        <
        <item>Select Version</item>
        <
        <item>Alpha</item>
        <
        <item>Beta</item>
        <
        <item>Cupcake</item>
        <
        <item>Donut</item>
        <
        <item>Eclair</item>
        <
        <item>Froyo</item>
        <
        <item>Gingerbread</item>
        <
        <item>Honeycomb</item>
        <
        <item>Ice Cream Sandwich</item>
        <
        <item>Jelly Bean</item>
        <
        <item>KitKat</item>
        <
        <item>Lollipop</item>
        <
        <item>Marshmallow</item>
        <
        <item>Nougat</item>
        <
        <item>Oreo</item>
        <
        <item>Pie</item>


    </
    </string-array>
</
</resources>
  xml version="1.0" encoding="utf-8"?>
<resources>
    <
    <color name="colorPrimary">#900C3F</color>
    <
    <color name="colorPrimaryDark">#581845</color>
    <
    <color name="colorAccent">#C70039</color>
</
</resources>
  <resources>

  
   
  
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      
       
      
        <item name="colorPrimary">@color/colorPrimary</item>
        <
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <
        <item name="colorAccent">@color/colorAccent</item>
    </
    </style>

</
</resources>
 

No comments: