How to create UI elements dynamically

Hi guys! How Can I create UI elements dynamically on Android? I need to build dynamic questionnaires from a XML file, so I need to create UI through code. Someone here did that before? Thanks!

Posts

  • bmoultonbmoulton USMember, Xamarin Team Xamurai

    @JacksonSantana

    You can do so by creating instances of widgets in code and adding them to ViewGroups using ViewGroup.AddView. Classes such as LinearLayout or RelativeLayout are examples of ViewGroups.
    http://androidapi.xamarin.com/?link=T:Android.Views.ViewGroup/*

  • Jackson.2976Jackson.2976 BRMember

    Have some example of that method? Sorry, I'm new on that...

  • Jackson.2976Jackson.2976 BRMember

    Thank you! I did it using

    LinearLayout principalview = FindViewById(Resource.Id.mainlayout);

                    LinearLayout.LayoutParams parametros = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
    

    var valueB = new Button(this);
    valueB.Text = "Teste";
    valueB.SetBackgroundColor(Color.Aqua);
    principalview.AddView(valueB, parametros);

    that's a good start!

    Thank you guys!

  • Jackson.2976Jackson.2976 BRMember

    Can I do that in a Fragment?

  • CheesebaronCheesebaron DKInsider, University mod

    You should be able to do that in OnCreateView in a Fragment. Instead of calling SetContentView in the end just return it instead.

  • Jackson.2976Jackson.2976 BRMember

    I think a Easier way to do that is getting the context of a fragment, how can I do that? I saw something about "getActivity()" but I don't know where to use that.

  • Jackson.2976Jackson.2976 BRMember

    Could you post an example? I'm not finding that on Activity property...

  • CheesebaronCheesebaron DKInsider, University mod
    public class MyFragment : Fragment
    {
        public override View OnCreateView(LayoutInflater i, ViewGroup c, Bundle b)
        {
            var activity = Activity;
            //Do stuff with it...
        }
    }
    

    It should be available in most of the overrideable methods

  • Jackson.2976Jackson.2976 BRMember

    The method "getActivity()" don't appear, probably I'm doing something wrong...

    class SampleTabFragment: Fragment
            {            
                public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    base.OnCreateView (inflater, container, savedInstanceState);
    
                    var a = Activity;
    
                    a. ???? (Don't appear a "getActivity()" method)
                }
            }
    
  • Jackson.2976Jackson.2976 BRMember

    Sorry:

    class SampleTabFragment: Fragment
            {            
                public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    base.OnCreateView (inflater, container, savedInstanceState);
    
                    var a = Activity;
    
                    a.OnMenuItemSelected(
    
                    var view = inflater.Inflate (Resource.Layout.Tab, container, false);
                    var sampleTextView = view.FindViewById<TextView> (Resource.Id.sampleTextView);             
                    sampleTextView.Text = "sample fragment text";
    
                    return view;
                }
            }
    
  • SKallSKall USMember ✭✭✭✭

    The property Activity is the same as the return value for Java call getActivity(). In other words you don't need getActivity().

  • Jackson.2976Jackson.2976 BRMember

    Worked! I returned the Activity on onCreateView... Thank you guys!

  • DnyaneshwarWadghaneDnyaneshwarWadghane USMember
    edited March 2014

    var aLabel = new TextView (this);
    aLabel.Text = "Hello Text!!!";
    aLabel.SetTextSize (Android.Util.ComplexUnitType.Dip, 15f);
    RelativeLayout ll = new RelativeLayout(this);
    ll.AddView(aLabel);

  • if the views are created dynamically how we can reference them in order to remove them?

  • CheesebaronCheesebaron DKInsider, University mod

    @ChristopherDrosos‌ either give them an ID you can find them with or keep a reference to the actual view in a list, dictionary or whatever.

  • androidexampleandroidexample INMember

    Hi
    I have found one good example here
    Dynamically Create View Elements

  • androidexampleandroidexample INMember

    Hi
    I have found one good example here
    Dynamically Create View Elements

  • Android123Android123 INMember

    Hello friends i have found some good tutorial here......
    < a href="http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Exampleindex.php?view=article_discription&aid=115&aaid=137" >Dynamically Create View Elements

  • ChalMcColloughChalMcCollough USMember

    Seriously you guys, if your going to post a source code example, include the namespaces your using, or don't post the example at all. @SKall: Your example looks great, only WFT is a List??? You mean List??? If u didn't include the namespace, then I don't know what your talking about. Include your namespaces and only post working code snippets, or don't code example at all. If you post non-working code examples, it makes you look sloppy and makes engineers mad because you just wasted my time.

  • gabooboogabooboo USMember ✭✭

    @Harshadcse said:
    Hii.. @JacksonSantana

    You can try like in below Way :

    var layout = new LinearLayout (this);
    layout.Orientation = Orientation.Vertical;
    
    var aLabel = new TextView (this);
    aLabel.Text = "Hello, World!!!";
    
    var aButton = new Button (this);
    aButton.Text = "Say Hello!";
    
    aButton.Click +=(sender, e) => 
    {aLabel.Text="Hello Android!";};
    
    layout.AddView (aLabel);
    layout.AddView (aButton);
    SetContentView (layout);  
    

    This is a simple way UI elements dynamically without layout use..

    Thanks!!!
    Harshad

    Thank you Harshad, I solved with your suggestions.

  • How do I add the fragment to the MainActivity Layout?

    Thanks,
    Reydan

  • PareshSukhiyaPareshSukhiya USMember ✭✭
    edited October 6

    @Harshadcse said:
    Hii.. @JacksonSantana

    You can try like in below Way :

    var layout = new LinearLayout (this);
    layout.Orientation = Orientation.Vertical;
    
    var aLabel = new TextView (this);
    aLabel.Text = "Hello, World!!!";
    
    var aButton = new Button (this);
    aButton.Text = "Say Hello!";
    
    aButton.Click +=(sender, e) => 
    {aLabel.Text="Hello Android!";};
    
    layout.AddView (aLabel);
    layout.AddView (aButton);
    SetContentView (layout);  
    

    This is a simple way UI elements dynamically without layout use..

    Thanks!!!
    Harshad

    @Harshadcse how can i add id to textview using your method!?
    and how do i set the image path of an image
    the image is stored in as resource/drawable/a.png

Sign In or Register to comment.