how to create dynamic views

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

how to create dynamic views

Postby makrandmane » Tue May 19, 2009 7:10 am

Hello

i am creating one quiz application, for question page i have created one view by using Linear layout in that i have added four TextAreas, first TextArea for question and reaming three are for answer cell. so now, when user select the answer cell i want to show next question screen for that i am thinking of creating new view. means some questions may have two answers so i want to create that run time.

how to create new view at run time and how to switch for new view.
makrandmane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Mar 23, 2009 1:07 pm

Top

Reply

Postby satishkumar_lskin » Tue May 19, 2009 11:39 am

Hi,

For ur requirement , u can use a single linear layout and add child views to it whenever u need a new View.

Say for example ,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LinearLayout parent = new LinearLayout ( context );
  2.  
  3. parent.setLayoutParams(width, height ); //width, height can be set as needed.
  4.  
  5. parent.addView(myView , 0 );
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


When u have formed a new LinearLayout as the next View , it can be added as the child to the parent .

Now,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parent.addView(newLinearLayout, 0 );
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


You can remove the previous View that u had added to the parent first by calling

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parent.removeViewAt(1);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


because after the new child was added , the previous view would have now become the second (index = 1 ) child.

You can apply the above logic whenever u have to display a new View.

Regards,
Satish
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Re: Reply

Postby makrandmane » Tue May 19, 2009 1:44 pm

Thanks for reply

i think u replied my each and every post

thanks once again.

Now I want to explain you in detail what i am trying to do

I have created one question screen by using Linear Layout

like this

Image


I have used text view for displaying Question and Answers

What I Want ?

when user click on Answers A

the current view will replaced with new view with new question and answer.

the new view contains any number of answers like 2 or 5

like this

Image

in short

i want to switch the new view when user clicks on answer.(with animation but this is not important)

I have used view Animator but am not able to create dynamic child for view animator

because for view animator we have to define all child's in XML layout and i don't want to use any XML dependency.

please help me !!!!!!



satishkumar_lskin wrote:Hi,

For ur requirement , u can use a single linear layout and add child views to it whenever u need a new View.

Say for example ,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LinearLayout parent = new LinearLayout ( context );
  2. parent.setLayoutParams(width, height ); //width, height can be set as needed.
  3. parent.addView(myView , 0 );
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


When u have formed a new LinearLayout as the next View , it can be added as the child to the parent .

Now,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parent.addView(newLinearLayout, 0 );
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


You can remove the previous View that u had added to the parent first by calling

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parent.removeViewAt(1);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


because after the new child was added , the previous view would have now become the second (index = 1 ) child.

You can apply the above logic whenever u have to display a new View.

Regards,
Satish
makrandmane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Mar 23, 2009 1:07 pm

Reply

Postby satishkumar_lskin » Tue May 19, 2009 2:06 pm

Hi,
How about using this method

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void addView(View child, int index, ViewGroup.LayoutParams params) ;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


in ViewAnimator class ?.

ViewAnimator class extends FrameLayout , which means that the lastly added child view will be displayed on top of others. So after displaying the initial view , use the above addView() method to display the new child View.

The new child View can be constructed as a dynamic LinearLayout. Have a factory method which will produce a new Vertical LinearLayout if u pass it an ArrayList containing the views (textViews, for example ) to be added to it.

U can remove the initial view by finding the appropriate API for it in the ViewAnimator docs.

Repeat the above logic whenever u need a new View to be shown.
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 15 guests