A ListView containing only images - android

how can i make a listView that only contains images and nothing else. i am new to the listView and all that adapter stuff. i succeed in making a listView containing text only but unable to do this with images.
this is what i have done so far.
My MainActivity Class
public class MainActivity extends AppCompatActivity {
ListView listView;
ImageView imageView;
int[] imageIds = {R.drawable.chrysanthemum, R.drawable.desert, R.drawable.hydrangeas, R.drawable.jellyfish};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, R.layout.imageview, imageIds);
listView.setAdapter(adapter);
}
}
Main XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.danishrizvi.myapplication.MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
ImageView Layout File
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
the error arises in this line ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, R.layout.imageview, imageIds); that is Unable to Resolve Constructor.
so far what i have learned about my mistake by searching on the internet is that i might need a custom adapter or something similar to get my job done but was unable to know that how to implement and how that works.

layout_adapter.xml
<?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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img_adapter"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ImageAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> arrayList;
public ImageAdapter(Context mContext, ArrayList<String> arrayList) {
this.mContext = mContext;
this.arrayList = arrayList;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_adapter, parent, false);
ImageView imgAdapter = (ImageView) convertView.findViewById(R.id.img_adapter);
Glide.with(mContext).load(arrayList.get(position)).into(imgAdapter);
return convertView;
}
}
Use this in activity like this,
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("image url");
arrayList.add("image url");
arrayList.add("image url");
ImageAdapter imageAdapter = new ImageAdapter(this, arrayList);
listView.setAdapter(imageAdapter);

sample code is here:
https://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Ii is showing row with image and text. So you can modified it according to your requirement.

Related

Android: How to identify which button is clicked from custom list view

I am creating a custom list view with:
activity_main.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:id="#+id/activity_display_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.anandgupta.login.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/myListView" />
</RelativeLayout>
and list_row.xml containing only a TextView and a button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_row_selector"
android:padding="8dp">
<!-- Movie Title -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/title"
android:textStyle="bold" />
<!-- Buy Ticket -->
<Button
android:id="#+id/buyTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Buy"
android:onClick="buyTicket" //Button calling buyTicket...Not working
/>
and MainActivity.java:
package com.anandgupta.login;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
private List<Movie> movieList = new ArrayList<Movie>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assume that MovieAdapter and Movie class is already created and its working fine.
MovieAdapter movieAdapter = new MovieAdapter(this,movieList);
listView = (ListView)findViewById(R.id.myListView);
listView.setAdapter(movieAdapter);
}
public void buyTicket() //Not working
{
Toast.makeText(MainActivity.this,"Ticket Booked",Toast.LENGTH_LONG).show();
}
}
and MovieAdapter.java:
package com.anandgupta.login;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MovieAdapter extends BaseAdapter {
private List<Movie> movieList;
private Activity activity;
private LayoutInflater layoutInflater;
public MovieAdapter(Activity activity, List<Movie> movieList) {
this.movieList = movieList;
this.activity = activity;
}
#Override
public Object getItem(int position) {
return movieList.get(position);
}
#Override
public int getCount() {
return movieList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
return convertView;
}
}
and Movie.java:
package com.anandgupta.login;
public class Movie {
private String title;
Movie()
{
}
public void setMovieTitle(String title)
{
this.title = title;
}
public void getMovieTitle(String title)
{
return title;
}
}
I am able to see the custom list but while trying to call the function buyTicket with the click of button my app get closed.
Any help will be appreciated.
Edit: The problem was that the method is not passed a view:
public void buyTicket(View view) { ....... }
This solve the above problem but now how to uniquely know which button from the list is clicked.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Button buyTicketButton = (Button)convertView.findViewById(R.id.buyTicket);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
buyTicketButton.setOnClickListener(new OnClickListener(){
//..... your code goes here
});
return convertView;
}
You can craete an Button Click event in the Adapter's getView() method.
Button btnBookTicket = (Button)convertView.findViewById(R.id.buyTicket);
btnBookTicket.setOnClickListener(new View.OnClickListener()
{
((MainACtivity)activity).buyMovieTicket();
});

listView with just images

I am trying to create a listView with just images. Please this is not a stupid question so any help is appreciated. I think my code is correct, but when I run it, it just shows the contentView but empty. It does not show the images I tell it to show.I have created the java file and then two xml files, one with the listView which is the contentView for the java file, and the other for one single row of the listView.
My java file:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
my craft.xml
<?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:background="#drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
My single_row.xml file
<?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:background="#drawable/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/one"
android:id="#+id/imageView23" />
Any help is appreciated!
You should change your ListView height to:
<ListView
.
.
.
android:layout_height="match_parent"/>
I see, you have not overriden getCount() in your adapter:
So your listview dosen't know that there are items.
You should add this to your adapter:
#Override
public int getCount() {
return images.length;
}
Ty to use this super statement
Super(c , R.layout.single_row,imgs);

How to display arraylist in gridview in android

I want to display Arraylist items in Gridview. My Arraylist is like this :
1 Hello Hello
2 Hello Hello
If I bind it to a gridview control like this :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, event_list);
gridview.setAdapter(adapter);
event_List is my Arraylist. Through this approach I get a complete Arraylist row or record in a cell of gridview . I want to display each item like "Hello" of Arraylist in each cell of gridview. Like 1 in one cell , "Hello" in another cell and so on.
Thanks in advance
Seems You need to use BaseAdapter, because default ArrayAdapter is not able to accomplish dividing of ArrayList element into number of elements.
So, it would look like the following:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView grid = (GridView) findViewById(R.id.gridView);
final ArrayList<String> items = new ArrayList<String>();
items.add("1 , Hello11 , Hello12");
items.add("2 , Hello21 , Hello22");
grid.setAdapter(new GridAdapter(items));
}
// Assume it's known
private static final int ROW_ITEMS = 3;
private static final class GridAdapter extends BaseAdapter {
final ArrayList<String> mItems;
final int mCount;
/**
* Default constructor
* #param items to fill data to
*/
private GridAdapter(final ArrayList<String> items) {
mCount = items.size() * ROW_ITEMS;
mItems = new ArrayList<String>(mCount);
// for small size of items it's ok to do it here, sync way
for (String item : items) {
// get separate string parts, divided by ,
final String[] parts = item.split(",");
// remove spaces from parts
for (String part : parts) {
part.replace(" ", "");
mItems.add(part);
}
}
}
#Override
public int getCount() {
return mCount;
}
#Override
public Object getItem(final int position) {
return mItems.get(position);
}
#Override
public long getItemId(final int position) {
return position;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
final TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(mItems.get(position));
return view;
}
}
}
Will produce grid with six items. See more in corresponding Android Guide for Grid View.
Or you maybe use this:
MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> values=new ArrayList<String>();
values.add("Home");values.add("About");values.add("Contact");values.add("Help");values.add("Index");
values.add("Home");values.add("About");values.add("Contact");values.add("Help");values.add("Index");
GridView myGrid=(GridView)findViewById(R.id.grid);
myGrid.setAdapter(new ArrayAdapter<String>(this,R.layout.cell,values));
}
}
Your activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mySelection" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/grid"
android:columnWidth="75dip"
android:gravity="center"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="75dip">
</GridView>
Create a new xml file in layout folder and name it cell.xml and put this in it:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp">
Now run and enjoy. This is output:

How can I add my Dynamic Layout to ListView?

I have created a Dynamic LinearLayout and I want to add these layouts to ListView. The code is as follows:
DynamicLayout code:
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DynamicContentLayout extends LinearLayout{
private Context context;
private int linearLayoutMargin;
public DynamicContentLayout(Context context) {
super(context);
this.context = context;
linearLayoutMargin = (int) context.getResources().getDimension(R.dimen.xlarge_margin_padding_fixed);
initViews();
}
private void initViews() {
LinearLayout.LayoutParams layout_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_lp.setMargins(linearLayoutMargin, linearLayoutMargin, linearLayoutMargin, linearLayoutMargin);
this.setLayoutParams(layout_lp);
this.setOrientation(LinearLayout.VERTICAL);
this.addView(headerTitle("My Text"));
}
private TextView headerTitle(String title) {
TextView txtView = new TextView(context);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtView.setPadding(6, 6, 6, 6);
txtView.setText(title);
txtView.setBackgroundColor(context.getResources().getColor(R.color.red));
txtView.setTextColor(context.getResources().getColor(R.color.white));
return txtView;
}
}
ListAdpaterCode:
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class ListViewAdapter extends ArrayAdapter<DynamicContentLayout> {
private Context context;
private ArrayList<DynamicContentLayout> layoutList = null;
public ListViewAdapter(Context context, int textViewResourceId, ArrayList<DynamicContentLayout> layoutList) {
super(context, textViewResourceId, layoutList);
this.context = context;
this.layoutList = layoutList;
}
#Override
public int getCount() {
return layoutList.size();
}
#Override
public DynamicContentLayout getItem(int position){
return layoutList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
DynamicContentLayout entry = (DynamicContentLayout) layoutList.get(position);
convertView = (View) entry;
return convertView;
}
}
Finally the code in the MainActivity where I am setting the Adapter:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<DynamicContentLayout> contents = new ArrayList<DynamicContentLayout>();
contents.add(new DynamicContentLayout(getApplicationContext()));
contents.add(new DynamicContentLayout(getApplicationContext()));
contents.add(new DynamicContentLayout(getApplicationContext()));
ArrayAdapter<DynamicContentLayout> arrayAdapter = new ArrayAdapter<DynamicContentLayout>(getApplicationContext(), android.R.layout.simple_list_item_1, contents);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
Xml Layout:
<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"
>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:background="#color/blue"
android:layout_height="fill_parent"
android:alwaysDrawnWithCache="true"
android:clickable="true"
android:layout_alignParentTop="true"
android:divider="#color/grey"
android:drawingCacheQuality="auto"
android:fastScrollEnabled="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
Now, when I run , there is no error reported. But I couldn't see listItems layouts. What I can see is name of the class only. for example: "com.myexample.dynamicontentoverview.DynamicContentLayout#405193d0" as the row of the listItem instead of the Layout.
There is one more point I want to add. I cannot use XMl Layout as my views inside my DynamicLinearLayout can increase/decrease for certain rows.
Please suggests something.
Thanks.
You got the concept of custom listViews all wrong.
You should define your custom layout in an xml layout.
The type of your array adapter shouldn't be a layout. It should be the object type you are trying to populate the listView with.
Check out this tutorial to get a better idea of the steps you should do.
You are using ArrayAdapter as your adapter for the ListView instead of your own ListViewAdapter which extends ArrayAdapter< DynamicContentLayout>
What you are seeing now is your objects toString representation, because, well, thats what the ArrayAdapter does.

ListView with RelativeLayout not highlighting background on press

I want to have a list of elements (using a ListView) and each element in list is styled with a relative layout. Currently, the items are being displayed correctly, however, for some reason the listview items dont glow green when they're clicked. Why is this?
I have removed all code to minimals and it still does this.
Activity class
package com.test;
import android.app.Activity;
public class My_ListView extends Activity
{
private ListView_Adapter listViewAdapter;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
// initialise the list-view object
listViewAdapter = new ListView_Adapter(this);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(listViewAdapter);
for (int i=0;i<20;i++)
{
listViewAdapter.add("item "+i);
}
}
public void clicked(View v)
{
v.setBackgroundColor(0xFF0000FF);
}
}
The listview item adapter class
package com.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListView_Adapter extends ArrayAdapter<String>
{
public ListView_Adapter(Context c)
{
super(c, R.layout.my_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
StationFinder_ListViewItemHolder holder = null;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_listview_item, parent, false);
holder = new StationFinder_ListViewItemHolder(row);
row.setTag(holder);
}
else
{
holder = (StationFinder_ListViewItemHolder) row.getTag();
}
holder.populateFrom(getItem(position));
return row;
}
static class StationFinder_ListViewItemHolder
{
private TextView destination = null;
StationFinder_ListViewItemHolder(View row)
{
destination = (TextView) row.findViewById(R.id.text1);
}
void populateFrom(String locationDistance)
{
destination.setText(locationDistance);
}
}
}
my_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
my_listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="clicked"
>
<TextView android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I think you may have to call invalidate() after setting the background color.
You may want to do this instead by setting the listSelector and possibly drawSelectorOnTop for your ListView. That way the selection/deselection and clicks will be handled in the normal manner.
Edit - also, since you're using a ListView, you probably want to listen for clicks by setting an OnItemClickListener on your ListView.

Categories

Resources