How do I space out items in a recyclerview? - java

Hey how do I space out items in a recyclerview, I am currently using an array in java to provide the images and the names of the objects, however, it wont let me use padding to space out the items in my list??
<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="com.athena.athenafront.NavigationFragment"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#android:color/white">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primaryColor"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_recyclerview"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:background="#ffff"
android:layout_height="400dp">
</android.support.v7.widget.RecyclerView>
Here is my java
public static List<AthenaPanel> getData() {
//created an object for ur Drawer recyclerview array
List<AthenaPanel> data= new ArrayList<>();
//this is where you would add all your icons for the drawer list
//arrays of icons
int[] icons={R.mipmap.ic_launcher};
String[] titles = {"Explore","Myprofile","MyStatus","Calendar","Setting","Send FeedBack"};
//this is our for loop to cycle through the icons and the titles
for(int i=0;i<5;i++){
AthenaPanel current=new AthenaPanel();
//i%().length allows ups to loop over the array any number of times we want to
current.iconId=icons[i%icons.length];
current.title=titles[i%titles.length];
data.add(current);
}
return data;
}

To add the space on the RV itself do this:
On your RecyclerView xml, use this: android:padding="8dp" or any other value that you like. Along with android:clipToPadding="false".
E.g.:
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_recyclerview"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:background="#ffff"
android:layout_height="400dp"
android:clipToPadding="false"
android:padding="8dp">
</android.support.v7.widget.RecyclerView>
More info on that here.
To add space on the RV items, you'll have to edit the XML files of your item row and add the margin/padding there.
EDIT: Maybe this question is related.

Related

Listview items not displaying the retrieved columns from database vertically

Using simple cursor adapter I retrieve 5 columns from a table using cursor query and display them using a listview. Everything goes well but the columns retrieved are displayed horizontally in the listview. I want them to be displayed one below the other like:
TX_UID
TX_NAME
TX_AMOUNT
TX_PARTICULARS
AND NOT LIKE (Displayed as of now):
TX_UID TX_NAME TX_AMOUNT TX_PARTICULARS
String[] from = new String[]{ vivzHelper.TX_UID,
vivzHelper.TX_NAME,vivzHelper.TX_AMOUNT,vivzHelper.TX_PARTICULARS};
int[] to = new int[]
{R.id.textView,R.id.textView2,R.id.textView3,R.id.textView5 };
SimpleCursorAdapter cursorAdapter;
cursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.customgetalltxrowforeditordelete , c , from , to , 0);
thislist.setAdapter(cursorAdapter);
thislist.setAdapter(cursorAdapter);
XML Layout:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="35dp"
android:fastScrollEnabled="false"
android:divider="#FFCC00"
android:dividerHeight="2px"
android:smoothScrollbar="true"
android:background="#drawable/my_selecter"
/>
columns retrieved are displayed horizontally in the listview. I want
them to be displayed one below the other
Problem is related to customgetalltxrowforeditordelete layout instead of ListView.
To show TextView's in ListView row Vertically. use LinearLayout with orientation attribute to vertical
In your customgetalltxrowforeditordelete xml file you need to do something like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/badge13"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView2"
android:layout_below = "#id/textView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView3"
android:layout_below = "#id/textView2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView4"
android:layout_below = "#id/textView3"
/>
you need to use layout_below attribute in your TextView providing top view's id in attribute.

RecyclerView with dynamic sized elements

I am trying to make the switch from listview to recyclerview but can't seem to figure out how best to recycle views. What I would like is a set of cardviews in a cardview. The data set resembles a bunch of students in different classes. Every class has different sizes and they are to be represented in the same card.
What I have at the moment is the following xml where 'tv1' will contain the title of the class and the student names are dynamically added in 'card_ll'. The problem I am having is that I inflate the view for each student name manually and add / clear the linearlayout depending on how many students there are in the class. Note, the number of students is non-zero but cannot be determined beforehand.
Is there a way to reuse the views of each student card?
Thanks
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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view1"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view2"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="#+id/card_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter onBindViewHolder implementation
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ClassType ec = mDataset.get(position);
final String name = ec.classtitle;
holder.tv1.setText(ec.header1);
if( ec.studentlist!=null && ec.studentlist.size()>0) {
holder.cardll.removeAllViews();
for (Student s: ec.studentlist) {
TextView tv = new TextView(mContext);
tv.setText( s.getName());
tv.setTextColor(Color.BLACK);
holder.cardll.addView(tv);
}
}
}

Selected text alignment for Android spinner

I am trying to align selected text of a Spinner in Android. It should align to the matching indentation of the other TextView and EditText in the layout.
What I have in my layout:
http://i.imgur.com/Y5sgbsN.png
What i want to do:
http://i.imgur.com/viVHpiC.png
My Layout code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:entries="#array/bank_arrays"
android:prompt="#string/bank_prompt"
android:fontFamily="arial,helvetica"
android:hint="#string/bank_prompt"
android:spinnerMode="dialog"
android:ellipsize = "none"/>
Please help me out with this UI change.
Thanks
Update 1:
Parent Layout code:
<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:layout_marginBottom="14.5sp"
android:layout_marginLeft="14.5sp"
android:layout_marginRight="14.5sp"
android:layout_marginTop="14.5sp"
tools:context=".RegistrationActivity" >
This is the spinnerinflate.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f5ff"
android:textSize="#dimen/text_size"
android:text="Text Here" >
</TextView>
In your code you can set the custom Layout for spinner as
MyAdapter myAdapter = new MyAdapter(this, R.layout.spinnerinflate,
arraylist);
spinner.setAdapter(myAdapter);
You can use SimpleAdapter as well if you don't need to have your own adapter as below
ArrayAdapter<String> simpleadapter = new ArrayAdapter<String>(YourActivityName.this, R.layout.spinnerinflate, getResources().getStringArray(R.array.yourarray));
spinner.setAdapter(simpleadapter);
By this inflate you can do whatever formatting you want. Please let me know if it helps.
you can use this lines
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="0dp"
android:textDirection="ltr" />
and you can increase padding from left to more space

Scroll Bar in List View

Why is there a Scroll Bar in List View even when the data in displayed in it doesn't require scrolling?
here is my xml file
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
Comparing getLastVisiblePosition() with getCount() in a Runnable, you will get to know whether your list view will fit into the screen or not. Also check if the last visible row fits entirely on the screen or not.
ListView listView;
Runnable fitsOnScreen = new Runnable() {
#Override
public void run() {
int last = listView.getLastVisiblePosition();
if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) {
listView.setScrollContainer(false);
}
else {
listView.setScrollContainer(true);
}
}
};
Finally in ListView's Handler: onCreate()
listView.post(fitsOnScreen);
You can do this by
listView.setScrollContainer(false);
for more please check
How to get a non scrollable ListView?
If you are not want to hide the scrollbar for ever. Then it will help you.
Due to android:layout_height="match_parent" on listView and TextView scrollbar appears.
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" // change 1
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" //change 2
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
android:scrollbars="none"
Try putting the above line in your listview in xml.

Using two listViews in one activity

I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?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="fill_parent"
android:orientation="vertical"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks

Categories

Resources