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

Get Facebook user email using latest Facebook SDK
IntentService not called if I put bitmap in Bundle and send to the service
Google Analytics v2 beta 5 for Android causes SIGSEGV
Android migrate Eclipse to Studio
state_focused didn't work on checkbox
unity3d couldn't find Android device
create database and connect native android app
how to check internet connection in Google nexus 7 (android 4.2)
View Pager Title Strip to show one title Only
AlertDialog Theme: How to change item text color?
GCM onRegistered() never gets called
Android Audio recorder automatically save a new file name in listview
How to add rows in table from JSON
Google Android Sales Report Auto Ingesting like iTunes Connect
How to setup a project that uses a lib but should not support all of its localizations?
Can an activity have two implements? [closed]

Categories

HOME
google-app-engine
cygwin
google-cloud-platform
cheerio
angular2-material
xbee
entity-framework-core
pkcs#11
discrete-mathematics
github-enterprise
siddhi
rtf
openbugs
speech-synthesis
selectize.js
git-extensions
charles
poedit
guzzle
ksh
springfox
react-toolbox
beta
draft-js-plugins
moses
mat
spotipy
calculation
userdefaults
event-flow
postback
android-sugarorm
hreflang
contacts
vue-chartjs
fossil
html-encode
browser-extension
apiary.io
watchman
jpad
graphhopper
sessionstorage
sim-card
odroid
railstutorial.org
levenshtein-distance
lpsolve
lightning
singleinstance
azure-cli
java-bytecode-asm
boxing
colorbar
check-mk
reshape
cmp
vibrate
pocketsphinx-android
gets
grails-plugin
reporting-services-2012
delta
simevents
google-query-language
stream-processing
redis-cluster
yajsw
aspen
morton-number
lasso
eoferror
amiga
teamcity-9.1
adehabitathr
delayed-execution
webmatrix-3
structuremap3
human-computer-interface
ora-00904
live555
memset
renderer
vbe
cfcache
syndicationfeed
multitasking
notorm
jammer
nebula
font-awesome-4.0.0
superscrollorama
chart-director
panda3d
wcf-configuration
failing-tests
jsr286
denied
expression-blend-4
tmx
object-database
filesystemobject
entitykey
image-capture
commercial-application

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App