Is it possible to place 2 buttons in List view - java

ListView
[Text] [Button 1][Button 2]
[Text] [Button 1][Button 2]
... (and so on) ...
Button 1 should be like Check Box and other button which allows to redirect on other page
Is above mentioned thing possible??
Thanks in advance..

Each row in a list view can have anything your heart desires. A row is basically defined as its own layout, and you can define the layout as you wish. In your case, you probably want to do a RelativeLayout. Check out the examples, they come pretty close to what you want.

You can have your ListView row to contain a CheckedTextView and a Button ina Linear Layout.
In \layout\ folder, define your customised my_row.xml, roughly so
<?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="horizontal">
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Then for populating your ListView, give this in your Adapter.
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.my_row, strings);
myList.setAdapter(myAdapter);

Related

RecyclerView animates items all at once instead of one by one

My Problem
I've been trying to get my Recycler View's items to animate as I scroll down. These items are loaded from a local XML and passed through my custom adapter.
I'm using this library for my adapter animation
I noticed that no matter what I tried, the items would just not animate at all, or they would animate all at once (every item would slide in on screen at the same time).
What I've Tried So Far
Utilizing the library I mentioned, I tried animating items in my RecyclerView.Adapter.
> IndexFragment.java snippet
//Create an adapter
SongAdapter adapter = new SongAdapter(songs, getContext());
//Set adapter
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
This one failed somehow. I also tried the setItemAnimator method which also failed.
recyclerView.setItemAnimator(new SlideInLeftAnimator());
Layout Structure
So I assume it has something to do with my RecyclerView being inside of two NestedScrollView's.
Another person seemed to be having a similar issue
Below are some portions of my layout files
activity_main.xml
....
<!-- Fragment Container -->
<android.support.v4.widget.NestedScrollView
android:id="#+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
....
This is where I put my fragments
index_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nestedScrollView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
This is pretty much my Recyclerview and its parent Nested Scrollview.
item_song.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp">
<!--I have two text views in here. Removed them just for this snippet -->
</LinearLayout>
<!-- This displays a background on each item when I press them AKA touch effects -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground" />
</FrameLayout>
This is the structure of each item in the Recyclerview. I inflate this layout for each item. Pretty straightforward.
Any ideas as to what I could do to solve this issue? Thanks.
So after much testing, I finally came up with a solution that worked.
Change the fragment container in activity_main.xml from NestedScrollView to RelativeLayout or perhaps LinearLayout.
Remove the parent NestedScrollView and keep only the RecyclerView in index_fragment.xml.
Animations now work at last. Yay!
Apparently, it is a bad idea to put your RecyclerView inside of a NestedScrollView.
I read somewhere that because of having to load the proper height of its child layout, the NestedScrollView has to wait for it to completely load, in order to know the total height of the RecyclerView. However, I'm not totally sure if that's the reason, so feel free to correct me.
Hope this helps someone!

Android - Can I override simple_list_item_1 colors programmatically?

EDIT: I've decided to go ahead and use a custom xml with a small change to get selection colors working. This doesn't completely solve my issue but I don't want to spend more time on it right now. Posting my current xml below in case it helps someone, and marking correct answer since it helped me pretty much achieve what i was going for in the first place. I ended up not needing to point to another xml if I use ?android:attr/selectableItemBackground
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black"
android:background="?android:attr/selectableItemBackground" />
I've made a GridView wrapped in a Dialog with an ArrayAdapter.
I wanted to have some kind of border between the cells (1dp white or black). The only way I was capable of accomplishing this was to write a custom xml and use it instead of simple_list_item_1, which would be fine, except that seems to break the item highlighting on select and such, and I can't seem to match the default look of it with my custom xml.
The question: Could I possibly just locate and override the color scheme of the built in xml?
I made a small attempt in the code below, but unsurprisingly I get a null pointer exception. Most of the searching just turned up suggestions for a custom xml, but it seems odd to do all that for a quick font color and font background color change.
I'll throw in my xmls just in case.
Working with a minimum API 11 if it matters.
Thanks guys,
Mike
-Java Snippet-
...
public void gridDialog(View v) {
String[] letters = new String[] {
"A", "B", "C", "D", "E"};
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.grid_pop);
GridView mGrid = (GridView) dialog.findViewById(R.id.fret_grid);
TextView tv = (TextView) findViewById(android.R.id.text1);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.BLACK);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, letters);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_item, letters);
mGrid.setAdapter(adapter);
mGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
// Do Stuff
dialog.dismiss();
}
});
dialog.show();
}
...
-my_item.xml-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black" />
-grid_pop.xml-
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fret_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:columnWidth="55dp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" >
</GridView>
Change to
android:textAppearance="?android:attr/textAppearanceListItemSmall"
and your item's will have the default selection behavior with the custom list item XML (a theory, haven't tested).
EDIT: Try this to make the list item light up when you tap it like the simple_list_item_1:
In the list item:
android:background="#drawable/clickable"
In drawable folder add this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
</selector>

