0

I want to add multiple textviews to relativelayout dynamically, for example, when i click a button, a new textview should be added to layout, also i want to change font size, style of these textviews individually, how i can achieve that.thanx

Dany Pop
  • 3,590
  • 2
  • 21
  • 28
user9308577
  • 13
  • 1
  • 5

2 Answers2

0

Create your styles for the textView and add the params dynamically like below

On Button Click

TextView textView = new TextView(this);
textView.setText("Test");
RelativeLayout.LayoutParams layoutParams =
      new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textView.setLayoutParams(layoutParams);
relativeLayout.addView(textView);
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
0

first of all, you have to declare relative layout in XML file like below:

    <RelativeLayout 
            android:id="@+id/rlMain"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    </RelativeLayout>

After that put this code in java file :

RelativeLayout m_rl = (RelativeLayout)findViewById(R.id.rlMain);
final Button addTextViewButton= (Button)findViewById(R.id.addTextViewButton);

LinearLayout LL_Outer = new LinearLayout(this);;
LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation
LL_Outer.setBackgroundColor(color.white); // set background
// set Layout_Width and Layout_Height
LinearLayout.LayoutParams layoutForOuter = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
LL_Outer.setLayoutParams(layoutForOuter);

addTextViewButton.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
        TextView text = new TextView(this);
        text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        text.setText("");

        Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
        text.setTypeface(typeFace);
        text.setTextSize(50);
        LL_Outer.addView(text);

    }
});
   m_rl.addView(text);
Mahesh Keshvala
  • 1,349
  • 12
  • 19
  • You aren't understanding my question, after adding multiple textviews dynamically, now i want to click on a particular textview and want to change its color,size,font etc, how to do that?? – user9308577 Feb 03 '18 at 10:31
  • for that you want to add popup or something like that for set this value to textview,for example you set up popup window into that you added color,size and font 3 edittext after that on click of button you have to set that value to textview which you has clicked. – Mahesh Keshvala Feb 03 '18 at 10:45
  • yes, and if you want to add horizontally text view then make Linear layout orientation horizontal. – Hemant N. Karmur Apr 06 '18 at 12:59