Dynamically add views to ScrollView

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Dynamically add views to ScrollView

Postby gskillz360 » Tue Apr 28, 2009 12:19 am

The skeleton of my code is written out below. I need a way to have an activity that can display a view that is larger than the screen. I need to be able to add views dynamically, so I cant create a static list in the xml manifest file. I need to add views sequentially (ReportView2 in this example) to the main layout. I implemented this by putting the views in a linearLayout and then placing the layout in the ScrollView. Since I also need to place buttons in the layout on an absolute basis, I placed the scrollview into an absoluteLayout.

When I run my code, the views' onDraw method is not called (the method that actually draws the views usually called by setContentView()), and therefore I get a blank screen. Any ideas?

Thanks in advance

[syntax="java"]public class RepMed2 extends Activity{

ScrollView reportView;
AbsoluteLayout myLayout;
LinearLayout mLL;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);



//initiating views
reportView = getView();
myLayout = new AbsoluteLayout(this);
myLayout.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 0 , 0));
myLayout.addView(reportView);

setContentView(myLayout);
reportView.invalidate();
myLayout.bringToFront();
}


private ScrollView getView(){
//setup new linearlayout
mLL = new LinearLayout(this);
mLL.setOrientation(LinearLayout.VERTICAL);
mLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mLL.setVerticalScrollBarEnabled(true);
mLL.setBackgroundColor(0xffffcc99);
addViews();

ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sv.addView(mLL, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return sv;

}


private void addViews() {
ReportView2 rep = new ReportView2(this, med, timeOfInterval);
mLL.addView(rep);
}
}
gskillz360
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 28, 2009 12:11 am

Top

Postby alpesh_harsoda » Fri May 01, 2009 2:37 pm

Hi,
set the hight and width of scrollview when adding to absolutelayout.so your button also display after scrollview has been added.
Regards,
Alpesh Patel,
Logistic Infotech
http://www.logisticinfotech.com
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby gskillz360 » Fri May 01, 2009 6:58 pm

Hi,

I took your advice and changed the code to:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myLayout.addView(reportView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


A blank screen still pops up when I run the code. Any ideas

Thanks
gskillz360
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 28, 2009 12:11 am

Postby zebdor44 » Tue Apr 20, 2010 12:12 am

I know this an old post but I was having the same problem.

Try setting the layout_width and layout_height to an actual pixel amount instead of FILL_PARENT or WRAP_CONTENT. That did the job for me.

Hope this helps anyone else out there with this problem.
zebdor44
Freshman
Freshman
 
Posts: 3
Joined: Mon Apr 19, 2010 5:19 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests