I’m new to Android development and am having an issue with my first app’s UI. When a particular activity starts up, the screen position is initially scrolled so that it cuts off the top couple view items in my UI. The issue appears to be with the ExpandableListView and my implementation of it. The Activity wants to place it at the top of the screen initially. How do i prevent this scrolling from happening so that a user will see the top of my activity when they start it?
Any help would be greatly appreciated, thanks guys!
Screenshots: Initial: http://imgur.com/FMba5 Desired: http://imgur.com/oEx1N
Here is the main Layout of the Activity
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/new_league_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="League Settings"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:focusable="true"/>
<TextView
android:id="@+id/num_players_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/new_league_title"
android:text="@string/num_players_prompt"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/num_players_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:layout_below="@id/num_players_prompt"
android:prompt="@string/num_players_prompt" />
<TextView
android:id="@+id/roster_size_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/roster_size_prompt"
android:layout_below="@id/num_players_spinner"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/roster_size_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/roster_size_prompt"
android:prompt="@string/roster_size_prompt" />
<ExpandableListView
android:id="@+id/scoring_settings_list"
android:layout_width="fill_parent"
android:layout_height="1000dp"
android:focusable="false"
android:layout_below="@+id/roster_size_spinner" />
</RelativeLayout>
</ScrollView>
Here is the Layout I use for the children of the ExpandedListView
<?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:orientation="horizontal" >
<EditText
android:id="@+id/settingEditText"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:maxLength="3"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="6.0" />
<TextView
android:id="@+id/settingLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
And here is the Java code for the activity
public class NewLeagueActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_league);
setUpUI();
}
public void setUpUI(){
//Sets up the # of players in league spinner
Spinner numPlayersSpinner = (Spinner)findViewById(R.id.num_players_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.num_players, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numPlayersSpinner.setAdapter(adapter);
numPlayersSpinner.setSelection(2);
//Sets up the max roster size spinner
Spinner rosterSizeSpinner = (Spinner)findViewById(R.id.roster_size_spinner);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.roster_size, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rosterSizeSpinner.setAdapter(adapter2);
rosterSizeSpinner.setSelection(2);
//Sets up the expandable listview for Passing settings
ExpandableListView passScoring = (ExpandableListView)findViewById(R.id.scoring_settings_list);
passScoring.setAdapter(new EditScoringAdapter());
}
public class EditScoringAdapter extends BaseExpandableListAdapter {
private String[] groups = { "Score Settings for Passing", "Score Settings for Rushing",
"Score Settings for Receiving", "Score Settings for Kicking", "Score Settings for Defense", "Score Settings for Special Teams" };
private String[][] children = {
{ "Points per Passing Touchdown", "Passing Yards per Point", "Points per Interception Thrown" },
{ "Points per Rushing Touchdown", "Rushing Yards per Point", "Points per Fumble" },
{ "Points per Receiving Touchdown", "Receiving Yards per Point", "Points per Reception" },
{ "Points per PAT", "Points per Field Goal (0-39 yds)", "Points per Field Goal (40-49 yds)", "Points per Field Goal (50+ yds)" },
{ "Points per Interception", "Points per Fumble Recovery", "Points per Sack", "Points per Safety", "Points per Defensive Touchdown",
"Points per Blocked Kick", "Points for Allowing 0 Points", "Points for Allowing 1-6 Points",
"Points for Allowing 7-13 Points", "Points for Allowing 14-17 Points", "Points for Allowing 18-21 Points",
"Points for Allowing 22-27 Points", "Points for Allowing 35-45 Points", "Points for Allowing 46+ Points"},
{}
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = NewLeagueActivity.this.getLayoutInflater();
View editTextView = inflater.inflate(R.layout.labeled_edit_text, null);
((TextView)editTextView.findViewById(R.id.settingLabel)).setText(getChild(groupPosition, childPosition).toString());
return editTextView;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public TextView getGenericView() {
//Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(NewLeagueActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(75, 0, 0, 0);
return textView;
}
}
}
Remove
ScrollView. It’s supposedly not a good practice to nestListViewin aScrollViewand/or vice versa. See this for more details.