Anand Jain Anand Jain - 19 days ago 10
Android Question

How to Change ListView to GridView on Button Click in android

I want to change my view listview to gridview and back gridview to listview on a button click.Here my product.xml:-

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

<LinearLayout
android:id="@+id/main_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/product_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:visibility="gone"/>

<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#166CED"
android:textAppearance="?android:attr/textAppearanceLarge"/>

<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
android:textColor="#D64530"/>

<TextView
android:id="@+id/product_discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="discout price"
android:textColor="#D64530"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/product_href"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:visibility="gone"/>


<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout> </LinearLayout></LinearLayout>


Here My product_category.xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="shoppingmazza.android.catalyst.com.dynamiclayout.ProductCategory">

<!-- Header aligned to top -->

<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FILTER"
android:id="@+id/textView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SORT"
android:id="@+id/textView2"
android:layout_marginLeft="257dp"
android:layout_marginStart="257dp" />

</LinearLayout>

</RelativeLayout>


<LinearLayout
android:layout_below="@+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ListView
android:id="@+id/list_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="@layout/product"/>

</LinearLayout>
</RelativeLayout>


Here My ProductCategory.java:-

public class ProductCategory extends AppCompatActivity {
private ListView listView;
ProductAdapter adapter;
ArrayList<shoppingmazza.android.catalyst.com.dynamiclayout.Product> productsList;
private int len;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_category);


new Product().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/products&category=369&key=test123$");

listView = (ListView)findViewById(R.id.list_product);
productsList = new ArrayList<shoppingmazza.android.catalyst.com.dynamiclayout.Product>();
adapter = new ProductAdapter(getApplicationContext(),R.layout.product,productsList);

listView.setAdapter(adapter);

}
public class Product extends AsyncTask<String,Void,Boolean>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(ProductCategory.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}

@Override
protected Boolean doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);

int status = response.getStatusLine().getStatusCode();
if(status==200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);

JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("products");

for(int i=0;i<jsonArray.length();i++){
len=jsonArray.length();
JSONObject jsonObject1 = jsonArray.getJSONObject(i);

shoppingmazza.android.catalyst.com.dynamiclayout.Product product = new shoppingmazza.android.catalyst.com.dynamiclayout.Product();


product.setId(jsonObject1.getString("id"));
product.setName(jsonObject1.getString("name"));
product.setPrice(jsonObject1.getString("pirce"));
product.setDiscountprice(jsonObject1.getString("discountprice"));
product.setHref(jsonObject1.getString("href"));
product.setThumb(jsonObject1.getString("thumb"));
product.setRating(jsonObject1.getString("rating"));

productsList.add(product);
}
return true;
}

}catch (IOException |JSONException e){
Log.e("Error :",e.getMessage());
}
return null;
}

@Override
protected void onPostExecute(Boolean aVoid) {
dialog.dismiss();
adapter.notifyDataSetChanged();
if(!aVoid){
Toast.makeText(ProductCategory.this, "Data is not Parsed", Toast.LENGTH_LONG).show();
}
else{
ProductAdapter adapter = new ProductAdapter(getApplicationContext(),R.layout.product,productsList);
listView.setAdapter(adapter);
}

}
}


}

Here My ProductAdapter.java:-

public class ProductAdapter extends ArrayAdapter<Product> {

ArrayList<Product> productsList;
LayoutInflater vi;
int Resource;
ViewHolder holder;

public ProductAdapter(Context context, int resource, ArrayList<Product> object) {
super(context, resource, object);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
productsList = object;
Resource = resource;
// this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);

holder.imageView=(ImageView)v.findViewById(R.id.image_product);
holder.tvId = (TextView)v.findViewById(R.id.product_id);
holder.tvName = (TextView)v.findViewById(R.id.product_name);
holder.tvPrice = (TextView)v.findViewById(R.id.product_price);
holder.tvDiscount = (TextView)v.findViewById(R.id.product_discount);
holder.tvHref = (TextView)v.findViewById(R.id.product_href);
holder.tvRating = (RatingBar)v.findViewById(R.id.rating);
v.setTag(holder);
}
else{
holder = (ViewHolder)v.getTag();
}
new DownlaoadImageTask(holder.imageView).execute(productsList.get(position).getThumb());
holder.tvId.setText(productsList.get(position).getId());
holder.tvName.setText(productsList.get(position).getName());
holder.tvPrice.setText(productsList.get(position).getPrice());
holder.tvDiscount.setText(productsList.get(position).getDiscountprice());
holder.tvHref.setText(productsList.get(position).getHref());
holder.tvRating.setNumStars(Integer.parseInt(productsList.get(position).getRating()));
return v;
}
static class ViewHolder{
public TextView tvId;
public TextView tvName;
public TextView tvPrice;
public TextView tvDiscount;
public TextView tvHref;
public ImageView imageView;
public RatingBar tvRating;
}
private class DownlaoadImageTask extends AsyncTask<String,Void,Bitmap>{
ImageView bmImage;

public DownlaoadImageTask(ImageView bmImage){
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls){
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try{
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (IOException e){
Log.e("Error", e.getMessage());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result){
bmImage.setImageBitmap(result);
}
}

}


I am new in android developing Please Help me thanks in advance!

Answer

You can add both ListView and GridView and set GridView android:visibility="gone"

 <LinearLayout
  android:layout_below="@+id/header_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <ListView
      android:id="@+id/list_product"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:listitem="@layout/product"/>

  <GridView
      android:id="@+id/list_productGV"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>


 </LinearLayout>

on button click change visibilty of ListView to gone and GridView to visible and vice versa

Comments