What could this empty spacing under my listview item be? - android

I extended the ArrayAdapter to inflate my own custom layout (it doesn't do too much else and the implementation is fairly straightforward). But I noticed there's this hairline of empty space between my listview items:
https://www.evernote.com/l/AM8w6ffsaQhGNJreixsY5fOwDbzTEL7Z73E
I've tried setting the height of the listview item as opposed to using wrap_content, there's no margin anywhere, and I'm setting the background color of the root view of the listview item so I'm having a hard time trying to figure out why that empty space is being added.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.event_row, parent, false);
holder = new ViewHolder();
holder.time = (TextView) convertView.findViewById(R.id.clock_event_time);
holder.title = (TextView) convertView.findViewById(R.id.clock_event_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ClockEvent clockEvent = mClockEvents.get(position);
holder.time.setText(
ClockUtil.halfHourIndexToHourString(clockEvent.getStartHalfHourIndex())
+ " - " +
ClockUtil.halfHourIndexToHourString(clockEvent.getEndHalfHourIndex())
);
holder.title.setText(clockEvent.getTitle());
convertView.setBackgroundColor(clockEvent.getColor());
return convertView;
}
And here's the 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:orientation="horizontal"
android:background="#color/flatui_blue_1"
android:padding="12dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical">
<com.compscieddy.foraday.ui.ForadayTextView
android:id="#+id/clock_event_time"
android:layout_height="wrap_content"
android:background="#drawable/rounded_white_outline"
app:fontface="montserrat_light"
android:paddingTop="2dp"
android:paddingBottom="2dp"
tools:text="7:00 - 8:00"
android:textSize="11sp"
style="#style/EventTimeTextView"/>
<com.compscieddy.foraday.ui.ForadayTextView
android:id="#+id/clock_event_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Early Morning Jog"
android:textColor="#android:color/white"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>

Since it's a ListView this probably is its divider. See dividerHeight, you can set it in xml or java to remove the divider.
android:dividerHeight="0dp"
// or
listView.setDividerHeight(0);

Ah - what a noob, I forgot the default styling of a dividerHeight exists. Solved by setting android:dividerHeight to 0dp. Also could have added a style/theme.

Related

Spinner custom style is not applying properly

I want to apply alternative spinner element background colour, and it works fine for spinner list with limited items that don't need to scroll. when the scroll is possible, the background colour is only applied to text background
#Override public View getDropDownView(int position, View view, ViewGroup parent) {
ViewHolder holder = null;
if(view==null)
{
view= inflater.inflate(R.layout.citylist, parent, false);
holder=new ViewHolder();
holder.txtTitle = (TextView) view.findViewById(R.id.tv);
holder.txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP,db.getSettings().getInt(15)-3);
holder.txtTitle.setPadding(10, 10, 10, 10);
view.setTag(holder);
}
else{
holder=(ViewHolder)view.getTag();
}
holder.txtTitle.setText(data.get(position));
if(position % 2 == 0)view.setBackgroundColor(Color.rgb(224, 224, 235));
if(position % 2 == 1)view.setBackgroundColor(Color.WHITE);
return view;
}
XML of Spinner
<Spinner
android:id="#+id/pnakshathram"
android:layout_width="0dp"
android:layout_weight="2"
android:paddingLeft="15dp"
android:background="#drawable/edittextbackground"
android:layout_height="wrap_content"/>
You need to create a custom layout with the background you want to set and then You need to do this -
set your custom layout in setDropDownViewResource
Spinner yourSpinner = (Spinner) findViewById(R.id.yourSpinner);
yourAdapter.setDropDownViewResource(R.layout.my_spinner);
yourSpinner.setAdapter(adapter);
You need to use your layout in place of android.R.layout.simple_spinner_dropdown_item
dont use android.R.layout.simple_spinner_dropdown_item use your_custom_layout_here
go to your res folder , create a new xml file give any name like my_spinner.xml
my_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/clear_sans_regular"
android:gravity="left"
android:padding="15dp"
android:textColor="#color/warm_grey"
android:textSize="14sp" />
and then add this line
yourAdapter.setDropDownViewResource(R.layout.my_spinner);
In your citylist.xml you have to make your TextView (tv) property to android:layout_width="match_parent" it will show like exactly you want.
Update
if(position % 2 == 0)holder.txtTitle.setBackgroundColor(Color.rgb(224, 224, 235));
if(position % 2 == 1)holder.txtTitle.setBackgroundColor(Color.WHITE);

