Inflating a TextView and an ImageView within a LinearLayout from a RecyclerView - android

package com.example.android.popularmovies;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private ArrayList<String> mTrailers;
private Context mContext;
// Provide a suitable constructor (depends on the kind of dataset)
public MovieAdapter(Context context, ArrayList<String> trailers) {
mTrailers = trailers;
mContext = context;
}
// Create new views (invoked by the layout manager)
#Override
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LinearLayout v = (LinearLayout)LayoutInflater.from(parent.getContext())
.inflate(R.layout.trailer_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
public void watchYoutubeVideo(String id) {
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
try {
mContext.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
mContext.startActivity(webIntent);
}
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mImageView.setImageResource(R.drawable.ic_play_arrow_black_24dp);
holder.mTextView.setText("Trailer " + (position + 1));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
watchYoutubeVideo(mTrailers.get(holder.getAdapterPosition()));
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mTrailers.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ImageView mImageView;
public ViewHolder(View v) {
super(v);
mImageView = v.findViewById(R.id.play_button_image_view);
mTextView = v.findViewById(R.id.movie_text_view);
}
}
}
I want my UI to be like this
With my code however the app looks like this:
Here is my xml resource:
<?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="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp"
/>
<TextView
android:id="#+id/movie_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Is there a way to implement this without using different viewholders? Can it be done with a single ViewHolder?
I have seen ways to implement different View types with a RecyclerView, but I would prefer to keep my code as simple as possible.

change the height of LinearLayot to "wrap_content". and add gravity as "center_vertical"
change you item's layout like this
<?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:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp"
/>
<TextView
android:id="#+id/movie_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>

Try android:layout_height="wrap_content", instead of match_parent in LinearLayout.

1) You need to change the height of the layout: android:layout_height="wrap_content"
2) For the ImageView set:
android:layout_width="10dp"
android:layout_height="10dp"
(very small)

You can also use ConstraintLayout like this
<?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="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/ic_play_arrow_black_24dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Trailer 1"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
That being said the easiest solution would be simply using "wrap_content" on your parent layout.

try this
<?xml version="1.0" encoding="utf-8"?><?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:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp" />
<TextView
android:id="#+id/movie_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trailer 1"
android:layout_margin="5dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Output

Related

Horizontal Recyclerview not scrolling properly

I have a fragment layout that is a part of a PageViewer.
The fragment has 2 RecyclerViews - one on the top of the layout which is horizontal, the other one at the bottom which is vertical.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/fragment_marketplace_marketplace_title"
android:textSize="30sp"
android:textStyle="bold" />
<SearchView
android:id="#+id/fragment_marketplace_searchview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search..."
app:iconifiedByDefault="false"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="15dp"
android:text="#string/fragment_marketplace_discover_products_from_myverte"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment_marketplace_brands_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="#layout/fragment_marketplace_vendor_row_item" />
<android.support.v4.widget.NestedScrollView
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:background="#color/very_light_grey"
android:paddingTop="15dp"
android:text="#string/fragment_marketplace_featured_products"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment_marketplace_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/very_light_grey"
tools:listitem="#layout/fragment_marketplace_products_row_item" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
1) When scrolling left/right the scrolling sometimes gets stuck and not responsive. Why does this happen?
2) How do I make the view shows some part of the next views in the list, so it will make the filling of an actual scrollable list and not just a stale image?
edit -
here is my row item 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"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="10dp">
<ImageView
android:id="#+id/vendorImageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
edit -
my recyclerview initing -
private void initViews(View view) {
gson = new Gson();
miniVendorModelList = new ArrayList<>();
miniProductModelList = new ArrayList<>();
searchView = view.findViewById(R.id.fragment_marketplace_searchview);
Drawable drawable = getResources().getDrawable(R.drawable.search_widget_very_light_grey_background);
searchView.setBackground(drawable);
//adapters
vendorsAdapter = new VendorAdapter(miniVendorModelList);
productsAdapter = new ProductsAdapter(miniProductModelList, getContext());
//lists
vendorsList = view.findViewById(R.id.fragment_marketplace_brands_recycler_view);
productsList = view.findViewById(R.id.fragment_marketplace_products_recycler_view);
vendorsList.setNestedScrollingEnabled(false);
productsList.setNestedScrollingEnabled(false);
//brands recycler
vendorsList.setHasFixedSize(true);
vendorsList.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
vendorsList.setAdapter(vendorsAdapter);
//products recycler
productsList.setLayoutManager(new GridLayoutManager(getContext(), 2));
productsList.setHasFixedSize(true);
productsList.setAdapter(productsAdapter);
}
my adapter -
package com.twoverte.adapters;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.twoverte.R;
import com.twoverte.adapters.holder.VendorsHolder;
import com.twoverte.models.Vendor.MiniVendorModel;
import java.util.ArrayList;
public class VendorAdapter extends RecyclerView.Adapter<VendorsHolder> {
private ArrayList<MiniVendorModel> miniVendorModels;
public VendorAdapter(ArrayList<MiniVendorModel> miniVendorModels) {
this.miniVendorModels = miniVendorModels;
}
#NonNull
#Override
public VendorsHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_marketplace_vendor_row_item, viewGroup, false);
return new VendorsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull VendorsHolder vendorsHolder, int i) {
MiniVendorModel model = miniVendorModels.get(i);
Picasso.get().load(model.getImageURL()).memoryPolicy(MemoryPolicy.NO_CACHE).into(vendorsHolder.vendorImageView);
}
#Override
public int getItemCount() {
return miniVendorModels.size();
}
}
How do I make the view shows some part of the next views in the list, so it will make the filling of an actual scrollable list and not just a stale image?
What you can do here is make your layout/fragment_marketplace_vendor_row_item occupy say 80% width also add some padding
update width to 80%
android:layout_width="0dp"
android:layout_weight="0.8"
When scrolling left/right the scrolling sometimes gets stuck and not responsive. Why does this happen?
this might have been answered here
Found a solution for bad scrolling - wrapped the horizontal RV with a NestedScrollView. Works perfectly, I have no idea why. Just trial and error.
If someone knows why this works it would be awesome.