Android: ListView tells me it's populated but not displaying items

So I've googled, and scoured SO for an answer to what I think is probably a ridiculous oversight, but here goes.
I have a ListView that I'm populating with an ArrayAdapter that I'm building out of a list of objects I'm using elsewhere in my application. I have checked via getCount that there are items in the adapter, both before and after I call .setAdapter(). Nothing is showing up in my application however.
My main layout res/layout/playlistview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playlist_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#80000000"
android:gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/playlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#206"
android:background="#602" >
</ListView>
</LinearLayout>
(I set the colors so I could see what's going on more easily)
the textview for each item res/layout/singlelistitem:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/single_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="5dp"
android:background="#206">
</TextView>
and the code I use to populate it:
private ListView playlistView;
private void buildPlaylistView() {
playlistView = (ListView)findViewById(R.id.playlistview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlelistitem, R.id.single_item, playlist.titlesAsArray());
playlistView.setAdapter(adapter);
playlistView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
((ArrayAdapter<String>)playlistView.getAdapter()).notifyDataSetChanged();
}
playlist.titlesAsArray() does return String[] and it works.
I have both of the .notifyDataSetChanged() in there, since I found that on SO and gave it a try.
When I change the android:layout_height in the ListView object in my XML to wrap_content, I see only the opaque background that is in the LinearLayout that wraps it. When I set the ListView android:layout_height to match_parent, the whole screen is #206 (magentish).
When i check getCount() on the adapter before setAdapter() it says there are items. When I check it after, from the view itself, it says there are the same number of items. I'm totally lost on this one, but nothing is displayed.
Try changing from android:orientation="horizontal" to android:orientation="vertical" this may fix.

Android Unchecking CheckBoxes on a ListView

I have a ListView with a custom adapter. It countains CheckBoxes. When one of them is checked, I would like to have the others unchecked. I tried creating a CheckBox list, and then created a method to return this list, but it kept giving IndexOutOfBounds (I added the CheckBoxes to the list at the getView method).
This is my adapter.
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox android:id="#+id/checkBoxListView"
android:layout_height="50px"
android:layout_width="50px"/>
<EditText android:id="#+id/editTxtListView"
android:layout_height="50px"
android:layout_width="680px"
android:editable="false"
android:focusable="false" />
why not have a global integer representing the checked position and in the getview method see if the global variable's value and the position, for which getView is called, are same then make check box checked else unchecked.Saw your code in the pastebin.You are making it quite a complex thing.Anyway, just a quick comment, Dont do this.pos=position;in the getView().Instead of this check if(postion==this.pos){//Check this checkbox}else{//uncheck this check box}Also, "pos" should be controlled by the activity or the pojo itself handling the data for the list.

ListView where each item is an ImageView with a radiobox and one item can be selected at a time?

I'm tearing my hair out over this. I would like a ListView where each item in the list is an ImageView. I want each item to have a radio button next to it and only one item can be selected at a time (i.e. single choice mode). This code works fine for creating a list of text box with radio buttons:
ListView listView = ...
String [] value = {"test1","test2"};
listView.setAdapter(
new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_single_choice,value
));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
For my ImageView items, I implemented by own ArrayAdapter. In the getView method of my ArrayAdapter object, I load the follow xml file for each list row:
e.g.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="center"
>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
<ImageView android:id="#+id/preview"
android:layout_width="75dip" android:layout_height="100dip"
android:scaleType="fitCenter"
android:enabled="true"
android:focusable="false"
/>
</RelativeLayout>
I copied the CheckedTextView item from the simple_list_item_single_choice.xml file used in the previous example. I assume that ListView must look for an item with id "text1" to use for displaying the selection status (I'm just guessing here as I cannot work out where the radio buttons are meant to come from). If I don't include the CheckedTextView item, I don't see any radio buttons at all.
When I use my custom adapter with the above xml file, I can see the radio buttons but selecting them does nothing. In addition, I've implemented the selection listener for the list view and this do not fire when I click items. What am I doing wrong?

Categories