I'm new at Android coding and I have a problem with my ListView. I have it showing the title and the description of the title. I want to ellipsize the description but I don't know how to.
This is the code for 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="com.example.filip.htecjson.MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:ellipsize="marquee"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
And this is the adapter I'm using:
class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> items;
public MyAdapter(Context context, ArrayList<Item> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TwoLineListItem twoLineListItem;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
twoLineListItem = (TwoLineListItem) inflater.inflate(
android.R.layout.simple_list_item_2, null);
} else {
twoLineListItem = (TwoLineListItem) convertView;
}
TextView text1 = twoLineListItem.getText1();
TextView text2 = twoLineListItem.getText2();
text1.setText(items.get(position).getTitle());
text2.setText("" + items.get(position).getDescription());
return twoLineListItem;
}
}
I tried adding this but it doesn't do anything as far as I can tell.
android:ellipsize="marquee"
android:singleLine="true"
So is there a way I can ellipsize just the description?
Change to this line in getView:
getLayoutInflater().inflate(R.layout.anthing_to_inflate, parent, false);
Related
I made BottomNavigationView, and I want to load my gallery pictures within one of them.
I used Glide with cursor to get uri of pictures (in fragment code).
And made RecyclerView Adapter to show images. But it doesn't work.
There is no error but when I run my project on my phone, it doesn't show any picture...
What is the problem? :(
private RecyclerView recyclerView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
initData();
return view;
}
public void initData(){
listt = getPic();
recyclerView = getView().findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
recyclerView.setAdapter(new exAdap(listt, getContext()));
}
public ArrayList<String> getPic() {
ArrayList<String> fileList = new ArrayList<>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, MediaStore.MediaColumns.DATE_ADDED + " desc");
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
int columnDisplayname = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
int lastIndex;
while (cursor.moveToNext()) {
String absolutePathOfImage = cursor.getString(columnIndex);
String nameOfFile = cursor.getString(columnDisplayname);
lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
lastIndex = lastIndex >= 0 ? lastIndex : nameOfFile.length() - 1;
if (!TextUtils.isEmpty(absolutePathOfImage)) {
fileList.add(absolutePathOfImage);
}
}
cursor.close();
return fileList;
}
private ArrayList<String> datas;
private Context context;
public exAdap(ArrayList<String> datas, Context context) {
this.datas=datas;
this.context = context;
}
#NonNull
#Override
public exAdap.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.activity_gallery, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context).load(datas.get(position)).into(holder.getImage());
holder.image.setImageURI(Uri.parse(datas.get(position)));
}
#Override
public int getItemCount() {
return datas.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image=itemView.findViewById((R.id.recylcerview_row_image));
}
public ImageView getImage(){return this.image;}
}
fragment_gallery.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=".fragment_gallery">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="409dp"
android:layout_height="460dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:id="#+id/recylcerview_row_image"
android:src="#mipmap/ic_launcher_round"
android:adjustViewBounds="true"
android:layout_height="wrap_content" />
</RelativeLayout>
You are using 2 methods together instead of using only one. Because both methods are loading the image in imageview. So better try only one method at a time & see if that makes a difference or not.
This question already has answers here:
ListView not showing at all
(1 answer)
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
I have a custom Adapter called MyAdapter where i'm trying to load a layout which supposedly should be inserted into a listview, but even when Android is not giving errors the view is showing in blank.
In the next screenshot, i show what i get after build my app.
What i'm expecting to load is my layout into my listview:
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=".Fragments.NewsFragment">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
Layout
<?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:id="#+id/list_item"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageabdcf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="25dp"
tools:srcCompat="#android:drawable/btn_star_big_on" />
<TextView
android:id="#+id/textabcdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
NewsFragment.java
public class NewsFragment extends Fragment {
private ListView listView;
private List<String> names;
public NewsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) v.findViewById(R.id.listView);
names = new ArrayList<String>();
names.add("Fernando");
names.add("Roberto");
names.add("Torres");
names.add("Urban");
ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
}
});
MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
listView.setAdapter(myAdapter);
// Inflate the layout for this fragment
return v;
}
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private Context context;
private int layout;
private List<String> names;
public MyAdapter(Context context, int layout, List<String> names) {
this.context = context;
this.layout = layout;
this.names = names;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return this.names.get(position);
}
#Override
public long getItemId(int id) {
return id;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
v = layoutInflater.inflate(R.layout.fragment_news, null);
String currentName = names.get(position);
//currentName = (String) getItem(position);
TextView textView = (TextView) v.findViewById(R.id.textabcdf);
textView.setText(currentName);
return v;
}
}
And what i'm expecting for is something like:
I'm trying to get ArrayList with different variables that i'm getting from a database to display in a customized listview
this is the 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:text="View the balance sheet"
android:textSize="29sp"
android:id="#+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</RelativeLayout>
this is the customized layout
<?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/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
this is the adapter, i wanted both the ArrayLists from the main activity but it's not working
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(#NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
main activity
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
i'm getting values from the database then putting them in ArrayLists then i'm trying to display them in a customized listview but it's not working when i try to use the second Arraylist
adapter calls from your main activity should be like,
CusatomeAdapter adapter;
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);
mListview.setAdapter(adapter);
here is your adapter calss
public class CusatomeAdapter extends BaseAdapter {
public ArrayList<String> mList1;
public ArrayList<String> mList2;
Activity activity;
public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
super();
this.activity = activity;
this.mList1 = mList1;
this.mList2 = mList2;
}
#Override
public int getCount() {
return mList1.size(); //give the size of list here
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail, null);
holder = new ViewHolder();
holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
//same way define another text view and set their value ..
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_detail.setText(mList1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView tv_detail;
}
}
I am new to android I have created recycler view with its adapter but it is not working. check the following code.
I tried to debug this activity but there is no error. and also in OtherOfferingAdapter program don't execute further after getItemCount().
File OtherOfferingAdapter.java
public class OtherOfferingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<OtherOfferingData> data = Collections.emptyList();
OtherOfferingData current;
int currentPos=0;
//Create constructor to innitilize context and data sent from MainActivity
public OtherOfferingAdapter(Context context,List<OtherOfferingData> data){
this.context=context;
inflater = LayoutInflater.from(context);
this.data=data;
}
//Inflate the layout when viewholder created
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = inflater.inflate(R.layout.container_offerings, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
//Bind Data
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
MyHolder myHolder = (MyHolder) holder;
current = data.get(position);
myHolder.inputOfferingName.setText(current.offeringname);
myHolder.inputOfferingPrice.setText(current.price);
myHolder.inputOfferingRating.setRating(Float.parseFloat(current.rating));
Picasso.with(context).load(current.imageUrl).into(myHolder.inputOfferingImage);
if(current.dietType.equals("1")){
myHolder.inputDietType.setBackgroundResource(R.drawable.veg);
}else{
myHolder.inputDietType.setBackgroundResource(R.drawable.nonveg);
}
}
#Override
public int getItemCount(){
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
ImageView inputOfferingImage, inputDietType;
TextView inputOfferingPrice, inputOfferingName;
RatingBar inputOfferingRating;
public MyHolder(View itemView){
super(itemView);
inputOfferingImage = (ImageView) itemView.findViewById(R.id.otherimage);
inputDietType = (ImageView) itemView.findViewById(R.id.otherdietType);
inputOfferingPrice = (TextView) itemView.findViewById(R.id.otherprice);
inputOfferingName = (TextView) itemView.findViewById(R.id.otherofferingnanme);
inputOfferingRating = (RatingBar) itemView.findViewById(R.id.otherrating);
}
}
}
File OtherOfferingData
public class OtherOfferingData {
public String imageUrl;
public String offeringname;
public String price;
public String dietType;
public String rating;
}
And I am calling adapter like this
recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
offeringAdapter = new OtherOfferingAdapter(ProductDescriptionActivity.this, data);
recyclerView.setAdapter(offeringAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(ProductDescriptionActivity.this));
container_offerings.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginBottom="30dp"
android:background="#color/lightGray"
android:paddingLeft="15dp"
android:paddingRight="15dp">
Activity Page
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycle_view"
android:layout_width="0dp"
android:layout_height="0dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/lightGray"
android:layout_marginBottom="90dp"/>
ThankYou
The size of your recycler view in the relative layout is 0dp try to change
android:layout_width="0dp"
android:layout_height="0dp"
both to match parent
android:layout_width="match_parent"
android:layout_height="match_parent"
Your recycler view and its adapter seems to be fine. But they don't appear on screen as they have 0dp as dimension.
Hi All I am Using a GridView Tool From The Android To Display The Image As A menu
I am Using this Code in A class I Name It ImageAdapter extends BaseAdapter
I've Use This Code Bellow
package com.example.gridviewtest;
import com.example.gridviewtest.R;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds =
{
R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8, R.drawable.pic9
};
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
return imageView;
}
}
I try to use a
imageView.setText("Something");
but it doesn't Works With Me
the require is to add a caption under a picture in a cell
Can You Help Me On This ...
Best Regards ...
You could use a custom adapter (as for a listView):
public class CustomAdapter extends ArrayAdapter<Integer> {
Activity context;
ArrayList<Integer> objects;
public CustomAdapter(Activity context, ArrayList<Integer> objects) {
super(context, R.layout.row, objects);
this.context = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
}
ImageView i = (ImageView) convertView.findViewById(R.id.icon);
i.setBackgroundResource(objects.get(position));
TextView t = (TextView) convertView.findViewById(R.id.title);
t.setText("title");
return convertView;
}
}
And of course you will set this it as an adapter for the gridView.
EDIT: Of course instead of ArrayAdapter<Integer> you could be extending ArrayAdapter<YourHolder>.
class YourHolder {
public int ID;
public String title;
}
EDIT 2: Adding a functional piece of code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:gravity="center_horizontal"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp"
android:background="#600000ff"
/>
</RelativeLayout>
grid_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#6000ff00"
android:padding="4dp">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="label"
android:layout_gravity="center_horizontal"/>
<FrameLayout
android:id="#+id/holder"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</FrameLayout>
</LinearLayout>
CustomAdapter.class
public class CustomAdapter extends ArrayAdapter<Integer> {
private Activity context;
private ArrayList<Integer> objects;
public CustomAdapter(Activity context, ArrayList<Integer> objects) {
super(context, R.layout.grid_item_layout, objects);
this.context = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater i = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) i.inflate(R.layout.grid_item_layout, parent, false);
}
TextView t = (TextView) convertView.findViewById(R.id.label);
ImageView i = (ImageView) convertView.findViewById(R.id.image);
t.setText("label " + position);
i.setImageResource(objects.get(position));
return convertView;
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView g = (GridView) findViewById(R.id.grid_view);
CustomAdapter adapter = new CustomAdapter(this, getData());
g.setAdapter(adapter);
}
public ArrayList<Integer> getData(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.pic1);
// add 2 - 11
list.add(R.drawable.pic12);
return list;
}
}
Final look:
Note: From my experience when there are a lot of pictures in the gridView scrolling becomes slow and laggy, however it's not the topic of your question, so in case it's also a problem, please see Lazy load of images in ListView