This question already has answers here:
How to group RadioButton from different LinearLayouts?
(21 answers)
Closed 5 years ago.
I have a group of radiobuttons in a radiogroup that refuses to behave the way I'd like. The radiobuttons need to be two lines of two, and one line of one, with the last button having an edittext instead of a label. The layout looks correct, but the radiobuttons allow more than one to be selected and won't deselect any.
Below is my XML:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgDesignation">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT1">
<RadioButton
android:text="#string/pressure_equipment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbPE"
android:layout_weight="1" />
<RadioButton
android:text="#string/assemblies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbAss"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llVT2">
<RadioButton
android:text="#string/unheated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbUnh"
android:layout_weight="1" />
<RadioButton
android:text="#string/heated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rbH"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/llVT3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rbOwnSel"
android:layout_weight="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/etOwnChoice"
android:layout_weight="1"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:elevation="5dp" />
</LinearLayout>
</RadioGroup>
Make custom RadioGroup class like this:
public class RadioGroup extends LinearLayout {
private int mCheckedId = -1;
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
private boolean mProtectFromCheckedChange = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
private PassThroughHierarchyChangeListener mPassThroughListener;
public RadioGroup(Context context) {
super(context);
setOrientation(VERTICAL);
init();
}
public RadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
#Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
mPassThroughListener.mOnHierarchyChangeListener = listener;
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mCheckedId != -1) {
mProtectFromCheckedChange = true;
setCheckedStateForView(mCheckedId, true);
mProtectFromCheckedChange = false;
setCheckedId(mCheckedId);
}
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof RadioButton) {
final RadioButton button = (RadioButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
public void check(#IdRes int id) {
// don't even bother
if (id != -1 && (id == mCheckedId)) {
return;
}
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
if (id != -1) {
setCheckedStateForView(id, true);
}
setCheckedId(id);
}
private void setCheckedId(#IdRes int id) {
mCheckedId = id;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
#IdRes
public int getCheckedRadioButtonId() {
return mCheckedId;
}
public void clearCheck() {
check(-1);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
#Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
#Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof RadioGroup.LayoutParams;
}
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
#Override
public CharSequence getAccessibilityClassName() {
return RadioGroup.class.getName();
}
public static class LayoutParams extends LinearLayout.LayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
#Override
protected void setBaseAttributes(TypedArray a,
int widthAttr, int heightAttr) {
if (a.hasValue(widthAttr)) {
width = a.getLayoutDimension(widthAttr, "layout_width");
} else {
width = WRAP_CONTENT;
}
if (a.hasValue(heightAttr)) {
height = a.getLayoutDimension(heightAttr, "layout_height");
} else {
height = WRAP_CONTENT;
}
}
}
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// prevents from infinite recursion
if (mProtectFromCheckedChange) {
return;
}
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
int id = buttonView.getId();
setCheckedId(id);
}
}
private class PassThroughHierarchyChangeListener implements
ViewGroup.OnHierarchyChangeListener {
private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void traverseTree(View view) {
if (view instanceof RadioButton) {
int id = view.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
view.setId(id);
}
((RadioButton) view).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup viewGroup = (ViewGroup) view;
if (viewGroup.getChildCount() == 0) {
return;
}
for (int i = 0; i < viewGroup.getChildCount(); i++) {
traverseTree(viewGroup.getChildAt(i));
}
}
/**
* {#inheritDoc}
*/
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void onChildViewAdded(View parent, View child) {
traverseTree(child);
if (parent == RadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/**
* {#inheritDoc}
*/
public void onChildViewRemoved(View parent, View child) {
if (parent == RadioGroup.this && child instanceof RadioButton) {
((RadioButton) child).setOnCheckedChangeListener(null);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
}
and add in your layout like this:
<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:orientation="vertical"
android:padding="#dimen/activity_vertical_margin"
>
<com.example.admin.myapplication.RadioGroup
android:id="#+id/radio_group_plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<RadioButton
android:id="#+id/rb_latte"
android:text="radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<RadioButton
android:text="radio2"
android:id="#+id/rb_latte1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_mocha"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_mocha"
android:paddingLeft="20dp"
android:text="Mocha" />
<RadioButton
android:id="#+id/rb_mocha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_americano"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_americano"
android:paddingLeft="20dp"
android:text="Americano" />
<RadioButton
android:id="#+id/rb_americano"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="#+id/iv_espresso"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_espresso"
android:paddingLeft="20dp"
android:text="Espresso" />
<RadioButton
android:id="#+id/rb_espresso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E0E0E0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1">
<ImageView
android:id="#+id/iv_orange"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_orange"
android:paddingLeft="20dp"
android:text="Orange" />
<RadioButton
android:id="#+id/rb_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#E0E0E0" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:paddingLeft="10dp">
<ImageView
android:id="#+id/iv_butter"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/iv_butter"
android:paddingLeft="20dp"
android:text="Butter" />
<RadioButton
android:id="#+id/rb_butter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
</com.example.admin.myapplication.RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#138BE8"
android:gravity="center"
android:onClick="onOrderClicked"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Order Drink"
android:textColor="#android:color/white" />
Related
Hi in my application i am using the Viewpager with PagerAdapter. it contains n number of images. but when i rotate the device when viewpager is on first image then the image shift to top left corner and appears very small. please have a look in the attached image.
code which i am using
public class CustomViewPager extends ViewPager {
private final long SWITCH_TIME_INTERVAL = 5000;
public CustomViewPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomViewPager(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* this Runnable is use for automatically switching the view pager item.
* #author
*/
private Runnable mSwither = new Runnable() {
#Override
public void run() {
if( CustomViewPager.this.getAdapter() != null )
{
int count = CustomViewPager.this.getCurrentItem();
if( count == (CustomViewPager.this.getAdapter().getCount() - 1) )
{
count = 0;
}else
{
count++;
}
//Log.d(this.getClass().getName(), "Curent Page " + count + "");
CustomViewPager.this.setCurrentItem(count, true);
}
CustomViewPager.this.postDelayed(this, SWITCH_TIME_INTERVAL);
}
};
#Override
public boolean onTouchEvent(MotionEvent arg0) {
switch (arg0.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP :
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
break;
default:
removeCallbacks(mSwither);
break;
}
return super.onTouchEvent(arg0);
} }
//PagerAdapter onInstantiateItem code..
#Override
public Object instantiateItem(ViewGroup container, final int position) {
mImageFetcher.setLoadingImage(R.drawable.placeholder_tabportrait);
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.test_fragment1, container, false);
description = (TextView) itemView.findViewById(R.id.description);
title = (TextView) itemView.findViewById(R.id.car_title);
published = (TextView) itemView.findViewById(R.id.date_data);
author = (TextView) itemView.findViewById(R.id.author);
jumbo_tron_image = (ImageView) itemView.findViewById(R.id.jumbo_imgae);
TrayItem trayItem = getData(position);
if (null != trayItem) {
if (trayItem.getType() != null) {
if (trayItem.getImages() != null) {
mImageFetcher.loadImage(trayItem.getImages().getWidget(),
jumbo_tron_image);
}
return itemView;
}
//xml 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="500dp"
android:background="#color/white">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical"
>
<ImageView
android:id="#+id/jumbo_imgae"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="300dp"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/container_height"
android:layout_below="#+id/main_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:layout_weight="0.5"
android:textColor="#color/black"
android:id="#+id/car_title"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp"
android:text=" "/>
<ImageView
android:id="#+id/iv_add_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:paddingTop="10dp"
android:src="#drawable/add_icon"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/car_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/linearLayout3"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/date_data"
android:layout_width="90dp"
android:layout_height="18dp"
android:text=""
android:textSize="13sp"
android:layout_gravity="left"
android:gravity="center"
android:textColor="#color/white"
android:background="#drawable/date_bg"/>
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:layout_marginLeft="10dp"
android:textColor="#color/black"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </RelativeLayout>
Sorry for my english. I spend many times but i cant fix my problem. I have listView and i want set setOnItemClickListener him. This is my listView:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "#+id/listScene"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
>
</ListView>
Its my code
public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
but when i click in item its nothing happens
UPD
My item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:focusable="false"
android:focusableInTouchMode="false"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<service.SwipeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:background="#32777777"
android:layout_width="match_parent"
android:layout_height="70dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_weight="0.7"
>
<TextView
android:id="#+id/conditions"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text=""
android:textColor="#25ac36"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/description"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=""
android:textColor="#222222"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:background="#66777777"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="#+id/rooms"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=""
android:textColor="#fff"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="250dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/open_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:background="#4fcc54">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Open"
android:textColor="#fff"
android:id="#+id/openText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/disable_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:background="#8e8e8e">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Disable"
android:textColor="#fff"
android:id="#+id/disableT"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/delete_b"
android:clickable="true"
android:layout_width="78dp"
android:layout_height="match_parent"
android:background="#de5454">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Delete"
android:textColor="#fff"
android:id="#+id/deleteText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</service.SwipeLayout>
</LinearLayout>
UPD: simple listView
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"
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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true" />
</RelativeLayout>
my main
ArrayList<String> asd = new ArrayList<>();
for(int i = 0; i < 10; i++) {
asd.add(String.valueOf(i));
}
ListView listView = (ListView) findViewById(R.id.listView);
Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, asd);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
and item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView" />
</LinearLayout>
Its good, its work when i click in item listView, Toast is show position. But i need use SwipeLayout then i change code item.xml and add this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:descendantFocusability="blocksDescendants"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.bottom.smart.detetethis.SwipeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:background="#32777777"
android:layout_width="match_parent"
android:layout_height="70dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_weight="0.7"
>
<TextView
android:id="#+id/conditions"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text="Active"
android:textColor="#25ac36"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/description"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Turn light on \n when people arrive"
android:textColor="#222222"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:background="#66777777"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="#+id/rooms"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Livingroom, Kitechen, Rest room"
android:textColor="#fff"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="250dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/open_b"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:background="#4fcc54">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Open"
android:textColor="#fff"
android:id="#+id/openText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/disable_b"
android:layout_width="78dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:background="#8e8e8e">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Disable"
android:textColor="#fff"
android:id="#+id/disableT"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/delete_b"
android:layout_width="78dp"
android:layout_height="match_parent"
android:background="#de5454">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Delete"
android:textColor="#fff"
android:id="#+id/deleteText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</com.bottom.smart.detetethis.SwipeLayout>
</LinearLayout>
got sick, its not working when i click item listView. But when I get a finger between the clearance item, its work:
to blame SwipeLayout, code SwipeLayout:
public class SwipeLayout extends LinearLayout {
private ViewDragHelper viewDragHelper;
private View contentView;
private View actionView;
private int dragDistance;
private final double AUTO_OPEN_SPEED_LIMIT = 400.0;
private int draggedX;
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback());
}
#SuppressLint("MissingSuperCall")
#Override
protected void onFinishInflate() {
contentView = getChildAt(0);
actionView = getChildAt(1);
actionView.setVisibility(GONE);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
dragDistance = actionView.getMeasuredWidth();
}
private class DragHelperCallback extends ViewDragHelper.Callback {
#Override
public boolean tryCaptureView(View view, int i) {
return view == contentView || view == actionView;
}
#Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
draggedX = left;
if (changedView == contentView) {
actionView.offsetLeftAndRight(dx);
} else {
contentView.offsetLeftAndRight(dx);
}
if (actionView.getVisibility() == View.GONE) {
actionView.setVisibility(View.VISIBLE);
}
invalidate();
}
#Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == contentView) {
final int leftBound = getPaddingLeft();
final int minLeftBound = -leftBound - dragDistance;
final int newLeft = Math.min(Math.max(minLeftBound, left), 0);
return newLeft;
} else {
final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance;
final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight();
final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound);
return newLeft;
}
}
#Override
public int getViewHorizontalDragRange(View child) {
return dragDistance;
}
#Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
boolean settleToOpen = false;
if (xvel > AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = false;
} else if (xvel < -AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = true;
} else if (draggedX <= -dragDistance / 2) {
settleToOpen = true;
} else if (draggedX > -dragDistance / 2) {
settleToOpen = false;
}
final int settleDestX = settleToOpen ? -dragDistance : 0;
viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(viewDragHelper.shouldInterceptTouchEvent(ev)) {
return true;
}
return super.onInterceptTouchEvent(ev);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
#Override
public void computeScroll() {
super.computeScroll();
if(viewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
}
I can not delete it, because I need it in the project. I do not know how to fix this situation
Sometimes the ListView click doesn't work. And at this case you might have to add one more attribute.
android:descendantFocusability="blocksDescendants"
Add this attribute to the top most layout of your XML where you have provided the ListView elements.
In your ListView remove the xml attribute android:clickable="true"
This is taking your click event and not sending it down to the child views.
Edit
You are passing in getApplicationContext() which is the wrong context. The listener does work, but the Toast is not displaying in the right context. You need to pass in getContext(). However, since this is an Activity, you can just pass in this instead.
public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(this, R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
I have tried to scroll the ListView up when the event item is more than the space of the ListView but it fails. I would like to know if I can make the ListView scrollable without adding any listener. Thank you.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:focusableInTouchMode="true"
tools:context="com.example.ssycorpapp.SecondActivity" >
<TabHost
android:id="#+id/tabhost2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/events"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SearchView
android:id="#+id/search"
android:queryHint="#string/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</SearchView>
<RelativeLayout
android:id="#+id/titleBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_below="#id/search" >
<TextView
android:id="#+id/resultName"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/event"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/resultDate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/resultTime"
android:layout_toStartOf="#+id/resultTime"
android:text="#string/eventDate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#id/resultTime"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/eventTime"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<ListView
android:id="#+id/event_list"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="#id/titleBar"
android:layout_above="#+id/DelAll" />
<Button
android:id="#+id/DelAll"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:text="#string/DelAll" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/EventName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/event" />
<EditText
android:id="#+id/eveName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
</EditText>
<TextView
android:id="#+id/EventDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/eventDate" />
<DatePicker
android:id="#+id/datePicker1"
android:layout_width="match_parent"
android:layout_height="158dp" />
<TextView
android:id="#+id/EventTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/eventTime" />
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="109dp" />
<Button
android:id="#+id/Tim"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Set" />
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
If you don't want to use any listener then you can simply use a custom ListView like this,
public class CustomListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
private int listViewTouchAction;
public CustomListView (Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
Then use in your layout,
<CustomListView
android:id="#+id/event_list"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="#id/titleBar"
android:layout_above="#+id/DelAll" />
I wanna make footer buttons in android one way is to simply make buttons and align them to bottom but I want the footer like in Facebook android app whenever we drag screen down three buttons appears for status , photo , checkin.
How to do this ??
To get a list like the following image, create a layout.xml as follows after the sample image
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header" >
<LinearLayout
android:id="#+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#drawable/button_bg_drawable" >
<ImageView
android:id="#+id/imageView0"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical|left"
android:background="#drawable/button_bg_drawable"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:src="#drawable/back_btn_small" />
</LinearLayout>
<EditText
android:id="#+id/headerText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#android:drawable/editbox_background_normal"
android:editable="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="normal" />
<AutoCompleteTextView
android:id="#+id/filterNewProject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#drawable/bg_input_blue"
android:completionThreshold="1"
android:hint="Search for a locality, developer or project"
android:textColor="#color/black"
android:textSize="14sp"
android:visibility="gone" />
<Button
android:id="#+id/clearAutoCompleteList"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/custom_button_clear"
android:paddingLeft="20dp"
android:visibility="gone" />
<Button
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#drawable/seaerch_glass" />
<View
android:id="#+id/sep_header"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#id/tabBar"
android:background="#d5d5d5"
android:visibility="visible" />
</RelativeLayout>
<include
android:id="#+id/footerLayout"
layout="#layout/post_requirement_footer"
android:visibility="gone" />
<ListView
android:id="#+id/projectsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:divider="#color/white"
android:dividerHeight="1.5dp" >
</ListView>
<RelativeLayout
android:id="#+id/zeroResultsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:visibility="gone" >
<ImageView
android:id="#+id/emptyIllustration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/no_results_illustration" />
</RelativeLayout>
<com.housing.utils.QuickReturnRelativeLayoutFooter
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#color/transparent" >
<RelativeLayout
android:id="#+id/bottomListViewContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/footer_bg" >
<ImageButton
android:id="#+id/filterButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/filter_icon" />
<ImageButton
android:id="#+id/subscribeButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/subscribe_iphone"
android:visibility="visible" />
<TextView
android:id="#+id/resultsText"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ellipsize="end"
android:paddingTop="10dp"
android:scrollHorizontally="false"
android:singleLine="false"
android:text=""
android:textColor="#color/black"
android:textSize="15dp"
android:visibility="visible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/resultsText"
android:paddingRight="5dp"
android:paddingTop="15dp"
android:src="#drawable/filter_normal"
android:visibility="gone" />
</RelativeLayout>
</com.housing.utils.QuickReturnRelativeLayoutFooter>
</RelativeLayout>
Here is the Class com.housing.utils.QuickReturnRelativeLayoutFooter
PS : Replace com.housing.utils with your package name
package com.housing.utils;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.RelativeLayout;
public class QuickReturnRelativeLayoutFooter extends RelativeLayout implements
OnScrollListener {
private class ChildDescriptor {
public int index;
public int total;
public int drawable;
public ChildDescriptor(int index) {
this.index = index;
}
public int getApproximateScrollPosition() {
return index * total + (total - drawable);
}
}
public int MAX_HEIGHT_DP =60;
public int MIN_HEIGHT_DP = 0;
public static final int SCROLL_DIRECTION_INVALID = 0;
public static final int SCROLL_DIRECTION_UP = 1;
public static final int SCROLL_DIRECTION_DOWN = 2;
private int direction = SCROLL_DIRECTION_INVALID;
private ChildDescriptor lastchild;
private OnScrollListener onscrolllistener;
private int maxheight;
private int minheight;
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs) {
super(context, attrs);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context) {
super(context);
calculateMinMax();
}
private void calculateMinMax() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
maxheight = (int) (metrics.density * (float) MAX_HEIGHT_DP);
minheight = (int) (metrics.density * (float) MIN_HEIGHT_DP);
}
private void adjustHeight(int howmuch, int max, int min) {
if ((howmuch < 0) && (direction != SCROLL_DIRECTION_UP)) {
direction = SCROLL_DIRECTION_UP;
return;
} else if ((howmuch > 20) && (direction != SCROLL_DIRECTION_DOWN)) {
direction = SCROLL_DIRECTION_DOWN;
return;
}
int current = getHeight();
current += howmuch;
if (current < min) {
current = min;
} else if (current > max) {
current = max;
}
RelativeLayout.LayoutParams f = (RelativeLayout.LayoutParams) getLayoutParams();
if (f.height != current) {
f.height = current;
setLayoutParams(f);
}
if (direction == SCROLL_DIRECTION_UP
&& Math.abs(f.topMargin) <= current) {
// if (f.topMargin != howmuch) {
//
// f.topMargin = howmuch + f.topMargin;
//
// if (f.topMargin > 0) {
// f.topMargin = -f.topMargin - 10;
// }
//
// }
// mBottomListViewContainer.setVisibility(View.GONE);
f.bottomMargin = -100;
setLayoutParams(f);
} else if (direction == SCROLL_DIRECTION_DOWN) {
if (f.bottomMargin != 0) {
f.bottomMargin = 0;
setLayoutParams(f);
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
ChildDescriptor currentchild = getFirstChildItemDescriptor(view,
firstVisibleItem);
try {
adjustHeight(lastchild.getApproximateScrollPosition()
- currentchild.getApproximateScrollPosition(), maxheight,
minheight);
lastchild = currentchild;
} catch (NullPointerException e) {
lastchild = currentchild;
} catch (Exception e) {
}
if (onscrolllistener != null) {
onscrolllistener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
private ChildDescriptor getFirstChildItemDescriptor(AbsListView view,
int index) {
ChildDescriptor h = new ChildDescriptor(index);
try {
Rect r = new Rect();
View child = view.getChildAt(0);
child.getDrawingRect(r);
h.total = r.height();
view.getChildVisibleRect(child, r, null);
h.drawable = r.height();
return h;
} catch (Exception e) {
}
return null;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onscrolllistener != null) {
onscrolllistener.onScrollStateChanged(view, scrollState);
}
}
public void attach(AbsListView view) {
view.setOnScrollListener(this);
}
public void setOnScrollListener(OnScrollListener l) {
onscrolllistener = l;
}
}
I Have made it as a widget and Now finally to add this to your List View use
frame = (QuickReturnRelativeLayoutFooter) newProjectsView
.findViewById(R.id.frame);
frame.attach(projectsList);
where projectList is your List View
example
ListView projectList =(ListView)findViewById(R.id.projectList);
and Voila Cheers Completed Smooth as Heaven .......
I am trying to make something like customised quick badge with some buttons on it. I have successfully desgined it but when i am clicking on those buttons it does nothing. Can someone tell me where am i going wrong.
demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/likemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<Button
android:id="#+id/likequickaction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
</LinearLayout>
popup_grid_layout.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" >
<FrameLayout
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip"
android:background="#drawable/quickaction_top_frame" />
<ImageView
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/quickaction_arrow_up" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header2"
android:background="#drawable/quickaction_slider_background"
android:fadingEdgeLength="0.0dip"
android:paddingLeft="1.0dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_left" />
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/api_buttons" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_right" />
</LinearLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scroll"
android:background="#drawable/quickaction_bottom_frame" />
</RelativeLayout>
api_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/quickaction_slider_background" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/one"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="One" />
<Button
android:id="#+id/two"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Two" />
<Button
android:id="#+id/three"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Three" />
<Button
android:id="#+id/four"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Four" />
<Button
android:id="#+id/five"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Five" />
<Button
android:id="#+id/six"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Six" />
</TableRow>
</HorizontalScrollView>
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext()
.getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
protected void onCreate() {
}
protected void onShow() {
}
private void preShow() {
if (this.root == null) {
throw new IllegalStateException(
"setContentView was not called with a view to display.");
}
onShow();
if (this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);
this.window.setContentView(this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ this.anchor.getWidth(), location[1] + this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
// display on bottom
if (rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
}
LikeQuickActionDemo.java
public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.demo_layout);
this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
this.likemenuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
this.likequickactionButton = (Button) this
.findViewById(R.id.likequickaction);
this.likequickactionButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu(0, 200);
}
});
}
private static class DemoPopupWindow extends BetterPopupWindow implements
OnClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
protected void onCreate() {
// inflate layout
LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
// setup button events
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
View item = row.getChildAt(j);
if (item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}
// set the inflated view as what we want to display
this.setContentView(root);
}
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(),
Toast.LENGTH_SHORT).show();
this.dismiss();
}
}
}
The problem is in DemoPopupWindow.onCreate method:
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
root will be RelativeLayout defined in popup_grid_layout.xml. Later you iterate through all its children and set onclick listeners only if child view is TableRow, which is never the case. I guess what you want there is to find horizontalScrollView1 view and do the same with its children instead, something like:
View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
View v = horizontalScrollView1.getChildAt(i);
if (v instanceof TableRow) {
...