How Can I Add Two Buttons In A Row Of ListView Without Overlapping

I want to make a app like that custom layout ScreenShot: .This is one row and I add like that row 100 more.So the people can clik number or its information(i).
But it is shown like that : two buttons overlapping .But I want two button next to each other it is like that in custom_layout.xml(you can see screen shot).
This is my custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="87dp" />
<Button
android:id="#+id/button2"
android:layout_width="51dp"
android:layout_height="49dp"
android:text="Button"
tools:layout_editor_absoluteX="317dp"
tools:layout_editor_absoluteY="87dp" />
</android.support.constraint.ConstraintLayout>
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="162dp"
android:layout_height="424dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
</ScrollView>
And this is my MainActivity.java:
package com.hey.task.attendancesystem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button[] button=new Button[15];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
CustomAdapter customAdapter = new CustomAdapter();
//listeyi customlayouttaki şekilde oluşturdum hepsini
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return button.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
Button button1 = (Button) view.findViewById(R.id.button);
Button button2 = (Button) view.findViewById(R.id.button2);
for (int j = 0;j<15;j++)
button1.setText(i + "");
for (int j = 0;j<15;j++)
button2.setText("i");
return view;
}
}
}
I don't know if i get your question but you can simply use LinearLayout with weight. eg:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 2" />
</LinearLayout>
You can use linear layout with a android:weightSum="" attribute in place of constraint layout in your custom_layout.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:weightSum="1.0"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.2"/>
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.8"/>
</LinearLayout>
weightSum attribute is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

Recycler view and Card View not working properly

