android


Why are my dynamically added ListViews to a LinearLayout not displaying properly?


I am dynamically adding ListView layouts to my vertically oriented LinearLayout. However, when the app runs, all of the data are squished together on the screen. If I try to scroll, I find that each independent ListView is scrollable, and I only see one line at a time per ListView, instead of my enclosing LinearLayout being scrollable.
Here is what I have:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(){
super.onCreate();
setContentView();
printSensors();
}
private void printSensors(){
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
ListView.LayoutParams layoutParams = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
for(Sensor sensor : sensorList){
ListView listView = new ListView(this);
listView.setId(View.generateViewId());
listView.setLayoutParams(layoutParams);
String[] sensorArray = sensor.toString().substring(1, sensor.toString().length()-1).split(",");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sensorArray);
listView.setAdapter(arrayAdapter);
mainLayout.addView(listView);
}
}
}
activity_main.xml
<ScrollView>
<LinearLayout android:id="#+id/main_layout" android:orientation="vertical">
</LinearLayout>
</ScrollView>
However, if I change ListView.LayoutParams.WRAP_CONTENT to an integer, say 500, then everything lays out nicely and looks good.
So, what gives?
change width of your listView to match parent instead of wrap content, also you have to set height and width to scroll view
private void printSensors(){
list = (ListView) findViewById(R.id.list);
ArrayList<String> sensorArray = new ArrayList<>();
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
for(Sensor sensor : sensorList){
sensorArray.add(sensor.toString().substring(1, sensor.toString().length()-1).split(",").toString());
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sensorArray);
list.setAdapter(arrayAdapter);
}
Wrap the LinearLayout in another LinearLayout as follows:
<ScrollView>
<LinearLayout android:orientation="vertical">
<LinearLayout android:id="#+id/main_layout"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>

Related Links

Android : Navigation button show wrong side when layout direction is RTL
Issue with Left to Right Swipe Gesture in Android
CachedRowSet on Android?
how to display a datable from webservice using SOAP in a spinner oF Android
FTPClient.connect does not work [duplicate]
Android swipe tab crash
Unity3d+Android, Get path of Assets/Plugins/Android directory
Different package when app is getting launched (Lollipop or above issue)
Android Spinner Force closed
Getting battery stats of all apps in android
Is it safe to store encryption key in android class file
About bluetooth socket issue
Android : In Lollipop sms intent not return to app
i want to convert 8 digit hexa decimel color code of #4CAF50 in android
when i am adding android support library v7 app compact not install error
Toolbar menu item always in overflow

Categories

HOME
paraview
gdb
c++11
string
telegram
github-for-windows
ns-3
apiblueprint
spring-data-mongodb
settings
google-admin-sdk
flume
fabric.io
endeca
plist
java.util.logging
emoji
filechannel
package.json
topology
asp.net-core-webapi
drive
springfox
pimcore
hta
epsilon
event-flow
paragraph
utorrent
quickfixj
playstation-portable
code-snippets
parse-tree
sql-server-ce
imagenet
demo
mongoose-schema
spring-mvc-test
attributeerror
tcc
importerror
launchctl
openblas
page-replacement
wpas
boxing
android-testing
django-1.10
structuremap4
rmongo
mta
model-associations
freepbx
lua-telegram-bot
yajsw
html-to-pdf
beanstalk
crouton-os
inf
terracotta
themoviedb-api
video-player
dock
cross-join
mass-assignment
sql-scripts
build-script
winginx
windows-embedded-standard
scrypt
printdialog
kogrid
dolby-audio-api
virtual-pc
idn
wcf-configuration
paginator
predicatebuilder
cross-database
cdonts
spread
django-piston

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App