Here I have to add text view programmatically based on array list size. Text views should be appear in row like continues pattern...
eg. tv1, tv2, tv3 and so on till the size of array list.
But here I am getting text views which are appearing on each other. I can't read the text on them. Here is my code:
ArrayList<String> languageNames = new ArrayList<String>();
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
if(languageNames.size()>0)
{
int size = languageNames.size();
TextView[] tv = new TextView[size];
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
for(int i=0; i<size; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setLayoutParams(p);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
}
else
{
}
what needs to be done so that I can get text views in appropriate manner?
Add buttons inside a LinearLayout and add this LinearLayout in the RelativeLayout.
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.r1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
LinearLayout LL = new LinearLayout(getBaseContext());
LL.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i< size;i++) {
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
LL.addView(tv);
}
r1.addview(LL, p);
Try this code:
LinearLayout rl = (LinearLayout)findViewById(R.id.mainLayout);
TextView[] tv = new TextView[10];
for(int i=0; i<10; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText("TextView "+ i);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
Hope this will help you
Related
i'm writing progrmicaly adding Linearlayout and Textview into ScrollView. but my ScrollView could not scroll to height.
My programicall Code:
if (cursor.moveToFirst()) {
do {
String last_ID = cursor.getString(cursor.getColumnIndex("lastId"));
String smsBody = cursor.getString(cursor.getColumnIndex("smsBody"));
String senderName = cursor.getString(cursor.getColumnIndex("senderName"));
String date[] = cursor.getString(cursor.getColumnIndex("receiveDate")).split("/");
CalendarTool ct =
new CalendarTool(
Integer.valueOf(date[0]),
Integer.valueOf(date[1]),
Integer.valueOf(date[2])
);
String IranianDate = ct.getIranianDate();
ScrollView SV =new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.parseColor("#f7cbad"));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 10);
ll.setLayoutParams(params);
TextView TV_IranianDate = new TextView(this);
TV_IranianDate.setText(IranianDate);
TV_IranianDate.setTextColor(Color.parseColor("#ffffff"));
TV_IranianDate.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
ll.addView(TV_IranianDate);
TextView TV_smsBody = new TextView(this);
TV_smsBody.setText(smsBody);
TV_smsBody.setGravity(Gravity.RIGHT);
TV_smsBody.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
TV_smsBody.setPadding(5, 5, 5, 5);
ll.addView(TV_smsBody);
TextView TV_spacer2 = new TextView(this);
TV_spacer2.setBackgroundColor(Color.parseColor("#000000"));
TV_spacer2.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
1)
);
ll.addView(TV_spacer2);
SV.addView(ll);
((LinearLayout) linearLayout).addView(SV);
} while (cursor.moveToNext());
}
I guess the problem is the ScrollView object. You should use HorizontalScrollView instead, like this for example:
HorizontalScrollView SV = new HorizontalScrollView(this);
SV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
like described in the API:
ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.
Dynamic linearlayout weight is not setting properly, refer the following coding
LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
1.0f);
mainparams.setMargins(0, 10, 0, 0);
LinearLayout li = new LinearLayout(Appointmentdetails.this);
li.setOrientation(LinearLayout.HORIZONTAL);
li.setLayoutParams(mainparams);
LinearLayout text = new LinearLayout(Appointmentdetails.this);
text.setWeightSum(1);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
2.0f);
text.setLayoutParams(lparams);
tv = new TextView(this);
tv.setTextSize(18);
tv.setTypeface(MyriadPro);
tv.setLayoutParams(lparams);
tv.setText(Alldata.question.get(i).getgQuestion());
tv.setSingleLine();
tv.setPadding(25, 0, 0, 0);
tv.setMarqueeRepeatLimit(10);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSelected(true);
text.addView(tv);
li.addView(text);
LinearLayout Spi = new LinearLayout(Appointmentdetails.this);
Spi.setWeightSum(1);
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
1f);
Spi.setLayoutParams(lparams1);
RadioGroup rg = new RadioGroup(Appointmentdetails.this);
// rg.setLayoutParams(lparams1);
gAnswerList.add("Select");
for (int j = 0; j < helptext.split(Expressions).length; j++) {
RadioButton rb = new RadioButton(Appointmentdetails.this);
rb.setText(helptext.split(Expressions)[j].toString());
rb.setId(j);
rb.setLayoutParams(lparams1);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton rb = (RadioButton) rg.getChildAt(i);
if (rb.getId() == checkedId) {
CharSequence text = rb.getText();
mSelectedRadioValue = text + ",";
gAnswerList.set(i2, text.toString());
// do something with text
return;
}
}
}
});
Spi.addView(rg);
li.addView(Spi);
mQuestionLayout.addView(li);
}
}
This is how i have added a radio button inside the dynamic linear layout, i dont know whats wrong in my coding its not properly aligned, kindly suggest me to get the solution. Thanks in advance.
I would attempt setting the layout params to center. Your pretty much just giving them free flow and setting wherever they like with WRAP_CONTENTS. Yes, they will not fill the entire screen which is not what you want, but try passing something like Try doing something like this
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
2.0f, lparams.Gravity.CENTER);
Try this for RadioButton and text alignment
LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT
);
LinearLayout li = new LinearLayout(Appointmentdetails.this);
li.setOrientation(LinearLayout.HORIZONTAL);
li.setLayoutParams(mainparams);
li.setWeightSum(2);
LinearLayout.LayoutParams mainparams1 = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT
);
mainparams1.weight = 1;
TextView tv = new TextView(this);
tv.setTextSize(18);
tv.setLayoutParams(mainparams1);
tv.setText("text ");
RadioGroup rg = new RadioGroup(Appointmentdetails.this);
rg.setLayoutParams(mainparams1);
rg.setOrientation(RadioGroup.VERTICAL);
for (int j = 0; j < helptext.split(Expressions).length; j++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
RadioButton rb = new RadioButton(Appointmentdetails.this);
rb.setText(helptext.split(Expressions)[j].toString());
rb.setId(j);
rb.setLayoutParams(params );
rg.addView(rb);
}
li.addView(tv);
li.addView(rg);
I'm developing an Android 3.1 Tablet application with fragments.
I've seen that only two fragments are on memory at the same time. When I show a third one, first one calls onDestroyView.
I add EditText to fragment's view programmatically. Those EditText don't show again after fragment's view recreation on onResume method.
I use those EditText to let users add data to a form and I store a reference in firstTable HashMap. I will use that HashMap to retrieve user's values.
Here I create those EditText programmatically:
private LinearLayout createNewFirstTableRow(long articleId)
{
LinearLayout layout = new LinearLayout(mActivity);
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
parentParams.weight = 1;
layout.setLayoutParams(parentParams);
if (firstTable == null)
firstTable = new HashMap<Long, ArrayList<EditText>>();
ArrayList<EditText> fields = new ArrayList<EditText>(7);
LayoutParams params = new LayoutParams(0, 40);
params.weight = 0.125f;
TextView textView = new TextView(mActivity);
textView.setLayoutParams(params);
textView.setText(new Long(articleId).toString());
layout.addView(textView);
for (int i = 0; i < 7; i++)
{
EditText edit = new EditText(mActivity);
edit.setLayoutParams(params);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
fields.add(i, edit);
layout.addView(edit);
}
firstTable.put(new Long(articleId), fields);
return layout;
}
firstTable variable is a global variable: private HashMap<Long, ArrayList<EditText>> firstTable;.
To add my EditText I do the following on onResume:
#Override
public void onResume()
{
Log.v("QuantityFragment", "onResume: " + firstTableRowIndex);
if ((firstTable != null) && (secondTable != null))
{
firstTableRowIndex = FIRST_TABLE_ROW_INDEX;
secondTableRowIndex = SECOND_TABLE_ROW_INDEX;
LinearLayout table = (LinearLayout)view.findViewById(R.id.quantityTable);
for (int index = 0; index < firstTable.size(); index++)
{
Long articleId = articleIds.get(index);
table.addView(resumeTable(articleId, secondTable.get(articleId)), secondTableRowIndex);
table.addView(resumeTable(articleId, firstTable.get(articleId)), firstTableRowIndex);
firstTableRowIndex++;
secondTableRowIndex++;
}
}
super.onResume();
}
private LinearLayout resumeTable(Long articleId, ArrayList<EditText> fields)
{
LinearLayout layout = new LinearLayout(mActivity);
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
parentParams.weight = 1;
layout.setLayoutParams(parentParams);
LayoutParams params = new LayoutParams(0, 40);
params.weight = 0.125f;
TextView textView = new TextView(mActivity);
textView.setLayoutParams(params);
textView.setText(new Long(articleId).toString());
layout.addView(textView);
for (int index = 0; index < fields.size(); index++)
{
layout.addView(fields.get(index));
}
return layout;
}
But, here layout.addView(textView); I get an exception:
IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Is there another way to re-add those EditText?
UPDATE:
I have solved my problem changing resumeTable:
private LinearLayout resumeTable(Long articleId, ArrayList<EditText> fields)
{
LinearLayout layout = new LinearLayout(mActivity);
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
parentParams.weight = 1;
layout.setLayoutParams(parentParams);
LayoutParams params = new LayoutParams(0, 40);
params.weight = 0.125f;
TextView textView = new TextView(mActivity);
textView.setLayoutParams(params);
textView.setText(new Long(articleId).toString());
layout.addView(textView);
for (int index = 0; index < fields.size(); index++)
{
LinearLayout parent = (LinearLayout)fields.get(index).getParent();
parent.removeView(fields.get(index));
layout.addView(fields.get(index));
}
return layout;
}
This is the important part:
for (int index = 0; index < fields.size(); index++)
{
LinearLayout parent = (LinearLayout)fields.get(index).getParent();
parent.removeView(fields.get(index));
layout.addView(fields.get(index));
}
The question is open, if you have a better solution, please, let me know.
That exception is thrown because you store references to Views(the EditText) that were added to the layout and then later you're again re-adding those Views to a newly constructed parent.
Regarding a solution, I don't know why you decided to store references to those EditTexts. The only data that I see worth storing from those EditTexts is the text entered by the user, in which case you should store that text instead of that particular EditText .Your method would be:
//...
if (firstTable == null) {
// your HashMap now stores text instead of an EditText
firstTable = new HashMap<Long, ArrayList<String>>();// store only the text from the EditText
}
ArrayList<String> fields = new ArrayList<String>(7);
LayoutParams params = new LayoutParams(0, 40);
params.weight = 0.125f;
TextView textView = new TextView(mActivity);
textView.setLayoutParams(params);
textView.setText(new Long(articleId).toString());
layout.addView(textView);
for (int i = 0; i < 7; i++) {
EditText edit = new EditText(mActivity);
edit.setLayoutParams(params);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
fields.add(i, ""); // the EditText are empty at first
layout.addView(edit);
}
firstTable.put(new Long(articleId), fields);
Then when is time to restore the EditTexts:
private LinearLayout resumeTable(Long articleId, ArrayList<String> fields) {
LinearLayout layout = new LinearLayout(mActivity);
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
parentParams.weight = 1;
layout.setLayoutParams(parentParams);
LayoutParams params = new LayoutParams(0, 40);
params.weight = 0.125f;
TextView textView = new TextView(mActivity);
textView.setLayoutParams(params);
textView.setText(new Long(articleId).toString());
layout.addView(textView);
for (int index = 0; index < fields.size(); index++) {
// create new EditTexts
EditText edit = new EditText(mActivity);
edit.setLayoutParams(params);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setText(fields.get(index)); // get the text coresponding to this particular EditText
layout.addView(edit);
}
return layout;
}
Of course when the user enters something in the EditTexts you should store it in the firstTable variable at the right position.
I think problen is due to adding the element of fields twice
ArrayList<EditText> fields = new ArrayList<EditText>(7);
1 - in createNewFirstTableRow
for (int i = 0; i < 7; i++)
{
EditText edit = new EditText(mActivity);
edit.setLayoutParams(params);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
fields.add(i, edit);//<-----------
layout.addView(edit);//<----------- added in layout
}
2- onResume()->resumeTable
for (int index = 0; index < fields.size(); index++)
{
layout.addView(fields.get(index));//<----------------
}
when fields element alreay added on the screen you can't add that twice.......
In my array of buttons i want to place the text of each button to be placed in center.I have use Gravity but it doesnt work. The code i have use is.Please anyone help me to solve this out.
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
//Create Button
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setTextSize(12);
m_pBtnDay[i][j].setGravity(Gravity.CENTER);
rowLayout.addView(m_pBtnDay[i][j], param);
m_pBtnDay[i][j].setOnLongClickListener(this);
m_pBtnDay[i][j].setOnClickListener(this);
m_pBtnDay[i][j].setOnTouchListener(this);
//save button position
m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
}
}
try this: m_pBtnDay[i][j].getPaint().setTextAlign(Paint.Align.CENTER);
I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++) {
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
ll.addView(cb);
}
So how can i put the output on the bottom of the screen linearly.
You should use LinearLayout to automatically add one TextView after another.
Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:
public class HelloWorld extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
Random rnd = new Random();
int prevTextViewId = 0;
for(int i = 0; i < 10; i++)
{
final TextView textView = new TextView(this);
textView.setText("Text "+i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
layout.addView(textView, params);
}
}
}
You've to provide the location of your newly added view. As #Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
If you want to show multiple textviews one after the other, then you should go with LinearLayout.
You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++)
{
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
cb.setId(2000+i);
RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
ll.addView(cb,TextViewLayoutParams);
}