i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:
main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
MainActivity.java:
linearLayout = (LinearLayout) view.findViewById(R.id.result);
.......
public void addItem(){
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
EditText quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
Help me, please.
I've edit my question
Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:
Items Qtt
Cola......1
Chicken...1
Meat......2
Help me.
Do you want to get like this?
String str = item.getText().toString();
String str2 = quantity.getText().toString();
As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.
Try declaring your Views as global variables or doing it outside of the loop such as;
//....
TextView item;
EditText quantity;
for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.
This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.
Anyway. You could add the views to an list as you add them to the linearlayout.
List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();
for(int i = 0; i < cartCount; i++) {
...
childLayout.addView(item);
childLayout.addView(quantity);
items.add(item);
quantities.add(quantity);
linearLayout.addView(childLayout);
}
then loop through your views
for (TextView textView : items) {
}
for (EditText editText : quantities) {
}
item.getText().toString()
quantity.getText().toString()
Related
I have to create textview with json dynamically. For now I successfullyClick Here create but not in proper way.
Like this image but not able to create. I done like that, Click Here
Here is xml code --
<LinearLayout
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:id="#+id/layout_top_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
Here is java code
try {
JSONObject object = new JSONObject(response);
boolean status = object.getBoolean("status");
if (status) {
JSONArray array = object.getJSONArray("topsearches");
for (int i = 0; i < array.length(); i++) {
final TextView textView;
JSONObject jsonObject = array.getJSONObject(i);
String keyword = jsonObject.getString("search-keyord");
String cat_id = jsonObject.getString("category_id");
textView = new TextView(getActivity());
textView.setText(keyword);
textView.setId(i);
textView.setBackgroundResource(R.drawable.border_gray);
textView.setPadding(10, 10, 10, 10);
textView.setTextSize(14);
textView.setLayoutParams(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
getLayout_top_search().addView(textView);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String query = textView.getText().toString();
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
Please help.
All you want is Flow Layout
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Instead of LinearLayout you can use Flow Layout Which helps you to add textview in a row. If the row has no enough space it will extend a new row. And then the layout will become like this.
For more Information: Please read this and this
Please Feel free to ask me.if you are facing any issue.
TextView tvContent = new TextView(getActivity());
medicineName.add(tvContent);
LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvContent.setLayoutParams(tvParam);
tvContent.setGravity(Gravity.LEFT);
tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tvContent.setText(name);
tvContent.setTextSize(16);
layout.addView(tvContent);
use this in onPostExecute() method
LinearLayout layout = new LinearLayout(getActivity());
LinearLayout.LayoutParams lLayoutlayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lLayoutlayoutParams.setMargins(0, 8, 8, 8);
layout.setLayoutParams(lLayoutlayoutParams);
layout.setPadding((int) (fDimRatio * 8), (int) (fDimRatio * 8),
(int) (fDimRatio * 8), (int) (fDimRatio * 8));
layout.setOrientation(LinearLayout.VERTICAL);
TextView tvContent = new TextView(getActivity());
medicineName.add(tvContent);
LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvContent.setLayoutParams(tvParam);
tvContent.setGravity(Gravity.LEFT);
tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tvContent.setText(name);
tvContent.setTextSize(16);
layout.addView(tvContent);
linearLayout.addView(layout);
linearLayout is id of linearlayout in xml
linearLayout = (LinearLayout) view.findViewById(R.id.container);
fDimRatio value is
private float fDimRatio = 1.0f; // /xml
If you want like image 1 then change android:orientation="horizontal"
I have the following LinearLayout, into which I want to insert a number of dynamically generated TableLayouts. This runs without any errors but nothing is appearing on the screen. Why aren't the TableLayouts appearing? How can I generate the TableLayouts and add them to the LinearLayout?
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/lblOverviewText">
</LinearLayout>
This is how I'm generating the TableLayouts:
var linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
foreach (var block in status.blocks)
{
var tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent, TableLayout.LayoutParams.FillParent);
var rowParams = new TableLayout.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent);
var tableLayout = new TableLayout(this);
tableLayout.LayoutParameters = tableParams;
TableRow tableRow = new TableRow(this);
tableRow.LayoutParameters =tableParams;
TextView textView = new TextView(this);
textView.Text = block.Name;
textView.LayoutParameters = rowParams;
tableRow.AddView(textView);
tableLayout.AddView(tableRow, rowParams);
linearLayout.AddView(tableLayout);
}
To start I would delete the alignParentLeft/alignParentRight in the xml for the linearlayout and simply put android:layout_width="match_parent".
You will also need to define the 'orientation' property of the linearlayout in the xml as 'vertical'.
The next part of the problem is the complicated mix of different kinds of layout params, you have correctly identified the need for table and row layout params seperatley but have defined the rowParams variable as a 'new TableLayout.LayoutParams' instead of which it should be 'new TableRow.LayoutParams' but both should have a width of match_parent and a height of wrap_content. Code example below:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
for(int i = 0; i < listItems.length; i++)
{
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(lp2);
tableLayout.setColumnStretchable(0, true);//NOTE: you may not want this if you do not want your textview to always fill the available space
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText(listItems[i]);
textView.setLayoutParams(lp);
tableRow.addView(textView);
tableLayout.addView(tableRow);
linearLayout.addView(tableLayout);
}
Hope this helps.
I am facing one issue for updating TextView content in RelativeLayout. When I update in the second time, I need to clear the previous text from RelativeLayout.
I have one RelativeLayout in my app activity_calendar.xml
<RelativeLayout
android:id="#+id/rel_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1">
</RelativeLayout>
This is the Main Code :
public void listall () {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
int prevTextViewId = 0;
for (int i = 0; i < titleCalendar.length; i++) {
final TextView textView = new TextView(this);
textView.setText(titleCalendar[i]);
cnvrttime = hourCalendar[i] - 3;
addcnvrtTime = 60*cnvrttime;
curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, addcnvrtTime);
params.setMargins(0, convertDPtoInt((float)addcnvrtTime), 0, 0);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
relativeLayout.addView(textView, params);
}
You need to give the TextView an id so you can find it later:
textView.setId(1234);
Then later:
TextView textView = findViewById(1234);
textView.setText("New Text");
If there aren't any child Views inside the relative layout that you want to keep, call relativeLayout.removeAllViews() before your for loop. Otherwise, use removeView(View) or removeViewAt(int) to remove the ones you don't want.
In my app. there is activity contain multiple linear layout and divider which created programmatically , its run fine ,
but i have to duplicate the linear layout and divider 5 times ,and all are the same except two things :
1- each linear layout has different string .
2- first divider margin differ than others divider margin .
is there's better approach to do that with more clean and shorter code .
any help will be much appreciated .
public class Dreams extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.trip);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
TextView tv = (TextView) findViewById(R.id.title);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
tv.setText("Dreams");
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// add text view
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.RIGHT);
tv1.setTextSize(30);
tv1.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv1);
tv1.setText(Html.fromHtml(getString(R.string.dreams)));
ImageView divider1 = new ImageView(this);
LinearLayout.LayoutParams lp1 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp1.setMargins(40, 0, 40, 0);
divider1.setLayoutParams(lp1);
divider1.setBackgroundColor(Color.RED);
ll.addView(divider1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setTextSize(30);
tv2.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv2);
tv2.setText(Html.fromHtml(getString(R.string.dream_1)));
ImageView divider2 = new ImageView(this);
LinearLayout.LayoutParams lp2 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp2.setMargins(10, 10, 10, 10);
divider2.setLayoutParams(lp2);
divider2.setBackgroundColor(Color.RED);
ll.addView(divider2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.RIGHT);
tv3.setTextSize(30);
tv3.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv3);
tv3.setText(Html.fromHtml(getString(R.string.dream_2)));
ImageView divider3 = new ImageView(this);
LinearLayout.LayoutParams lp3 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp3.setMargins(10, 10, 10, 10);
divider3.setLayoutParams(lp3);
divider3.setBackgroundColor(Color.RED);
ll.addView(divider3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.RIGHT);
tv4.setTextSize(30);
tv4.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv4);
tv4.setText(Html.fromHtml(getString(R.string.dream_3)));
ImageView divider4 = new ImageView(this);
LinearLayout.LayoutParams lp4 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp4.setMargins(10, 10, 10, 10);
divider4.setLayoutParams(lp4);
divider4.setBackgroundColor(Color.RED);
ll.addView(divider4);
TextView tv5 = new TextView(this);
tv5.setGravity(Gravity.RIGHT);
tv5.setTextSize(30);
tv5.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv5);
tv5.setText(Html.fromHtml(getString(R.string.dream_4)));
ImageView divider5 = new ImageView(this);
LinearLayout.LayoutParams lp5 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp5.setMargins(10, 10, 10, 10);
divider5.setLayoutParams(lp5);
divider5.setBackgroundColor(Color.RED);
ll.addView(divider5);
TextView tv6 = new TextView(this);
tv6.setGravity(Gravity.RIGHT);
tv6.setTextSize(30);
tv6.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv6);
tv6.setText(Html.fromHtml(getString(R.string.dream_5)));
ImageView divider6 = new ImageView(this);
LinearLayout.LayoutParams lp6 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp6.setMargins(10, 10, 10, 10);
divider6.setLayoutParams(lp6);
divider6.setBackgroundColor(Color.RED);
ll.addView(divider6);
}
}
Since all that is changing is the TextView setText() you can make this a for loop with a list of String inputs. For example:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
String[] textEntries = { getString(R.string.dream),
getString(R.string.dream_1),
getString(R.string.dream_2),
getString(R.string.dream_3),
getString(R.string.dream_4),
getString(R.string.dream_5)
};
for ( int i = 0; i < textEntries.length; i++)
{
TextView tv = new TextView(this);
tv.setGravity(Gravity.RIGHT);
tv.setTextSize(30);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv);
tv.setText(Html.fromHtml(textEntries[i]));
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp.setMargins(10, 10, 10, 10);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.RED);
ll.addView(divider);
}
first of all it would be easier if you define your layouts in XML instead of adding them programmatically. You will profit from the benefits of the UI editor as well. :)
Second, you may want to use ListView and an Adapter to fill the list, since you do not want do duplicate the same tasks for each layout.
Maybe these two links are helpful:
1. http://developer.android.com/guide/topics/ui/declaring-layout.html
2. http://developer.android.com/guide/topics/ui/layout/listview.html
So, to finally answer your question, I would do the following:
Create a file, e.g. list_item.xml, with something like:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"><TextView your attributes.../></LinearLayout>
Create another layout, for instance main.xml, which contains a ListView. You can change the color of the divider like described here How to change the divider color in the listview?.
In your code (activity or fragment) add the main.xml as content view via setContentView().
Also in your code you should then add an adapter to the ListView which then populates the list for you. Here is an example How to customize listview using baseadapter
Finally, and since you separate the concerns (design and code), you could achieve what you want with just a few lines in your activity (the layout stuff would be in the xml and the population could be moved to a separated class like MyAdapter.java...)
Hope that helps...
I want to create multiple TextView boxes dynamically, If i enter 2 in the Edittext it wants to create 2 TextView, If i enter 5 in the EditText it wants to create 5 TextView and so on... can anyone help me...
Thanks in advance.,
first you need to get the parent layout.if it is ,say LinearLayout means,you can add the textview by using addView(view).
letterTextView = new TextView[length];
for (int Index = 0; Index < length; Index++)
{
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 25, 0);
letterTextView[Index] = new TextView(FriendsPanel.this);
letterTextView[Index].setTextSize(20);
letterTextView[Index].setTextColor(Color.WHITE);
letterTextView[Index].setText("TextView"+letterIndex);
linearLayout.addView(letterTextView[Index], layoutParams);
}