Wednesday, September 2, 2015

Display View (Textview, Images and Buttons) at runtime on Android



 
Here are the simple steps

Get yourself  Layout in our case its Linear


/
//linearLayout -->BLUE
LinearLayout linearLayout=new LinearLayout(this);
             
               linearLayout.setOrientation(LinearLayout.HORIZONTAL);
               //vp  --> RED
               LinearLayout.LayoutParams vp=new  
               LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 
create your views  [TextView, Button and Image] and add them to the  created linear layout àvp object for Text and button and àiv object for image

TextView tv = new TextView(this);
              tv.setText("tic");
          linearLayout.addView(tv,vp);

Button btn = new Button(this);
              btn.setVisibility(View.VISIBLE);
              btn.setText("ClickToStart");
          linearLayout.addView(btn, vp);

ImageView imageView= new ImageView(this);
             
              imageView.setImageResource(R.drawable.ic_launcher);
              imageView.setVisibility(View.VISIBLE);
          imageView.setBackgroundColor(0xFFFF00FF);
//Iv --> GREEN
LinearLayout.LayoutParams Iv=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
              linearLayout.addView(imageView,Iv);
          setContentView(linearLayout,vp);

more than happy to read comments (not harsh i suppose)
until next time.