what is wrong with this layout, double clicks - android

I was trying to do something similar to this app List
But this was too hard for me, I can't understand very well the layout system, so I ended up doing something in RecyclerView with rectangular and imagebuttons in the left and two on the right. All buttons will have some functions. If you click the star it will add/remove it from favorites, the download button will download it, and the right buttons are to delete and update.
I added the function for clickListener using this code from google
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}
and linked the Listener doing this:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "Item CLICKED";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
it show Item Clicked even when i click on the ImageButton(i setted another function to it), both actions are called...
how can i make it separete the clicks?
But I think the main problem is my .xml, can someone look and see what is wrong, please? I added a lot of relatives, one is for border, another for background, and the third has the items inside =x.
Here's the item_list for the recycler.
<?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:paddingTop="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border"
android:padding="1dp"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:padding="5dp"
android:background="#android:color/white"
android:weightSum="1"
>
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:background="#android:color/white"
android:layout_marginRight="5dip">
<ImageButton
android:id="#+id/status"
android:background="#android:color/white"
android:layout_width="25dip"
android:layout_height="25dip"
/>
</LinearLayout>
<TextView
android:id="#+id/titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:background="#android:color/white"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#343434"
android:textSize="10dip"
android:layout_toRightOf="#+id/thumbnail"
android:background="#android:color/white"
android:layout_below="#+id/titulo" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:paddingRight="10dp"
android:id="#+id/delete"
android:layout_toLeftOf="#+id/update"
android:padding="5dip"
android:onClick="deletarLei" />
<ImageButton android:layout_width="wrap_content"
android:id="#+id/update"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="5dip"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

When I have similar problems, a tag on the top most ViewGroup in your layout usually solves the problem. In your case this would be the top most ViewGroup in each cell/row.
android:descendantFocusability="beforeDescendants"
You can even try setting this on your RecyclerView. There are only 3 options, from what I remember beforeDescendants should work, otherwise give the other 2 a try.
I think you can just put a OnClickListener on the image in your Adapter and just the onItemClickListener on the recycle view. Its not really clear from your code what you're doing.

Related

Swipe to Add view in android

I want to achieve something like the below gif:
What I have done so far:
I am using a swipelayout using I am able to implement the part where if you swipe the row item, it shows you the "Add to Cart" icon but I am not able to implement the animation where it zooms in the cart icon and add it to cart (I am not able to create a hook where I can call the addToCart function).
Below is my code:
Row XML File:
<RelativeLayout
android:id="#+id/swipe_view"
android:layout_width="120dp"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:minWidth="96dp">
<ImageView
android:id="#+id/add_to_cart"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_margin"
android:layout_marginStart="#dimen/activity_margin"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:padding="#dimen/activity_margin"
android:scaleType="center"
android:src="#drawable/ic_menu_cart"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/restaurant_menu_details_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/half_activity_margin"
android:paddingLeft="#dimen/activity_margin"
android:paddingRight="#dimen/activity_margin"
android:paddingTop="#dimen/half_activity_margin">
<ImageView
android:id="#+id/item_image"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
tools:src="#drawable/placeholder_restaurant"/>
<TextView
android:id="#+id/item_price"
style="#style/secondary_tv"
fontPath="#string/ubuntu_light"
android:layout_width="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="#dimen/half_activity_margin"
android:layout_marginLeft="#dimen/half_activity_margin"
android:gravity="center_horizontal"
android:maxLines="2"
tools:text="5KD"/>
<LinearLayout
android:id="#+id/restaurant_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/item_image"
android:layout_toLeftOf="#+id/item_price"
android:layout_toRightOf="#+id/item_image"
android:layout_toStartOf="#+id/item_price"
android:orientation="vertical">
<TextView
android:id="#+id/item_name"
fontPath="#string/ubuntu_mono_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/half_activity_margin"
android:layout_marginTop="#dimen/half_activity_margin"
android:gravity="start"
android:textAllCaps="true"
android:textColor="#color/black_primary_text_color"
android:textSize="#dimen/medium_text"
tools:text="Restaurant Name"/>
<TextView
android:id="#+id/item_desc"
style="#style/secondary_tv"
fontPath="#string/ubuntu_light"
android:layout_marginBottom="#dimen/half_activity_margin"
android:layout_marginLeft="#dimen/half_activity_margin"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:maxLines="2"
tools:text="Restaurant Description, max 1 line"/>
</LinearLayout>
</RelativeLayout>
Java Code
from RecyclerView Adapter:
SwipeLayout swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
#Override
public void onStartOpen(SwipeLayout layout) {
}
#Override
public void onOpen(SwipeLayout layout) {
}
#Override
public void onStartClose(SwipeLayout layout) {
}
#Override
public void onClose(SwipeLayout layout) {
}
#Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
// i tried leveraging the leftOffset which tells me the position of the //layout on the screen but because it is called multiple times, It was //adding many entries to the cart in just one swipe :(
}
#Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
I would really appreciate if anyone can suggest a better way to fix this or any open-source library which does such a thing.
Many thanks!
PS: Please add a comment if you need more information from me in order to help me. :)
what I'll write is a best guess based on the information you gave, but it seems that it's all right there:
#Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
float percent = ((float)leftOffset/((float)totalWidth));
// use `percent` to animate the icon, something like:
icon.setAlpha(percent);
}
#Override
public void onOpen(SwipeLayout layout) {
// here is the swipe fully open
addToCart(item); // create the method
layout.close(); // close the layout again
}

