Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement the different views dynamically and handle input data and set the data

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 322
    Answer it

    I would like to add different kinds of dynamic views like textview, edittext, datepicker etc to already existed layout. With this existed layout have some static views which are mandatory. this dynamic views have to add based on API response count and type of field.

 2 Answer(s)

  • 1.Create different Layouts for TextView,EditText
    2.Check API response(ex:- 1 for TextView and 2 for EditText ) and Inflate view according to API response.
    3. Add Inflated view to its parent layout.
    4. set API response result as Tag to parent view.(view.setTag(1)).

    When you want to get text from view then you can get View from parent and cast view according to Tag.

    Example:-

    RelativeLayout parentLayout;
        View view;
     int viewCode=1;
    

    above parentLayout relative layout is your main layout in which you want to add View and view in inflated view(TextView/EditText) which you want to add into parentLayout.
    view code is code which you get from API.

        parentLayout.addView(view);
        parentLayout.setTag(viewCode);  
    

    Add View To parent View and Add viewCode as Tag to parentView .

     int viewType = (Integer)parentLayout.getTag();
    
        if(viewType==1)
        {
            TextView textview = (TextView)parentLayout.getChildAt(0);
            // do processing with textview
        }  
    

    get View Type with getTag and process.

  • Create list which can hold view type of elements in it.

    List<View> view_list = new ArrayList<>();
    

    Create image view of text view whatever you want at runtime. eg.

    ImageView i = new ImageView(contex);
    i.setId(any integer value);
    layout.addView(b1); // add the image view created to the layout you have defined in xml file 
    view_list.add(b1);
    
    

    you can also add rules to the view that if it need to be in right, left below or above the previous view created earlier and stored it in list view.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: