Android ListView add text to the end of row - java

My ListView is goind to show the rank of users, usernames, and their score, smth like: 1. John 25
and I'd like to display the score just like it's shown on the example attached.

What you need to use is a CustomAdapter instead of the default adapter. Please refer to this other answer Custom Adapter for List View
Basically you create your own item layout: how you wish each item in the list will be displayed (it could be a TextView for the username and another TextView for the Score).
And then in the getView method of the Adapter set the value of each one.

You can achieve this using a custom list adapter and custom layout xml
Try this:-
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(new NameRankAdapter(this));
}
}
NameRankAdapter.java
public class NameRankAdapter extends BaseAdapter {
private Context context;
public NameRankAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, null);
}
TextView name = view.findViewById(R.id.name);
name.setText("Name " + i);
TextView rank = view.findViewById(R.id.rank);
rank.setText(String.valueOf(i));
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="name"
android:id="#+id/name"
android:textSize="30sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="name"
android:textSize="25sp"
android:id="#+id/rank"
android:textColor="#android:color/black"/>
</FrameLayout>

Related

ListView CustomAdapter not showing first position results

I'm new to Android Studio and coding in general and I am trying to display a custom ListView with an image of a colour and the name of the colour. I have used a custom Adapter to inflate the view with two arrays containing the drawable image colours and string of the names.
The problem I am having - the custom adapter is only displaying the layers from position 1 and onwards. For example, where the first layer should be img_01 and "red" - it is displaying img_02 and "Orange".
I've tried debugging the code and it seems that though the adapter is setting the values for position 0, but I cannot find the reason why it is not displaying (as the others are displaying fine).
The images & names are just placeholders, so an alternative solution that doesn't include both the drawables and names wouldn't be viable,
Thank you in advance
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
int[] images;
String[] colour;
LayoutInflater mInflater;
public CustomAdapter(Context c, int[] i, String[] col) {
this.context = c; //sets the application context that has been passed to the adapter
this.images = i; //sets the images array that has been passed to the adapter
this.colour = col; //sets the colour array that has been passed to the adapter
mInflater = (LayoutInflater.from(context)); //sets the layout inflater from context
}
#Override
public int getCount() { //total number of elements in the array
return images.length;
}
#Override
public Object getItem(int position) { //gets the item using the position
return null;
}
#Override
public long getItemId(int position) { //gets the item position
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) { //this is used to update the view
convertView = mInflater.inflate(R.layout.custom_layout, null);
ImageView image = convertView.findViewById(R.id.imageView);
TextView text = convertView.findViewById(R.id.textView);
text.setText(colour[position]);
image.setImageResource(images[position]);
return convertView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView mListView;
int[] images = {R.drawable.img01,
R.drawable.img02,
R.drawable.img03,
R.drawable.img04};
String[] colour = {"Red",
"Orange",
"Yellow",
"Green"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images, colour);
//passes information of images and application context to the item adapter
mListView.setAdapter(customAdapter);
//sets the adapter to the listview
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
app:srcCompat="#drawable/img01" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="36dp"
android:text="colourName" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try to set the height of the ListView to wrap_content instead of hardcoding it
<ListView
...
android:layout_height="wrap_content"/>
You can do it with RecyclerView.
Create a custom class
public class ExampleItem {
private int mImageResource; //your color image
private String mText1; //your text
public ExampleItem(int imageResource, String text1) {
mImageResource = imageResource;
mText1 = text1;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
}
Create a XML layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:text="Line 1"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Create a Recycler View Adapter
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
}
#Override
public int getItemCount() {
return mExampleList.size();
}
}
your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.application.recyclerviewproject.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.img01, "Red"));
exampleList.add(new ExampleItem(R.drawable.img02, "Orange"));
exampleList.add(new ExampleItem(R.drawable.img03, "Yellow"));
exampleList.add(new ExampleItem(R.drawable.img04, "Green"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}

Why the size of the images will be confusing when I use Glide in ListView?

I want to implement a ListView with internet images. I use Glide to load images in list items, but I found sizes of images always be confusing. I wrote some test code to reappear this problem.
At first it is normal.
However when I slide the ListView fastly, the sizes of images become confusing.
How can I fix the problem without modify the source code of glide?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] urls = new String[]{"https:......"};
//urls of images.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);
final SparseArray<String> sparseArray = new SparseArray<>();
listView.setAdapter(new BaseAdapter() {
#Override
public int getCount() {
return 100000;
}
#Override
public Object getItem(int i) {
String url = sparseArray.get(i);
if(url == null){
url = urls[(int)(Math.random() * urls.length)];
sparseArray.put(i,url);
}
return url;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = View.inflate(getBaseContext(),R.layout.list_view_item_image,null);
}
ImageView imageView = view.findViewById(R.id.image_view);
Glide.with(getBaseContext())
.load((String)getItem(i))
.into(imageView);
return view;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
list_view_item_image.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="wrap_content">
<ImageView
android:id="#+id/image_view"
android:adjustViewBounds="true"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Android : How to get a reference of a button defined in resource XML and not in Activity XML

Basically I am trying to code a onClickListener for a button in MainActivity.java .
This Button is defined in a resource file : main_resource.xml inside a ListView and not in activity_main.xml.
I am getting the error :
Trying to define a onClick Listener on null item.
Button btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFancyMethod(v);
}
});
Here is my main_resource.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip">
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="#+id/ridedetails"
android:layout_marginLeft="2dip">
</TextView>
<EditText
android:layout_height="wrap_content"
android:hint="Quote Price"
android:layout_width="wrap_content"
android:id="#+id/PriceQuotation"
android:layout_below="#+id/ridedetails"
android:layout_marginLeft="2dip">
</EditText>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/PriceQuotation"
android:layout_marginStart="61dp"
android:layout_toEndOf="#+id/PriceQuotation"
android:text="Button" />
</RelativeLayout>
Here is my activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
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:id="#+id/myrefresh"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/activity_home_drawer" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PreviousRide"
>
<ListView
android:id="#+id/incomingrides"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="30dp"
android:layout_marginTop="80dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Here I am not using any Custom Adapter Class : I am injecting the data into my list view using the following ArrayAdapter code written in onRefresh() of Swipe Refresh Widget ::
final ArrayAdapter<String > adapter=new ArrayAdapter<String>(getApplicationContext(),
R.layout.home_resource, R.id.ridedetails, allNames);
l1.setAdapter(adapter);
// L1 is the listView Reference
You have to create custom class for adapter
Here is sample code for adapter.
public class MyAdapter extends ArrayAdapter<String> {
private int resourceId;
private List<String> sites = null;
private Context context;
public MyAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.context = context;
this.resourceId = resource;
this.sites = objects;
}
#Override
public String getItem(int position) {
return sites.get(position);
}
#Override
public int getCount() {
return sites.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position);
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(resourceId, null);
}
TextView mTextView = (TextView) view.findViewById(R.id.ridedetails);
mTextview.setText(name);
Button mButton = (Button) view.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
set adapter in listview like below code.
l1.setAdapter(new MyAdapter(getApplicationContext(), R.layout.home_resource, allNames));
you should put your onClickListener in Adapter Not in Main Activity
Your Adapter
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Button btn = (Button ) v.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFancyMethod(v);
}
})
}

Efficient way to display data

I am creating an app that has a fragment where a user can open a spinner using a button and select a food item. The food item will then be displayed under the button in a list. I have tried creating textviews and making invisible textviews and setting the text later, but none have worked. I was wondering if anyone knew of a better way of creating this that I'm not seeing.
<FrameLayout 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="com.lastineindustries.ingredismartv2.Kitchen">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Add An Ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add"
android:textSize="30sp"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="320dp"
android:layout_height="match_parent"
android:id="#+id/main"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:id="#+id/valueButton"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
Is this what you want?
Then, read my code.
code of ChooseFoodFragment.java and its layout file:
public class ChooseFoodFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_choose_food, container, false);
initViews(v);
return v;
}
private Button btn_add;
private ListView listView;
private List<String> mData = new ArrayList<>();
private ArrayAdapter<String> mAdapter;
private void initViews(View v) {
listView = v.findViewById(R.id.list);
mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mData);
listView.setAdapter(mAdapter);
btn_add = v.findViewById(R.id.add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChooseFoodDialog dialog = new ChooseFoodDialog();
dialog.setOnSelectedListener(new ChooseFoodDialog.OnSelectedListener() {
#Override
public void onSelected(String name) {
mData.add(name);
mAdapter.notifyDataSetChanged();// update the list of selected food
}
});
dialog.show(getFragmentManager(), "");//show the spinner items to select
}
});
}
}
fragment_choose_food.xml,just remove other views except the root view and the button, then add a ListView.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="com.lastineindustries.ingredismartv2.Kitchen">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add An Ingredient"
android:textSize="30sp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp" />
</LinearLayout>
</FrameLayout>
the code of ChooseFoodDialog.java and its layout file.
public class ChooseFoodDialog extends DialogFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_choose_food, container, false);
initViews(v);
return v;
}
private ListView lv;
private List<String> mData = new ArrayList<>();
private void initViews(View v) {
// prepare some temp data
for (int i = 0; i < 10; i++) {
mData.add("Ingredients_" + i);
}
lv = v.findViewById(R.id.lv_ingredients);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mData);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String ingredient = mData.get(i);
if (mListener != null) {
mListener.onSelected(ingredient);// when the item of food is selected
}
dismiss();
}
});
}
private OnSelectedListener mListener;
public void setOnSelectedListener(OnSelectedListener listener) {
mListener = listener;
}
interface OnSelectedListener {
void onSelected(String name);
}
}
dialog_choose_food.xml,that's simple.
<?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">
<ListView
android:id="#+id/lv_ingredients"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
run the program, check it.
You should use ListView or RecyclerView for such tasks. Check out example from official Android documentation: link.

Implement custom Android grid view

I have made a custom gridView and a item witch I want to implement in the gridView, but how can I set these custom gridView onCreate inside my HomeActivity class file?
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// how can I say here that I want to set the gridView from list_holder.xml
}
}
list_holder.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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_item_block_holder"
android:layout_alignParentEnd="false"
android:numColumns="auto_fit"
tools:listitem="#layout/item_block" />
</RelativeLayout>
item_block.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_block"
android:padding="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#color/secondaryBackgroundColor">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/secondaryBackgroundColor"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView2"
android:background="#drawable/no_image_available" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/secondaryBackgroundColor"
android:padding="10dp"
android:layout_margin="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="video title"
android:id="#+id/item_block_title"
android:textColor="#color/textTitleColor"
android:textStyle="bold"
android:textSize="15dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/item_block_description"
android:textColor="#color/textContentColor"
android:textSize="10dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12-09-16"
android:id="#+id/item_date"
android:textColor="#color/textContentColor"
android:textSize="10dp"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Here Adapter like
AdapterDemoGrid adapterDemoGrid = new AdapterDemoGrid(this);
gridView.setAdapter(adapterDemoGrid);
}
}
Here, Adapter for Custom GridView Make this class :
public class AdapterDemoGrid extends BaseAdapter {
private final Context context;
private LayoutInflater mLayoutInflater;
public AdapterDemoGrid(Context context) {
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//lastPosition = -1;
}
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
//The view is not a recycled one: we have to inflate
view = mLayoutInflater.inflate(R.layout.item_block.xml, viewGroup, false);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
}
class ViewHolder {
/// Item Block views goes here...
}
}
Hopefully it will help you !!

Categories

Resources