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
Asked
Active
Viewed 1,006 times
0
-
first initiate the textviews with color and size in xml then in your code add your Dynamic Data... – Whats Going On Feb 03 '18 at 09:34
2 Answers
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
-
Problem is, after adding textviews, how i can change color, size,font, to a particular text, suppose i've added 3 textviews dynamically, now i want to change font, color size to only one particular textview!! – user9308577 Feb 03 '18 at 10:27
-
You can set the color,font and size before adding into the Relative layout. – Raja Jawahar Feb 03 '18 at 10:29
-
After clicking on particular text, i want to change its attributes, that is the problem i'm facing!! – user9308577 Feb 03 '18 at 10:33
-
Set an Id and Listeners for the textview whatever u had added.. After clicking get the view and cast it to TextView and change the attributes programatically.. – Raja Jawahar Feb 03 '18 at 10:35
-
-
Okie https://gist.github.com/rajajawahar/d6c4b9d47ac54ba0ab38576d1fc035d6 – Raja Jawahar Feb 03 '18 at 10:43
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