I have design like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="horizontal" >
<Button
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<Button
android:id="#+id/nextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
<!-- the two columns part -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:id="#+id/submenue"
>
<!-- this will be the menue list -->
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contents"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.20"
android:id="#+id/mainLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second Name" />
</LinearLayout>
</LinearLayout>
I added the following code to the next button such that it display and fill the list
this.next = (Button)this.findViewById(R.id.nextbutton);
this.next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] MainMenue = new String[] { "aaaa", "bbb", "ccc"};
menueview = (ListView)findViewById(R.id.MyHiddenListView);
menueview.setVisibility(ListView.VISIBLE);
menueview.setAdapter(new submenueadapter(getApplicationContext(), MainMenue));
}
});
and the adaptor class is
package com.appnetics;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class submenueadapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public submenueadapter(Context context,String[] objects)
{
super(context, R.layout.main, objects);
this.context = context;
this.values = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.contents);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
return rowView;
}
}
the problem is that the list be populated with the array plus the other controls in the page (the 2 buttons )
the layout before clicking next
the layout after clicking
any idea to fix that please
Best regards
Best regards
The main problem is that you are including everything in one main.xml layout.
And due to this problem, you are getting back/next button for every listitem (Actually whole layout is being repeating as Listitem).
Soluition:
Instead, Try to define separate layout for the ListItem and inflate that layout inside the getView() method. so there must be 2 XML layouts file, one is having main design with ListView only, and another one with only views that you want for every list item.
for example, (1) main.xml (2) row_item.xml
Now, you just need to make correction in code of inflating layout:
View rowView = inflater.inflate(R.layout.row_item, parent, false); // row_item.xml is inflated here
ViewGroup class do offers various methods to add view anywhere and using LayoutParams classes you can pass the various parent container class specific attributes to applied over the child view to be added in the container.
That you can google and is easily doable.
I can also think of putting a ListView in the XML with the Visibility GONE.
And on specific action trigger make it visible. That will prevent me doing various programing stuff required to add view at a perticular position.
And i also don't feel any harm of this approach.
e.g
<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/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</Linearlayout>
In the .java file
btnBack = (Button)findViewById(R.id.backbutton);
myListView = (ListView)findViewById(R.id.MyHiddenListView);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myListView.setVisibility(ListView.VISIBLE);
myListView.setAdaptor(new MyListViewAdaptor());
}
});
Hope it helps :)
Related
Here is the layout when each Button has the same size text:
And here is the layout when I increase the size of the bottom right Button's text:
What's going on here? Why is the "0" Button moving lower than the two Buttons adjacent to it?
Here is the layout:
<?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"
android:layout_gravity="center"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:singleLine="true"
android:textSize="40sp"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3">
<Button
android:id="#+id/the_1_button"
android:background="#android:color/white"
android:text="1"/>
<Button
android:id="#+id/the_2_button"
android:background="#android:color/white"
android:text="2"/>
<Button
android:id="#+id/the_3_button"
android:background="#android:color/white"
android:text="3"/>
<Button
android:id="#+id/the_4_button"
android:background="#android:color/white"
android:text="4"/>
<Button
android:id="#+id/the_5_button"
android:background="#android:color/white"
android:text="5"/>
<Button
android:id="#+id/the_6_button"
android:background="#android:color/white"
android:text="6"/>
<Button
android:id="#+id/the_7_button"
android:background="#android:color/white"
android:text="7"/>
<Button
android:id="#+id/the_8_button"
android:background="#android:color/white"
android:text="8"/>
<Button
android:id="#+id/the_9_button"
android:background="#android:color/white"
android:text="9"/>
<ImageButton
android:id="#+id/the_backspace_button"
style="#style/Widget.AppCompat.Button"
android:background="#android:color/white"
android:src="#drawable/ic_backspace"/>
<Button
android:id="#+id/the_0_button"
android:background="#android:color/white"
android:text="0"/>
<Button
android:id="#+id/the_done_button"
android:layout_width="88dp"
android:layout_height="48dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:background="#android:color/white"
android:text="done"/>
</GridLayout>
</LinearLayout>
Update:
I made the text of the "DONE" Button super large -- 100sp -- and set maxWidth and maxHeight for it equal to the height and width of the other Buttons. Here is the result:
*the blue is the GridLayout's background, the red the "0" Button's, and the yellow the "DONE" Button's
Why would changing the size of the text of the "DONE" Button affect anything other than that specific Button if the size of the Button never changes?
I think there is a problem with the done button. You set the background of it. Then you also set its text as "done" .
Just set it text empty and the problem may resolve ;)
What about using the grid:layout_rowWeight or grid:layout_rowHeight attributes to be the same for every button?
Look at this:
https://stackoverflow.com/a/32291319/2205825
Try this .....
Use GridView instead of many Button that not give you any Layout problem (Adjustment of Views).
Have look on the below example that solve your problem about Adjustment of Buttons
use this layout instead of your layout .....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:textSize="40sp" />
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="3" />
</LinearLayout>
add these 2 line in your OnCreate method .....
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new CustomAdapter(this));
add this adapter in your project .....
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
/**
* Created by sushildlh on 10/14/16.
*/
public class CustomAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public String[] text = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "DONE"
};
// Constructor
public CustomAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 12;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new textView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (holder == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.item, parent, false);
holder = new Holder();
holder.button = (Button) convertView.findViewById(R.id.button);
holder.image = (ImageButton) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (position != 9) {
holder.image.setVisibility(View.GONE);
holder.button.setText(text[position]);
} else {
holder.image.setImageResource(R.drawable.ic_backspace_black_24dp); //change ic_backspace_black_24dp into to your image ......
holder.button.setVisibility(View.GONE);
}
return convertView;
}
public static class Holder {
Button button;
ImageButton image;
}
}
and this is item.xml layout into your layout folder ....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#null"
android:textSize="25dp"/>
<ImageButton
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="60dp"
android:focusable="false"
android:background="#null"
android:gravity="center" />
</LinearLayout>
above code give you the output like this ....
Note:- Always use GridView OR ListView for same pattern of Views .....
You can use the android:layout_weight property.
It appears that the GridLayout is reserving enough height for the actual text size and some default padding despite the layout_height for the Done button.
Build it with 'Linear Layouts' instead.
Was facing the same problem, so just i did was to change the height and width of the button to it parent RowTable's match parent & wrap content and it solved my problem
I search a lot for an answer and didn't find one that suit me.
I found out that when I set my layout_heigh to 500dp -> all the item shown, but when it has normal size (layout_heigh=wrap_content) then only the first item is shown.
this is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
xml_item.xml: this is for one item to duplicate in the listView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/itemText"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="15dp"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/doneBtn"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Done" />
</RelativeLayout>
MyListAdapter: a class in the main_activity that extends the ArrayAdapter class so I can put in one row both TextView and a button.
private class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout= resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainHolder= null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView= inflater.inflate(layout, parent, false);
ViewHolder viewHolder= new ViewHolder();
viewHolder.item= (TextView) convertView.findViewById(R.id.itemText);
viewHolder.btn= (Button) convertView.findViewById(R.id.doneBtn);
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), itemArray.get(position), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
mainHolder= (ViewHolder) convertView.getTag();
mainHolder.item.setText(getItem(position));
}
return convertView;
}
public class ViewHolder {
TextView item;
Button btn;
}
}
Thanks for all! :)
but when it has normal size (layout_heigh=wrap_content)
It is a very bad idea using a ListView by giving it a height=wrap_content.
This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be.
You can check it your self debugging the getView() method inside the adapter.
In your case, using a LinearLayout just add a top margin (50dp as the Button) to your listView and use layout_heigh=match_parent.
Otherwise use a RelativeLayout.
Change your listview to take up as much room as is can considering the height of any sibling views (in this case your button). Here's how to do that with the android:layout_weight attribute
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture
I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.
Please provide some directions.
create_team_new.xml(layout where gridview resides)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/parentcreateteam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TextView
android:id="#+id/citytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:layout_alignTop="#+id/teamcity"
android:layout_alignParentLeft="true" />
<AutoCompleteTextView
android:id="#+id/teamname"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/teamcity"
android:layout_alignRight="#+id/gridviewplayer"
android:text="#string/teamname"
android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/createteam"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Create Team"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/red_button"
style="#+style/button_text" />
<TextView
android:id="#+id/teamnametextview"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignBaseline="#+id/teamname"
android:layout_alignBottom="#+id/teamname"
android:layout_alignParentLeft="true"
android:text="#string/teamname" />
<AutoCompleteTextView
android:id="#+id/teamcity"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cricket"
android:layout_alignRight="#+id/gridviewplayer"
android:layout_below="#+id/teamname"
android:layout_marginTop="22dp"
android:ems="10"
android:text="#string/city"
android:textSize="18sp" >
</AutoCompleteTextView>
<TextView
android:id="#+id/radiotextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/teamcity"
android:layout_marginTop="15dp"
android:text="Pick a Sport" />
<GridView
android:id="#+id/gridviewplayer"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_above="#+id/createteam"
android:layout_below="#+id/pick_sport"
android:layout_centerHorizontal="true"
android:background="#drawable/gv_bkg"
android:padding="5dp"
android:numColumns="4" >
</GridView>
<RadioGroup
android:id="#+id/pick_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/createteam"
android:layout_below="#+id/radiotextview"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/cricket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<RadioButton
android:id="#+id/soccer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer" />
</RadioGroup>
</RelativeLayout>
gv_createplayer.xml(view to be inflated inside gridview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/btn_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text= "+"
android:background="#drawable/red_button">
</Button>
</LinearLayout>
PlayerAdapter.java(gridview Adapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
// Keep all Images in array
public Integer[] mThumbIds = {};
/* R.drawable.gridview_createteam, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15
};
*/
// Constructor
public PlayerAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressWarnings("static-access")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = new View(mContext);
view.inflate(mContext, R.layout.gv_createplayer,null);
return view;
}
}
1) How to create a list view inside a main view, only using a half of the screen.
This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.
2) How to populate list view in grid manner (a row of two columns will repeat itself)
Well, with the GridView (the adapter API is the same) (:
I have two listviews, but they don't scroll. How do I correct this?
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/backgrund" >
<!-- Header Starts -->
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:background="#layout/header" >
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<TextView
android:id="#+id/textAD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/header"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:layout_marginRight="26dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_gravity="center_horizontal"
android:focusable="false"
android:paddingBottom="5px"
android:paddingTop="10px"
android:src="#android:drawable/divider_horizontal_bright" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000"
android:focusable="false" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:orientation="vertical" >
<TextView
android:id="#+id/textm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Malzemeler"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:id="#+id/editaramalzeme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/btnmalzlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:text="Ara" />
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<ListView
android:id="#+id/listsecili"
android:layout_width="wrap_content"
android:layout_height="210dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<EditText
android:id="#+id/txtNot"
android:layout_width="match_parent"
android:layout_height="88dp"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="6"
android:singleLine="false" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/btnkaydet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="malzeme ekle" />
<Button
android:id="#+id/btntoplugonder"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textAD"
android:layout_below="#+id/btnkaydet"
android:text="toplu gonder" />
</RelativeLayout>
</ScrollView>**
Never put ListView in ScrollView. ListView itself is scrollable.
By default ListView is scrollable. Do not put ScrollView to the ListView
I know this question is 4-5 years old, but still, this might be useful:
Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. That's because the operating system doesn't view it as actually exceeding the screen.
I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. And it was a real mystery. As soon as I added a few more, it started to scroll.
So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". It did, for me.
Practically its not good to do. But if you want to do like this, just make listview's height fixed to wrap_content.
android:layout_height="wrap_content"
Listview so have inbuild scrolling capabilities. So you can not use listview inside scrollview. Encapsulate it in any other layout like LinearLayout or RelativeLayout.
This is my working code. you may try with this.
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:background="#ffffff">
<TextView android:id="#+id/tvEmpId"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.3"/>
<TextView android:id="#+id/tvNameEmp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.5"/>
<TextView
android:layout_height="wrap_content"
android:id="#+id/tvStatusEmp"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.2"/>
</LinearLayout>
details.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/page_bg"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lLayoutGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/page_bg"
android:orientation="vertical" >
................... others components here............................
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alwaysDrawnWithCache="true"
android:dividerHeight="1dp"
android:horizontalSpacing="3dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:verticalSpacing="3dp"
android:layout_marginBottom="30dp">
</ListView>
</LinearLayout>
</RelativeLayout>
Adapter class :
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<EmployeeBean> employeeList;
publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
this.context = context;
this.employeeList = employeeList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
EmployeeBeanHolder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
holder = new EmployeeBeanHolder();
holder.employeeBean = employeeList.get(position);
holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);
row.setTag(holder);
holder.tvEmpId.setText(holder.employeeBean.getEmpId());
holder.tvName.setText(holder.employeeBean.getName());
holder.tvStatus.setText(holder.employeeBean.getStatus());
if (position % 2 == 0) {
row.setBackgroundColor(Color.rgb(213, 229, 241));
} else {
row.setBackgroundColor(Color.rgb(255, 255, 255));
}
return row;
}
public static class EmployeeBeanHolder {
EmployeeBean employeeBean;
TextView tvEmpId;
TextView tvName;
TextView tvStatus;
}
#Override
public int getCount() {
return employeeList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
employee bean class:
public class EmployeeBean {
private String empId;
private String name;
private String status;
public EmployeeBean(){
}
public EmployeeBean(String empId, String name, String status) {
this.empId= empId;
this.name = name;
this.status = status;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId= empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status =status;
}
}
in Activity class:
onCreate method:
public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();
//create emplyee data
for(int i=0;i<=10;i++) {
EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
EMPLOYEE_LIST .add(emplyee );
}
ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
Putting ListView inside a ScrollView is never inspired.
But if you want your posted XML-like behavior, there're 3 options to me:
Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).
Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)
If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect.
To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
I found a tricky solution... which works only in a RelativeLayout.
We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview
android:clickable="false" />
<View
android:layout_width="match_parent"
android:background="#drawable/gradient_white"
android:layout_height="match_parent"
android:clickable="true"
android:layout_centerHorizontal="true"
android:layout_alignTop="#+id/listview" />
I had this problem and I found the solution. In my case the listView was bigger than the screen, so the list was not big enough to need scroll, but I coudnĀ“t see it because half list was out of screen.
I solved it adding enough marginBotton to make the listView shorter and it suits to the screen.
<ListView
android:id="#+id/lvDatos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="300dp"/>
I have a very basic ListView in android and had set a very basic adapter. My problem is that the list view does not show anything, regardless of the adapter and the notifyDataSetChanged();
Here is my code:
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">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
The Activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.androidcourse.phonemapper.R;
import com.androidcourse.phonemapper.model.SelectViewAdapter;
public class SelectActivity extends Activity {
private ListView mListView;
private SelectViewAdapter mAdapter;
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.select_activity);
initializeListView();
}
private void initializeListView() {
mListView = (ListView) findViewById(R.id.selectView);
mAdapter = new SelectViewAdapter(this);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
#Override
public void onResume() {
super.onResume();
}
}
And the Adapter code:
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SelectViewAdapter extends BaseAdapter {
private Context mContext;
private TextView mMockTextView;
public SelectViewAdapter(Context cnt) {
mContext = cnt;
mMockTextView = new TextView(mContext);
mMockTextView.setText("Test text");
mMockTextView.setBackgroundColor(Color.CYAN);
}
#Override
public int getCount() {
return 3;
}
#Override
public Object getItem(int position) {
return mMockTextView;
}
#Override
public long getItemId(int position) {
return 3;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return mMockTextView;
}
}
The problem is that nothing is shown on the screen. A black screen (and the first text view from the XML) is all I get. I cannot see the mockTextView and its text. Apparently I am doing something quite wrong, but I cant figure out what.
A few things I can think of.
First, Your RelativeLayout has no relative positioning information. I would assume you meant to put this in a LinearLayout with orientation set to vertical from what you describe. My guess is that the list is not actually being drawn since it isn't even anchored to anything in the current RelativeLayout. If you stick with the RelativeLayout, make sure to put an id on the app_name TextView and position the ListView under it via layout_below.
LinearLayout Solution
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
RelativeLayout Solution:
<?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">
<TextView
android:id="#+id/app_name_text"
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_below="#id/app_name_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
Next, your getView() returns the same textView for all 3 indexes. It's not a problem to display the same view over multiple indexes however with a list size of three, I am betting that the screen can display all three at the same time. And since a View can't be in more than one position at a time, I actually would expect this to fail so I doubt it is even getting to this code yet. Try creating a new TextView for each getView(). Also your MockTextView doesn't have layout params of it's own. So laying it out within a listView cell might not be happening either. So you can give it params of type AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT). Again though I would expect this to error if it got to the original code.
getView() tidy up:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
TextView textView = new TextView(parent.getContext());
textView.setText("Position is:"+position);
textView.setBackgroundColor(Color.CYAN);
textView.setLayoutParams(new AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
convertView = textView;
}
return mMockTextView;
}
And lastly the wrap_content height of your list can sometimes be problematic. I am not aware of all the scenarios. If you end up changing to a LinearLayout try setting your layout_height of the list view to 0 and then set the layout_weight=1. This forces the linear layout to inflate it into more space.
LinearLayout Weight Solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="#+id/selectView"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).
I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.
Once I added android:orientation="vertical" everything worked fine.
Two hours of my life on this one.
While it does not answer this question, a typical issue is to inflate as adapter resource a layout based on a parent LinearLayout with height=wrap_content and each LinearLayout element with height=fill_parent.
Like in
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/someText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"/>
While it is displayed nicely in Eclipse, each row ends with height=0. This can be fixed by setting height=wrap_content for one of the child element
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/someText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Could be helpfull for someone.
Make sure your adapter function getCount() is implemented and returns higher value then 0. If method returns 0, list is not shown or filled.
i had the same problem but when i changed the
ConstraintLayout
into
Relative layout
it become visible and problem solved.