setOnItemClickListener doesn't work when setOnLongClickListener is being used - Android

As the subject suggests, setOnItemClickListener doesn't work when setOnLongClickListener is being used, the layout I'm using in the base adapter is below,
<?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"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="20dp"
android:paddingTop="5dp">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:focusable="false"
app:border_color="#EEEEEE"
app:border_width="4dp"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/contacts_grid_image"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="11dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Mobile number"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="9dp"/>
</RelativeLayout>
and I'm using this,
image.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("test", mimeTypes, item);
MyDragShadowBuilder myShadow = new MyDragShadowBuilder(holder.image);
if (groups.size() > holder.position) {
v.startDrag(dragData, myShadow, null, 0);
return true;
}
});
and this,
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "clicked");
}
});
Now when I tap on the image the item click doesn't work, but when i click on the text views it works, what am i doing wrong?
Also note that when I remove the image.setOnLongClickListener() the whole gridview item becomes clickable again.
This should be the expected behaviour for GridView, because the child view is clickable, even though it is only handling the OnLongClick only.
As a workaround, you can
setOnClickListener for ImageView to perform the same action as you would in setOnItemClickListener
consider using setOnItemLongClickListener, but this would affect the whole RelativeLayout
use onTouchListener, but this would requires more works

TextView and Button in ListView - OnClick works, OnItemClick does not work

UPDATE
My button is as big as the view of each ListItem - maybe this causes the problem? Is it possible to press once and let both OnItemclick and OnClick respond?
I have a list view with two textviews and a button. I know there are a lot of questions being posted but I have read a lot of them and not one seems to work.
I want the OnItemClick of each view of the list view to work.
I also want the OnClick of the button inside each view of the list view to work.
Only the onClick of the button seems to respond
In my xml i have set android:focusable="false" for the button. For the textviews I have set android:textIsSelectable="false" android:clickable="false" android:focusable="false"
<?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:background="#drawable/rectangle_order"
android:layout_gravity="center_horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/order_height"
>
<Button
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="#dimen/text_size"
android:textColor="#color/white"
android:rotation="90"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/timer_size"
android:rotation="90"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="29dp"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
this is the layout for my list view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.5"
android:layout_width="#dimen/order_height"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none"
>
</ListView>
</RelativeLayout>
In the GetView of my adapter i have :
Button b = (Button) rowView.findViewById(R.id.img);
b.setBackgroundResource(R.color.pending);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
});
in my main activity i have a onItemClickListener set to the listView:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG_MAIN, "we are in select onitemclicklistener");
Toast.makeText(getApplicationContext(),"BOOOOH", Toast.LENGTH_SHORT).show();
}
});
Try to replace OnClickListener with OnTouchListener.
Like this:
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
return false;
}
};);
When you return false at this listener, you mean "hey i'm not done with that action(press down), please let the action to the next View"