Just check out main activity .xml
what i am doing is displaying recycler view beneath a button and edit text ..
the value from edit text is taken made into a card but the problem is the button is not working i dont know why plz help
I am new to Coordinator views so plz pardon me if i doing some noob mistake
AdapterEx.java
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class AdapterEx extends RecyclerView.Adapter<AdapterEx.ViewHolder>{
private List<String> nos;
public AdapterEx(List<String> nos){
this.nos = nos;
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView number;
public ViewHolder(View itemView) {
super(itemView);
number =
(TextView)itemView.findViewById(R.id.nos);
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
int position = getAdapterPosition();
Snackbar.make(v, "Click detected on item " + position,
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.number.setText(nos.get(i));
}
#Override
public int getItemCount() {
return nos.size();
}
}
initializing recycler view
recycler = (RecyclerView) findViewById(R.id.rv);
LayoutManager manager = new LinearLayoutManager(this);
recycler.setLayoutManager(manager);
nos = new ArrayList<>();
nos.add("sadasd");
nos.add("sdada");
adapter = new AdapterEx(nos);
recycler.setAdapter(adapter);
recycler.setHasFixedSize(true);
recycler.setItemAnimator(new DefaultItemAnimator());
main activity . xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.dev99.mathbuddy.prime"
android:id="#+id/prime"
android:background="#drawable/prime">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="number|numberDecimal|numberSigned"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FACTORISE IT!"
android:id="#+id/button5"
android:onClick="checker"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editText2"
android:layout_below="#+id/button5"
android:textAlignment="center"
android:textSize="30dp"
android:textColor="#000000"
android:layout_alignParentStart="false"
android:layout_alignParentLeft="false" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
card view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#color/bluemat"
card_view:cardElevation="10dp"
card_view:contentPadding="4dp"
android:clickable="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nos"
android:textSize="40dp"
android:textStyle="bold"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="#color/red"/>
</android.support.v7.widget.CardView>
The problem is RecyclerView with wrap_content.
For some reason, a RecyclerView does not display the last item or the last few items well. The most common problem I experienced is that the last item got cut off.

RecyclerView Margin

i am developing application for home security. One of the feature, that must be implemented is the ability to see connected devices (for sending notifications, blocking access, etc). So far, i've been able to create RecyclerView list of the devices, everything is perfect (for me), except that cards in this list have no spacing between them.
Screenshot of how it looks now
device_data_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="#+id/deviceIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="8dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignBottom="#+id/deviceIcon"
android:layout_toEndOf="#+id/deviceIcon">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="#+id/deviceUsername"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Model"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:id="#+id/deviceModel"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Version"
android:gravity="center_vertical|right"
android:id="#+id/deviceVersion"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
devices_list.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"
android:padding="#dimen/activity_vertical_margin">
<view
android:id="#+id/connectedDevicesList"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
View Holder Code
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.exampleapp.R;
public class DeviceViewHolder extends RecyclerView.ViewHolder {
protected ImageView deviceIcon;
protected TextView deviceUsername;
protected TextView deviceModel;
protected TextView deviceVersion;
public DeviceViewHolder(View view) {
super(view);
this.deviceIcon = (ImageView) view.findViewById(R.id.deviceIcon);
this.deviceUsername = (TextView) view.findViewById(R.id.deviceUsername);
this.deviceModel = (TextView) view.findViewById(R.id.deviceModel);
this.deviceVersion = (TextView) view.findViewById(R.id.deviceVersion);
}
}
RecyclerView Adapter
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import com.exampleapp.API.AuthorizedDevice;
import com.exampleapp.R;
public class ConnectedDeviceAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
private List<AuthorizedDevice> devices;
private Context mContext;
public ConnectedDeviceAdapter(Context context, List<AuthorizedDevice> devices) {
this.devices = devices;
this.mContext = context;
}
#Override
public DeviceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
DeviceViewHolder viewHolder = new DeviceViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(DeviceViewHolder deviceViewHolder, int i) {
AuthorizedDevice device = devices.get(i);
if (device.getDeviceId().equals("0")) {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.android_icon));
} else {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.apple_icon));
}
deviceViewHolder.deviceUsername.setText(device.getUsername());
deviceViewHolder.deviceModel.setText(device.getDeviceName());
deviceViewHolder.deviceVersion.setText(device.getDeviceVersion());
}
#Override
public int getItemCount() {
return (null != devices ? devices.size() : 0);
}
}
Can anyone please help me solve this problem? Thank you very much in advance!
In your RecyclerView Adapter onCreateViewHolder:
replace:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
with:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, viewGroup, false);
Or you can use RecyclerView.addItemDecoration(ItemDecoration decor).

how to remove empty list item in recycler views?