Hide Button In ArrayAdapter

I have a button which receive permissions in runtime. When user clicks that button, popup opens and user can give permission to read contacts.
When user give the permission, button should be hidden and users must be listed.
But when list is creating, hidden buttons come back. How can i delete them? Thanks.
FRAGMENT
boolean granted = checkContactPermission();
if(granted){
permissionButton.setVisibility(View.GONE);
permissionText.setVisibility(View.GONE);
ArrayList contacts = getContacts(getActivity());
ArrayAdapter adapter = new ArrayAdapter<Contact>(getActivity(), R.layout.contactpage_layout, R.id.test, contacts);
listView.setAdapter(adapter);
}
LAYOUT
<?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="match_parent">
<TextView
android:id="#+id/permissionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/permissionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="200dp"
android:text="Permission"
app:layout_constraintEnd_toEndOf="parent" />
<ListView
android:id="#+id/contactListview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
</android.support.constraint.ConstraintLayout>
You're setting the visibility in your parent Fragment, you need to set the visibility for each particular cell in your adapter when you override getView().
You need to implement a custom BaseAdapter for your listView instead ArrayAdapter,
maybe this answer could help you
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(layoutResID, parent, false);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(items.get(position).getText1());
holder.text2.setText(ititems.get(position)getText2());
if(items.get(position).isSelected()){
holder.text2.setVisibility(View.VISIBLE) //This can be your own Button
}else{
holder.text2.setVisibility(View.GONE)
}
return convertView;
}

ListView items drawn on top of each other when scrolling (Android)

In a fragment, I have a ListView that has a custom ParseQueryAdapter<T>. The problem may not have anything to do with Parse, although I'm not sure.
As I was testing my app, I noticed something very strange. When I would scroll down my ListView, all the visible ListView items would be drawn on top of the next ListView item as seen in the second image below.
The list initialized properly as such:
As you can see, in my list item layout, I have an ImageView (ParseImageView to be specific) and a TextView. The TextView now displays some notes (don't mind the ID user_name_text_view) and the ImageView displays a placeholder blank profile picture.
When I scrolled down, the list looked like:
Here's my list view layout named fragment_post_view_list_view:
<?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="vertical" >
<ListView
android:id="#+id/post_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Here's my list item layout named list_item_post_view:
<?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">
<com.parse.ParseImageView
android:id="#+id/icon"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:background="#drawable/com_facebook_profile_picture_blank_square" />
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/link_blue" />
</RelativeLayout>
Here's my adapter named PostViewListViewAdapter:
public class PostViewListViewAdapter extends ParseQueryAdapter<Post> {
// call superclass with a query to populate list view
public PostViewListViewAdapter(Context context, final String[] postsObjectIds) {
super(context, new ParseQueryAdapter.QueryFactory<Post>(){
public ParseQuery<Post> create() {
ParseQuery<Post> query = Post.getQuery();
query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
return query;
}
});
}
// this is similar to getView method in an adapter
#Override
public View getItemView(Post post, View v, ViewGroup parent) {
if(v == null) {
v = View.inflate(getContext(), R.layout.list_item_post_view, null);
}
super.getItemView(post, v, parent);
TextView usernameTextView = (TextView) v.findViewById(R.id.user_name_text_view);
usernameTextView.setText(post.getNotes()); // some string
return v;
}
}
How can I fix this problem?
Is this an issue with XML or Java?
I was following the two tutorials from Parse and the example from the Parse docs:
MealSpotting
Parse Query Adapter
I set the adapter and ListView here:
View rootView = inflater.inflate(R.layout.fragment_post_view_list_view, container, false);
mPostsObjectIds = SOME_STRING[];
PostViewListViewAdapter adapter = new PostViewListViewAdapter(getActivity(), mPostsObjectIds);
ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);
I've tried getting rid of the ParseImageView in my list item layout, but my TextViews still draw on top of each other when I scroll.
Edit:
I forgot to mention that the list items display on top of each other after an orientation change.
I tested this on my Galaxy S5 (Android version 4.4.2 and Parse 1.4.1).
In my Activity, I show the Fragment here (called PostViewListViewFragment):
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();
Try below 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"
android:orientation="vertical" >
<ListView
android:id="#+id/post_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >
</ListView>
</RelativeLayout >
Make Sure your adapter like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
return rowView;
}
}
PS:You should watch this Google IO video about Listview,and here is the slides.
First create a ViewHolder class
static class ViewHolder {
protected TextView usernameTextView;
}
Then change your getItemView method like below
public View getItemView (Post post, View convertView , ViewGroup parent)
{
ViewHolder viewHolder = null;
if (convertView == null)
{
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.list_item_post_view, null);
viewHolder = new ViewHolder();
viewHolder.usernameTextView = (TextView)convertView.findViewById(R.id.user_name_text_view);
convertView.setTag(viewHolder);
convertView.setTag(R.id.user_name_text_view, viewHolder.usernameTextView);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.usernameTextView.setText(post.getNotes()); // some string
return convertView;
}
The problem seems to be in your list item layout -
Change it to 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">
<com.parse.ParseImageView
android:id="#+id/icon"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/link_blue" />
</RelativeLayout>
Probably you have extra background for each list item set that is causing such effect.
Alter and watch.
Hope this gives you idea!
Try changing your list view layout height to match_parent.
Credit to #VedPrakash for helping me fix this.
In case it helps anyone, I fixed the problem by replacing the fragment not adding it. So I changed this line from:
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();
to:
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new PostViewListViewFragment()).commit();

Android - how to align list view items to be nicely spaced left and right align?

I am trying to add an image to my ListView to make it look more like a button. I would like the images to be a little smaller, maybe 60% of current. And the images to lign up nicely on the right in a column. Here is a screen of what I currently have:
and here is my list view xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:layout_width="match_parent"
android:drawableRight="#drawable/arrow_button"
>
</TextView>
any idea what I am doing incorrectly?
The ListView that contains this TextView is defined like this:
One note, the way I create and work with my Lists is with the ListAdapter, using code like this:
Question q = new Question ();
q.setQuestion( "This is a test question and there are more than one" );
questions.add(q);
adapter = new ArrayAdapter<Question>( this, R.layout.questions_list, questions);
setListAdapter(adapter);
Thanks!
Ahh. You are doing the correct thing using a compound drawable. Not sure if there is a better way to maybe have the spacing in your compound drawable expand, but I know this'll work.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<View
android:layout_height="64dip"
android:layout_width="64dip"
android:background="#drawable/arrow_button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
Basically just pointing out using the align parent right and left. You may want to add some margins or padding to them. Also make sure to vertically center your elements.
With the comment and advice that Frank Sposaro gave, you will be able to position your views correctly.
For your next problem, I advice you to make your own adapter similar to this:
private class CustomAdapter extends ArrayAdapter<Question> {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
super(context, R.layout.row);
mInflater = LayoutInflater.from(context);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.mTextView);
holder.image = (ImageView) convertView.findViewById(R.id.mImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill the views in your row
holder.text.setText(questions.get(position).getText());
holder.image.setBackground... (questions.get(position).getImage()));
return convertView;
}
}
static class ViewHolder {
TextView text;
ImageView image;
}
In your onCreate:
ListView mListView = (ListView) findViewById(R.id.mListView);
mListView.setAdapter(new CustomAdapter(getApplicationContext(), questions));
Another example for a ListView with an Adapter can be found here

Add a new item to a listview

I am not quite sure my question really has something to do with listview.There is an app named Gleeo Time Tracker ,and here has a screenview of it.When you press the symbol "+",a new item will be created,and you can delete one item by pressing the "-".More is that when I click the record button on the left of the item,the background will change.
My question is,what is it in the end,a listview? How can I achieve such thing?Thank you all!
What you want to do, is build a custom ListView.
This is done by providing a layout file, for example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<ImageView android:id="#+id/Plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<TextView android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
</LinearLayout>
This layout is then applied to each row of your ListView by overriding its Adapter's getView() method, for example something like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
ImageView plus = (ImageView) v.findViewById(R.id.plus);
icon.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.plus));
TextView title = (TextView) v.findViewById (R.id.Browse_FileName);
// add a listener to your button and you're done
return v;
}
read about list view in developer docs and here is an example

Categories

Resources