OnItemClickListener and OnClickListener not working for ListView

I have used a Custom ListView and I am displaying some data using the same ListView. When I click on the List View item, the onClickListener is not getting called. I am not able to select any list item.
Layout Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp"
android:background="#drawable/list_selector"
android:clickable="true"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/imgProperty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher"
android:focusable="false"/>
</LinearLayout>
<TextView
android:id="#+id/tvCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="75dip"
android:layout_toRightOf="#+id/list_image"
android:paddingBottom="10dip"
android:text="property"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/tvprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgProperty"
android:layout_alignLeft="#+id/tvCity"
android:text="Price"
android:focusable="false"/>
</RelativeLayout>
Adapter code:
public class CustomListAdapter extends BaseAdapter {
ArrayList<Propety> PropertiesArray;
private LayoutInflater Inflater;
public CustomListAdapter(ArrayList<Propety> PropertiesArray) {
this.PropertiesArray=PropertiesArray;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PropertiesArray.size();
}
#Override
public Object getItem(int position) {
return PropertiesArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.customlistview, parent, false);
}
final Propety ListArray = PropertiesArray.get(position);
TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity);
tvPropertyName.setText(ListArray.getName());
TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice);
tvPrice.setText(ListArray.getPrice());
ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image);
imgProperty.setImageResource(R.drawable.ic_launcher);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
ListView code:
ListView propertylistview = (ListView) findViewById(R.id.listview);
CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray);
propertylistview.setAdapter(customlistview);
ListView XML:
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
custom.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" >
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txtangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="115dp"
android:layout_marginLeft="95dp"
android:text="" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
</ListView>
<view
android:id="#+id/customview"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.example.samplebuapp.CustomCompass" />
<view
android:id="#+id/view1"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_above="#+id/listview"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
class="com.example.samplebuapp.CustomView" />
<LinearLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/listview"
android:orientation="vertical"
android:focusable="false">
</LinearLayout>
</RelativeLayout>
Even scrolling is not working.
I am unable to figure out why is this happening? Am I missing out something?
Any help in resolving this is appreciated.
The following will do the job in your case.
ListView propertylistview = (ListView) findViewById(R.id.listview);
propertylistview.setOnItemClickListener( myListViewClicked ):
OnItemClickListener myListViewClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show();
}
};
Dont forget to remove the following from the CustomAdapter
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
If the touch events are getting intercepted inside the cell layout, in the layout for your custom cell, set
android:descendantFocusability="blocksDescendants" on the top level layout of your cell. For me, it was too much of a pain to add xml attributes to every individual view.
Note that this can also be set in code:
cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
I also tried this with a ListView inside a cell, and I had to override onTouchEvent to get it to work:
public class NoTouchListView extends ListView {
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
the simple code that solved my problem, and method view.setOnClickListener be within my custom adapter
view.setFocusable(false)
Check your list row layout once :
layout or any view should not be `android:clickable="true" and
android:focusable="true"
if it is there just delete and run the application again.
Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,
Added the following properties to the checkbox,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and ItemClickListner started working.
For detailed example you can go through the link,
http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/
Hope it helps Cheers!!
For TextView you should also use
android:textIsSelectable="false"
https://developer.android.com/reference/android/widget/TextView.html#setTextIsSelectable

How to make android listview scrollable?

I have two listviews, but they don't scroll. How do I correct this?
Here is my layout.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="wrap_content"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/backgrund" >
<!-- Header Starts -->
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:background="#layout/header" >
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<TextView
android:id="#+id/textAD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/header"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:layout_marginRight="26dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_gravity="center_horizontal"
android:focusable="false"
android:paddingBottom="5px"
android:paddingTop="10px"
android:src="#android:drawable/divider_horizontal_bright" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000"
android:focusable="false" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:orientation="vertical" >
<TextView
android:id="#+id/textm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Malzemeler"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:id="#+id/editaramalzeme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/btnmalzlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:text="Ara" />
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<ListView
android:id="#+id/listsecili"
android:layout_width="wrap_content"
android:layout_height="210dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<EditText
android:id="#+id/txtNot"
android:layout_width="match_parent"
android:layout_height="88dp"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="6"
android:singleLine="false" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/btnkaydet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="malzeme ekle" />
<Button
android:id="#+id/btntoplugonder"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textAD"
android:layout_below="#+id/btnkaydet"
android:text="toplu gonder" />
</RelativeLayout>
</ScrollView>**
Never put ListView in ScrollView. ListView itself is scrollable.
By default ListView is scrollable. Do not put ScrollView to the ListView
I know this question is 4-5 years old, but still, this might be useful:
Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. That's because the operating system doesn't view it as actually exceeding the screen.
I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. And it was a real mystery. As soon as I added a few more, it started to scroll.
So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". It did, for me.
Practically its not good to do. But if you want to do like this, just make listview's height fixed to wrap_content.
android:layout_height="wrap_content"
Listview so have inbuild scrolling capabilities. So you can not use listview inside scrollview. Encapsulate it in any other layout like LinearLayout or RelativeLayout.
This is my working code. you may try with this.
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:background="#ffffff">
<TextView android:id="#+id/tvEmpId"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.3"/>
<TextView android:id="#+id/tvNameEmp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.5"/>
<TextView
android:layout_height="wrap_content"
android:id="#+id/tvStatusEmp"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.2"/>
</LinearLayout>
details.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/page_bg"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lLayoutGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/page_bg"
android:orientation="vertical" >
................... others components here............................
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alwaysDrawnWithCache="true"
android:dividerHeight="1dp"
android:horizontalSpacing="3dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:verticalSpacing="3dp"
android:layout_marginBottom="30dp">
</ListView>
</LinearLayout>
</RelativeLayout>
Adapter class :
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<EmployeeBean> employeeList;
publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
this.context = context;
this.employeeList = employeeList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
EmployeeBeanHolder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
holder = new EmployeeBeanHolder();
holder.employeeBean = employeeList.get(position);
holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);
row.setTag(holder);
holder.tvEmpId.setText(holder.employeeBean.getEmpId());
holder.tvName.setText(holder.employeeBean.getName());
holder.tvStatus.setText(holder.employeeBean.getStatus());
if (position % 2 == 0) {
row.setBackgroundColor(Color.rgb(213, 229, 241));
} else {
row.setBackgroundColor(Color.rgb(255, 255, 255));
}
return row;
}
public static class EmployeeBeanHolder {
EmployeeBean employeeBean;
TextView tvEmpId;
TextView tvName;
TextView tvStatus;
}
#Override
public int getCount() {
return employeeList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
employee bean class:
public class EmployeeBean {
private String empId;
private String name;
private String status;
public EmployeeBean(){
}
public EmployeeBean(String empId, String name, String status) {
this.empId= empId;
this.name = name;
this.status = status;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId= empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status =status;
}
}
in Activity class:
onCreate method:
public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();
//create emplyee data
for(int i=0;i<=10;i++) {
EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
EMPLOYEE_LIST .add(emplyee );
}
ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
Putting ListView inside a ScrollView is never inspired.
But if you want your posted XML-like behavior, there're 3 options to me:
Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).
Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)
If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect.
To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
I found a tricky solution... which works only in a RelativeLayout.
We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview
android:clickable="false" />
<View
android:layout_width="match_parent"
android:background="#drawable/gradient_white"
android:layout_height="match_parent"
android:clickable="true"
android:layout_centerHorizontal="true"
android:layout_alignTop="#+id/listview" />
I had this problem and I found the solution. In my case the listView was bigger than the screen, so the list was not big enough to need scroll, but I coudnĀ“t see it because half list was out of screen.
I solved it adding enough marginBotton to make the listView shorter and it suits to the screen.
<ListView
android:id="#+id/lvDatos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="300dp"/>

Categories

Resources