I'm using recycler view to show items with multiple views. whenever I launch the app some white space is being showed. which is exactly equal to my list items.
I provided the screenshot please go through it.
I think there is no problem with my code.
following is my code
recyclerview
<?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"
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=".activities.GroupView">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_group_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
adapter code
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Objects;
import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.pojo.PdfItemHolder;
import pdfshare.hemanthreddy.com.pdfshare.pojo.RequestPojo;
public class GroupContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int PDF = 0,REQUEST = 1;
List<Object> list;
OnClickListener onClickListener;
Context context;
public GroupContentAdapter(List<Object> list, OnClickListener onClickListener, Context context) {
this.list = list;
this.onClickListener = onClickListener;
this.context = context;
}
public class PdfHolder extends RecyclerView.ViewHolder {
TextView pdfName,pdfDescription,Helped,uploadedBy;
Button downloadPdf,viewPdf;
View container;
CardView cardView;
public PdfHolder(View itemView) {
super(itemView);
container = itemView;
uploadedBy = (TextView) itemView.findViewById(R.id.pdf_info_uplodedby);
pdfName = (TextView) itemView.findViewById(R.id.pdf_name);
pdfDescription = (TextView) itemView.findViewById(R.id.pdf_description);
Helped = (TextView) itemView.findViewById(R.id.helped);
downloadPdf = (Button) itemView.findViewById(R.id.download_pdf);
viewPdf = (Button) itemView.findViewById(R.id.view_pdf);
cardView = (CardView) itemView.findViewById(R.id.card_view_pdf_row);
}
}
public class RequestHolder extends RecyclerView.ViewHolder {
View container;
TextView name,request;
public RequestHolder(View itemView) {
super(itemView);
container = itemView;
name = (TextView) itemView.findViewById(R.id.requested_name);
request = (TextView) itemView.findViewById(R.id.request);
}
}
public interface OnClickListener{
public void OnItemClick(View view,int position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case PDF:
View v1 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v1);
break;
case REQUEST:
View v2 = inflater.inflate(R.layout.group_request_row_view,parent,false);
viewHolder = new RequestHolder(v2);
break;
default:
View v3 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v3);
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder.getItemViewType() == PDF)
{
PdfItemHolder pdfItemHolder = (PdfItemHolder) list.get(position);
PdfHolder pdfHolder = (PdfHolder) holder;
if(!TextUtils.isEmpty(pdfItemHolder.getPdfName())) {
pdfHolder.pdfName.setText(pdfItemHolder.getPdfName());
pdfHolder.pdfDescription.setText(pdfItemHolder.getPdfDescription());
pdfHolder.Helped.setText(pdfItemHolder.getHelped());
pdfHolder.uploadedBy.setText(pdfItemHolder.getUplodedBy());
pdfHolder.downloadPdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "downloading pdf bitch", Toast.LENGTH_LONG).show();
}
});
pdfHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickListener.OnItemClick(v, position);
}
});
}
else
{
Toast.makeText(context,"empty",Toast.LENGTH_LONG).show();
Log.e("empty","empty");
pdfHolder.container.setVisibility(View.GONE);
pdfHolder.pdfName.setVisibility(View.GONE);
pdfHolder.pdfDescription.setVisibility(View.GONE);
pdfHolder.Helped.setVisibility(View.GONE);
pdfHolder.downloadPdf.setVisibility(View.GONE);
pdfHolder.uploadedBy.setVisibility(View.GONE);
pdfHolder.viewPdf.setVisibility(View.GONE);
}
}
else
{
RequestHolder requestHolder = (RequestHolder) holder;
RequestPojo requestPojo = (RequestPojo) list.get(position);
requestHolder.name.setText(requestPojo.getName());
requestHolder.request.setText(requestPojo.getRequest());
}
}
#Override
public int getItemCount() {
return list.size();
}
#Override
public int getItemViewType(int position)
{
if(list.get(position) instanceof PdfItemHolder)
return PDF;
else if(list.get(position) instanceof RequestPojo)
return REQUEST
;
return -1;
}
}
also I checked stack overflow to remove spaces and I added some code to adapter to check wheather the data is empty or not, it didn't worked well.
following Is code of row views`group_row_view code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Requested By : "
android:id="#+id/textView7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/requested_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/request"
android:lines="2"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
code of Pdf_row_view
</LinearLayout>`
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="pdfName"
android:id="#+id/pdf_name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp"
android:lines="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="pdf descriptionl"
android:id="#+id/pdf_description"
android:layout_below="#+id/pdf_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:lines="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Pdf"
android:id="#+id/view_pdf"
android:layout_alignTop="#+id/download_pdf"
android:layout_toRightOf="#+id/pdf_info_uplodedby"
android:layout_toEndOf="#+id/pdf_info_uplodedby" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download Pdf"
android:id="#+id/download_pdf"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/pdf_description" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="helped"
android:id="#+id/helped"
android:textSize="15dp"
android:layout_alignBottom="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_alignTop="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Upladed by : "
android:id="#+id/pdf_info_uplodedby"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
please help me.. thank you
In all of your list item layout files:
pdf_row_view.xml, group_request_row_view.xml, pdf_row_view.xml
set layout_height="wrap_content" of their root layout
Remove the match_parent and set wrap_content as the layout_height instead in the root of your layout of your list items.
For example your pdf_row_view.xml should look like this
<?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="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<!-- Other elements in your layout -->
</android.support.v7.widget.CardView>
</RelativeLayout>
See, I've set the android:layout_height="wrap_content" in the root RelativeLayout of your pdf_row_view list item.
Do the same for the other list item layouts.
Just set the height of the rootView of specific viewHolder to 0 like this -
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
rootView.setLayoutParams(layoutParams);
where rootView is same as this -
ViewHolder(View view) {
super(view);
rootView = view;
}
Let me know if you want more clarity on this.

Categories

Resources