In my app I have a Button on click of which it should take user to the top row of ListView
Code:
goTopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lvItems.smoothScrollToPosition(0);
}
});
It does not work as intended. It sometimes takes user just few rows up, sometimes just half screen, sometimes to the top most row. I think this is because of varying size of row(elements like TextView having longer text?).
Xml for the Button
<Button
android:id="#+id/topButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="To Top"
android:textAlignment="gravity"
android:gravity="bottom|center"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:textColor="#color/white"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:background="#drawable/go_to_top" />
Anyone has any idea how can I go to top of the list irrespective of number of rows?
Thank you.
Try this listView.setSelectionAfterHeaderView();. It worked for me.
Related
I need a way of turning a TableRow into a Button in android. I have tried to set up an onCLickListener() and I have tried nesting a TableRow inside a Button but that just crashes the app.
Edit:
I deleted the android:onCLick="onClick" like you said and that got rid of the crashing but nothing happens when I click the table row.
My code:
tableRow1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openInfoTR1 = new Intent("android.intent.action.MENU");
fromTableRow = 1;
startActivity(openInfoTR1);
System.out.println("Confirmed click");
}
});
<TableLayout
android:id="#+id/tlDisplayTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/trTableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:visibility="invisible"
android:clickable="true">
<TextView
android:id="#+id/tvDisplayedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="5"/>
</TableRow>
</TableLayout>
Every element that inherits from View can have attached an OnClickListener. No need to wrap it inside a button.
However, you'll have to look at how events are propagated through your layout. E.g. if you have clickable elements within your TableRow, the click events will normally be consumed by that elements and will not reach your OnClickListener. There are different ways to intercept or modify that behaviour, but you'd have to post your code to get more specific help.
EDIT:
The exception in your app comes from the line android:onClick="onClick" in your layout file. As you set the onClick listener programmatically, you do not need this. android:onClick="onClick" is a shortcut that would expect a method void onClick(View view) directly within your Activity (not, as you have it, as part of the OnClickListener implementation).
I create a ListView from an ArrayAdapter. Each row of ListView have an ImageView and a TextView. Now I handle clicked event by using setOnItemClickListener
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CategoryInfo cat = (CategoryInfo) lv
.getItemAtPosition(position);
showGameByCategory(Long.valueOf(cat.getId()), cat.getName());
}
});
When I click on a row, it will start another activity. On that activity, I have a button to go back to the ListView. When click that button, I'll call Activity.finish();.
Here's the problem: first time I open ListView and click on any items, it works fine and open new activity. But when I click button back and go back to ListView, I can't click to any items. My app have many tabs, if I switch to another tab and switch back to the ListView, I can click ListView item again. But anytime I click button back, the items are unclickable.
I test on 2 different OS and it works normally on Android 2.3 but this error occurred on Android 4.0.
Does anyone know how to fix this?
EDIT: this is my layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#drawable/listitem_selector_odd"
android:padding="6dip" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="#+id/avatar"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="6dip"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/toptext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/avatar"
android:layout_marginRight="6dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/arrow"
android:layout_width="10dip"
android:layout_height="15dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="6dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:src="#drawable/arrow_icon" />
</RelativeLayout>
</LinearLayout>
I had exactly the same problem as you. Even stranger, it happened on some tablet 4.0 or 4.1, while it didn't happen on a phone 4.1.
Using "descendentFocusabilty=blocksDescendants" didn't solve it.
Following the idea of user1603602, I ask for the invalidation of Views of the list (hence a redraw of elements of the list) each time the View becomes visible:
#Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
if (visibility == View.VISIBLE) {
list.invalidateViews();
}
}
As it reloads each items of the list, it can be annoying if you load images with animations for example, but it solves the problem, and it keep the scrolled position.
I had the same problem and just figured out a way to solve it. Remove the listview, then add it back. This will force the view to refresh to the original working state.
for example if your ListView lv is on a LinearLayout lout
lout.removeView(lv);
lout.addView(lv);
make sure you call this from the UI thread.
I think this is the same issue as described here
I used a custom adapter containing an array of Views. Within getView(position, ...), I return the View at the specified position. Apparently, ListView didn't like this and sometimes (platform specific) recycles views that shouldn't be recycled.
I have a button which currently has an animation assigned to it on click:
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.startAnimation(animRotate);
}
});
Here is an example of the button in XML:
<Button
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/btnText"
android:textColor="#ffffff"
android:textSize="18dp"
android:padding="12dp"
android:drawableTop="#drawable/btnIcon"
android:layout_weight="1" />
The animation works spot on, however on the button itself there is a text value and a drawable value, and I would like to be able to target the drawable and animate that only. I have searched all over the net and everything appears to be targeting objects by their id without any background/drawable selection.
Any feedback is welcome, even if its just to inform me that it can't be done.
Many thanks.
You may want to create a FrameLayout with the TextView and the Button and animate only the Button.
I am writing an application in which I want to limit access to a menu section from one user group. That user group has no fine motor skills, so I decided that a good way to access the menu would be to use a slider or some other method that requires precision hand movements.
I have been trying to use Radio Buttons to do this; I have 3 radio buttons, and I want to have it so that when they are all checked, they (the radio buttons) become invisible and the button that allows users to navigate to the next menu screen becomes visible.
I have been reading around, but am unsure of how to do this. Any code or tips that point me in the right direction would be greatly appreciated!
Sorted it out, using the following:
In the XML:
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="onCheckboxClicked"
android:text="" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="onSecondCheckboxClicked"
android:visibility="gone"
android:text="" />
In the Java:
public void onCheckboxClicked(View v) {
CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
// Toast.makeText(MainPage.this, "Selected",
// Toast.LENGTH_SHORT).show();
cb2.setVisibility(View.VISIBLE);
}
}
public void onSecondCheckboxClicked(View v2) {
Button button = (Button) findViewById(R.id.access_adult_controls);
if (((CheckBox) v2).isChecked()) {
button.setVisibility(View.VISIBLE);
}
}
I have some odd requirement. I have some menu buttons.when i am clicking on the buttons some other 3 buttons should visible. But when the focus is moving to another menu button, this 3 buttons should hide or become invisible. i did the first requirement. But unable to do the second. I take the three buttons in a relative layout.
<RelativeLayout android:id="#+id/relativelayout_inventory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout_menu"
android:layout_toRightOf="#id/relativelayout_checkout"
android:layout_marginTop="10px"
android:layout_marginLeft="18px"
android:visibility="invisible"
>
<Button android:id="#+id/stckupdt"
android:background="#drawable/stckupdt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<Button android:id="#+id/pushoffer"
android:background="#drawable/stckstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stckupdt"
android:layout_marginTop="10px"
>
</Button>
</RelativeLayout>
And in the java file, i write the code like below..
final Button button_inventory = (Button)findViewById(R.id.inventory);
final RelativeLayout view_inventory = (RelativeLayout)findViewById(R.id.relativelayout_inventory);
button_inventory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view_inventory.setVisibility(View.VISIBLE);
}
});
So you are intend to do like Windows menu? I don't know why you need to do that on a phone, but you better look at Touch Event to OnClickListener: Handling UI Events