I am making an android application containing a listview which updates every 30 seconds. If there is new data, I want a button to appear where I can update the feed, similar to the facebook app. How can I accomplish this? Is there a way to overlay a button on top of a listview that appears at the top of the screen when not scrolling in the listview?
Here is my listview xml:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
/>
</LinearLayout>
<Button
android:id="#+id/update_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button" />
Try using the setOnScrollListener and implement the onScrollStateChanged.
setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//make your button invisible
button.setVisible(false);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//make your button visible.
button.setVisible(true);
}
}
});
Overlaying of views can be seen here.
Related
I have a listview in a constraintlayout.
when I try to do a long click on one of the elements of the list, to inflate the context menu, there are 2 problems:
1: It triggers the long click only sometimes, really random
2: After the long click was triggered the "normal" click is also triggered even though the onContextItemSelected returns true (to indicate the event was handled)
for some list elements, I want to have both the onClickListener and the long click for the contextmenu, on others only the contextmenu. (the listview is registered for contextmenu).
Heres the XML of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.netknights.piauthenticator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:backgroundTint="#color/PIBLUE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#drawable/ic_add_white_24dp" />
<TextView
android:id="#+id/countdownfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:backgroundTint="#color/PIBLUE"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/PIBLUE"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:popupTheme="#color/PIBLUE"
tools:layout_editor_absoluteY="-57dp" />
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/countdownfield"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
and the XML of the listentry
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/textViewToken"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="124891"
android:textSize="28sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="0dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<!-- android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"-->
<TextView
android:id="#+id/textViewLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="privacyidea something"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewToken"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewLabel"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.517"
/>
</android.support.constraint.ConstraintLayout>
the part of the MainActivity.java
final ListView listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(view.getContext(), "SHORT itemclick", Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(listview);
and the getview method from my custom adapter:
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
final ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
final Token token = getItem(position);
final TextView tmp2 = (TextView) v.findViewById(R.id.textViewToken);
final TextView tmp1 = (TextView) v.findViewById(R.id.textViewLabel);
if (token.getType().equals(HOTP)) {
progressBar.setVisibility(GONE);
} else {
progressBar.setVisibility(VISIBLE);
//v.setClickable(false);
}
progressBar.setTag(position);
progressBar.setMax(token.getPeriod());
progressBar.getProgressDrawable().setColorFilter(
Color.rgb(0x83, 0xc9, 0x27), android.graphics.PorterDuff.Mode.SRC_IN);
token.setPb(progressBar);
tmp1.setText(token.getLabel());
tmp2.setText(token.getCurrentOTP());
EDIT: thank you for taking your time to post answers.
Ive solved the/my problem: the rootelement of the listentries was a contraintlayout and that doesn't seem to work properly with the scenarios i want, so i changed it so a relativelayout and it now it works perfectly!
hi Cxc the problem might be due to listView registered for both onClick and onLongClick . So instead of setting the onClickListener for the entire list view try setting the onClick listener to only the item view for which you need both onClickListener and long click.
You can set onClickListener to particular views inside getView() method based on their position or view type.
for example,
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
if(position % 2 == 0)
{
v.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "SHORT itemclick",
Toast.LENGTH_SHORT).show();
}
});
hope this helps
I think you can use both onClick and onLongClick and override the onLongClick method making it returns true to avoid conflict of triggering onClick event
ex:
GridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
<do somthing here>
}
});
GridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, final int i, long l) {
<do something here>
return true;
}
});
I want item from ListView disappear when I touch it. It is working but
problem is when I scroll list. Random items are disappearing or showing.
This is my code:
private ListView list;
(...)
list = (ListView) findViewById(R.id.units_list);
adapter = new UnitAdapter(this, R.layout.row_list, units);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
v.setVisibility(View.INVISIBLE);
}
});
ListView XML:
<ListView
android:id="#+id/units_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/strut"
android:layout_marginTop="5dp" >
</ListView>
row_list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/img"
android:gravity="center_vertical"
android:textSize="14sp" />
<ImageView
android:id="#+id/upgrade"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical" />
I tried too:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
list.getChildAt(position).setVisibility(View.INVISIBLE);
}
And even:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
list.getChildAt((int) id).setVisibility(View.INVISIBLE);
}
But it works same.
What is the problem? Why View is related with multiple rows? How can I fix this?
It may be related to the fact that a view in a ListView is recycled in Android, so when you edit a row you may see the difference when you scroll in multiple rows.
One solution could be to update the underlying adapter and then call notifyDataSetChanged. But there may be smoother solutions.
Use View Holder design pattern.
when you are removing the item from List View, remove the same from list(in your case unit) also.
all my views and non focusable and non clickable except the switch which is clickable but making it non clickable still doesn't make the list view fire
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/timed_events_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/event_name"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textSize="20sp"
android:textStyle="bold"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
<TextView
android:id="#+id/event_time"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
</LinearLayout>
<Switch
android:id="#+id/state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="isActivated"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
below is the OnItemSelectedListener which is in the onCreate method
listView.setOnItemSelectedListener( new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ActivityContext, "its working", Toast.LENGTH_LONG).show();
Intent intent ;
if(listAdapter.getItemViewType(position) == 1){
intent = new Intent(ActivityContext,Volume.class);
Volume.currentPref((SoundDetails) timers.get(position),position);
} else{
intent = new Intent(ActivityContext,Message.class);
Message.currentMessage((MessageListDetails) timers.get(position),position);
}
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
For ListView you need to set onItemClickListener instead of onItemSelectedListener
chanage like this..
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// perform your operation
}
});
i have a custom listview in that, i have text field and check box. when i click on the list view the check box should be enable if it is not enable and vice visa. it is possible in android?
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
my listview is below.
<?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:orientation="vertical"
android:padding="6dip" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#color/white"
android:focusableInTouchMode="false"
android:text="CheckBox" />
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:visibility="invisible"
android:textColor="#color/white"
android:layout_toRightOf="#+id/checkBox1"
android:text="TextView" />
this is my list in main activity xml
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
1) You need an onItemClickListener
2) get the CheckBox
3) set the CheckBox to the opposite if its current status
4) done :)
listView.onItemClickListener(new OnItemClickListener(){
public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3){
CheckBox checkBox = v.findViewById(R.id.checkBox1);
checkBox.setChecked(!checkBox.isChecked());
}
});
You already have the listener that reacts, when someone clicks on a list-item.
The View that is given in the method onItemClick is the list-item.
Now you just need to get the CheckBox from the list-item:
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
And then you can check, if it's checked with isChecked() and set it with setChecked().
I have a Listview inside a Scrollview and manually binding its data on the code behind. My main problem is, when I bind my data, my listview isn't expanding its height. I've tried setting it to wrap_content, fill_parent but nothing works. Here is my XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parentScrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
>
<LinearLayout >
<ImageView
/>
<LinearLayout
>
<TextView />
<TextView />
<LinearLayout >
<TextView/>
<TextView/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView/>
<TextView />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View />
<TextView/>
<ScrollView
android:id="#+id/childScrollView"
android:layout_width="match_parent"
android:layout_height="375dp"
android:layout_below="#+id/view1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="18dp"
android:background="#drawable/scrollviewborder"
android:scrollbarStyle="insideInset" >
<ListView
android:id="#+id/listFriends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
>
</ListView>
</ScrollView>
<Button />
</RelativeLayout>
</ScrollView>
Please see that it is a nested ListView and here is my code behind
ArrayList<HashMap<String, String>> friendList = new ArrayList<HashMap<String, String>>();
// adding database values to HashMap key => value
for (int i = 0; i < friendName.length; i++) {
// creating new HashMap
HashMap<String, String> friend = new HashMap<String, String>();
//merchant.put(KEY_ID, merchantIdArray[i]);
friend.put(KEY_FRIEND_NAME, friendName[i]);
friend.put(KEY_THUMB_URL,imageIDs[i]);
friendList.add(friend);
}
list=(ListView)findViewById(R.id.listFriends);
adapter =new LazyAdapter(this, friendList);
list.setAdapter(adapter);
What seems to be the problem?
EDIT
Ive added this code for validating which scroll should be used
parentScrollView= (ScrollView) findViewById(R.id.parentScrollview);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Log.v(TAG,"PARENT TOUCH");
findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView= (ScrollView) findViewById(R.id.childScrollView);
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Log.v(TAG,"PARENT TOUCH");
findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
The main reason about this is that I will put my Facebook friendlist inside this Listview that it should be scrollable
The problem is the following (as stated here by Romain Guy):
Using a ListView to make it not scroll is extremely expensive and goes
against the whole purpose of ListView. You should NOT do this. Just
use a LinearLayout instead.
#Androyd Friend its always better if u dont put listview inside scrollView and also not scrollview inside listview,
abd then make listview fill parent or fix some hieght in dps like 300dp or that much height u want...