I have a layout and i need to insert into in some kind of blocks with text - like comments date, name, text. Is there a way to use it like this
for (int r=0; r<10;r++) {
TextView t1=(TextView) findviewbyid(R.id.text1);
t1="BlaBla";
mainlayout.add(commentLayout);
}
So i get something like listview but without adapter and etc.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/comm_name"
android:layout_marginLeft="20dp"
android:layout_marginTop="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:id="#+id/comm_date"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_margin="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:id="#+id/comm_text" />
you can do it, but the way you are doing it will throw an exception, because findViewById returns always the same object, and after the first iteration t1 has already a parent. You should create a new instance of the TextView programmatically, at every iteration.
Try adding views dynamically in linear layout which is inside scroll view.
Try this code.I know it is not exact solution.But,Hope it gives you some idea.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
Use Following approach
Looping to add dynamically Views to Linear Layout
TextView textView1, textView2;
LinearLayout completeLinearLayout = (LinearLayout) findViewById(R.id.ll);
for (int i = 0; i < 10; i++) {
textView1 = new TextView(Activity.this);
textView2 = new TextView(Activity.this);
String name1 = "xyz";
String name2 = "abc"
completeLinearLayout.addView(textView1);
completeLinearLayout.addView(textView2);
setTextViewParam(textView1, name1);
setTextViewParam(textView2, name2);
}
Method to set TextView Parameters Dynamically
private void setTextViewParam(TextView tv, String text) {
LayoutParams textLayoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textLayoutParams.setMargins(5, 0, 0, 0);
tv.setGravity(Gravity.LEFT);
tv.setText(text);
tv.setTextSize(13);
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(textLayoutParams);
}
Now you can customize the views according to need and container as well.
Happy Coding!!
So, this code works exactlay as i need.
Is there anything wrong with it??
public class InflateView extends Activity {
LinearLayout lLayout;
TextView t,f;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lLayout = (LinearLayout)findViewById(R.id.layout1);
for (int i=0;i<10;i++)
{
RelativeLayout tv = (RelativeLayout)inflater.inflate(R.layout.text, null);
lLayout.addView(tv);
t = (TextView) tv.findViewById(R.id.ttt);
t.setText("aaaaaaa" + i) ;
f = (TextView) tv.findViewById(R.id.textView);
f.setText(String.valueOf(i));
}
}}
Related
I have an Xml that add LinearLayout and RelativeLayout in ScrollView by programmatically.When i add Text with OnclickButton for first time show me message but for 2nd time get me crash :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
My code :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
sclView = (ScrollView) findViewById(R.id.scrollID);
Button btn = (Button) findViewById(R.id.btnSend);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
relativeLayout = new RelativeLayout(context);
firstLinearLayout= new LinearLayout(context);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
Crash here now======>sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
I need that when i click on Button and send message for 2nd time create a new RelativeLayout in LinearLayout for show.(In scrollView)
Error :
AndroidRuntime
MainActivity$1.onClick(MainActivity.java:54)
mmmmm. I recommend that you using from table and you can add programmatically your TextView and etc. simply :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
TextView txtviewUserT;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
TableRow tr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
final TableLayout tl = (TableLayout) findViewById(R.id.tbl);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUserT = new TextView(MainActivity.this);
txtviewUserT.setText("OK");
txtviewUser.setText(Medtconversation);
tr = new TableRow(context);
tr.addView(txtviewUser);
tr.addView(txtviewUserT);
tl.addView(tr);
edtconversation.setText("");
}
});
}
}
Your XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TableLayout
android:id="#+id/tbl"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
First you need to add the LinearLayout to ScrollView
Then add the relative layout to the LinearLayout
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
sclView = (ScrollView) findViewById(R.id.scrollID);
firstLinearLayout= new LinearLayout(this);
relativeLayout = new RelativeLayout(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
sclView.addView(firstLinearLayout);
firstLinearLayout.addView(relativeLayout);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
You are creating a new LinearLayout and RelativeLayout only once (inside onCreate), however you add them multiple times. Instead create new instances from inside of the onClick method:
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LinearLayout firstLinearLayout= new LinearLayout(context);
RelativeLayout relativeLayout = new RelativeLayout(context);
...
firstLinearLayout.addView(relativeLayout);
...
}
});
Description::
On click of button I want to add a textview dynamically on run-time
I don't want to use a textview widget(from xml)
I want to generate textviews programatically
Is it possible ?
If so how ?
MainActivity.java
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Container" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:text="Button" />
</RelativeLayout>
Just
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.parent_layout);
TextView textView = new TextView(MainActivity.this);
layout .addView(textView);
}
});
You can create a textView using the context of your activity.
example:
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
TextView tv = new TextView(MainActivity.this);
relativeLayout.addView(tv,lp);
}
});
Simple example, including layout params (width, height, etc):
// instantiate the new view - int his example, a textview
TextView label=new TextView(context);
label.setText(labelText);
// new LayoutParams, specify the height as WRAP_CONTENT
// width is 0 in this example as we are using the layout weight (2)
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT,2
);
// margins
lp.rightMargin=5;
lp.leftMargin=5;
// apply the layout params to this view
label.setLayoutParams(lp);
// finally, add the view to the container view
content.addView(label);
Something like this:
// Create the TextView
TextView textView = new TextView(this);
// Create some parameters
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
// Get your relative layout by its id
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
// Add the newly created TextView to the layout
relativeLayout.addView(textView, p);
TextView in a LinearLayout:
LinearLayout layout = new LinearLayout(this);
TextView textview = new TextView(this);
textview.setText("Hellow, I'm a TextView!");
layout.addView(textview);
this.setContentView(layout);
I have created dynamically Table Layout. However, my Title Row pushes downward when other rows are dynamically created. I want to make the title stay at the top.
The XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstScreen"
android:orientation="vertical"
android:background="#color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Shopping Cart"
android:textColor="#898989"
android:textSize="17dp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/displayLinear"
android:layout_marginLeft="10dp"
android:background="#ffffff"
android:orientation="vertical" >
</TableLayout>
<Button
android:id="#+id/second"
style="#style/btnStyleShakespeare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="10dp"
android:text="Checkout"
android:textColor="#ffffff" />
<Button
android:id="#+id/scanMore"
style="#style/btnStyleShakespeare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:text="Scan More"
android:textColor="#ffffff" />
</LinearLayout>
This is my dynamically created code.
package info.androidhive.slidingmenu;
public class ScreenFirstFragment extends Fragment {
public ScreenFirstFragment(){}
public static double pFinalPrice;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_firstscreen, container, false);
//final LinearLayout lm = (LinearLayout) rootView.findViewById(R.id.linearMain);
final Button secondBtn = (Button) rootView.findViewById(R.id.second);
final Button scanMoreBtn = (Button) rootView.findViewById(R.id.scanMore);
//Get Global Controller Class object (see application tag in AndroidManifest.xml)
final Controller aController = (Controller) getActivity().getApplicationContext();
/******* Create view elements dynamically and show on activity ******/
//Product arraylist size
int ProductsSize = aController.getProductsArraylistSize();
/******** Dynamically create view elements - Start **********/
TableLayout ll = (TableLayout) rootView.findViewById(R.id.displayLinear);
ll.setStretchAllColumns(true);
ll.setShrinkAllColumns(true);
TableRow rowProdLabels = new TableRow(this.getActivity());
// Product column
TextView prodLabel = new TextView(this.getActivity());
prodLabel.setText("PRODUCT");
prodLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(prodLabel);
// Price column
TextView priceLabel = new TextView(this.getActivity());
priceLabel.setText("PRICE");
priceLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(priceLabel);
// Quantity column
TextView quantityLabel = new TextView(this.getActivity());
quantityLabel.setText("Quantity");
quantityLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(quantityLabel);
// Total column
TextView totalLabel = new TextView(this.getActivity());
totalLabel.setText("Total Price");
totalLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(totalLabel);
ll.addView(rowProdLabels);
for(int j=0;j< ProductsSize;j++)
{
// Get probuct data from product data arraylist
String pName = aController.getProducts(j).getProductName();
double pPrice = aController.getProducts(j).getProductPrice();
int pQuantity = aController.getProducts(j).getProductQuantity();
double pTotalPrice = pPrice * pQuantity;
TableRow row= new TableRow(this.getActivity());
TextView product = new TextView(this.getActivity());
product.setText(pName+" ");
//Add textView to LinearLayout
row.addView(product);
TextView price = new TextView(this.getActivity());
price.setText("RM "+pPrice+" ");
//Add textView to LinearLayout
row.addView(price);
TextView quantity = new TextView(this.getActivity());
quantity.setText(pQuantity+" ");
//Add textView to LinearLayout
row.addView(quantity);
TextView totalprice = new TextView(this.getActivity());
totalprice.setText(String.format("RM %.2f",pTotalPrice));
//Add textView to LinearLayout
row.addView(totalprice);
//Update final price
pFinalPrice += pTotalPrice;
ll.addView(row,j);
}
/******** Dynamically create view elements - End **********/
secondBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ScreenSecondFragment(), "Cart");
mFragmentTransaction.commit();
}
});
scanMoreBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ScanFragment(), "Scan Product");
mFragmentTransaction.commit();
}
});
return rootView;
}
}
I'm trying to use two buttons to dynamically add and remove linear layout.
protected void createT () {
// -----------------------------------------------
count++;
LinearLayout temp_ll, frame;
frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
EditText temp1, temp2;
for (int i=0; i<numClass; i++) {
temp_ll = new LinearLayout(this);
temp_ll.setOrientation(LinearLayout.HORIZONTAL);
temp1 = new EditText(this);
temp2 = new EditText(this);
temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
temp1.setHint("class name");
temp2.setHint("grade");
temp_ll.addView(temp1);
temp_ll.addView(temp2);
frame.addView(temp_ll);
}
ll.addView(frame);
}
protected void deleteT() {
// --------------------------------------
if (count > 0) {
LinearLayout temp = new LinearLyout(this);
temp = (LinearLayout) findViewById(count);
temp.removeAllViews();
temp.setVisibility(View.GONE);
count--;
}
}
Every time I press add button, it calls the createT()
Every time i press del button it calls the deleteT()
The problem is that I'm using count variable to keep track of linearLayout IDs.
First time when I press add button x times and then press del button everything is fine.
THE PROBLEM IS THAT AFTER PRESSING ADD SECOND TIME .
I checked your code and I found a little mistake inside the deleteT() method, you remove all view inside the LinearLayout, but you didn't remove the actual layout from the root layout.
And an another suggestion, you don't need a new LinearLayout instance when you want to use findviewbyid method, and use ll.removeView(View) method to delete the dynamic LinearLayout container.
Here is the code:
private int count;
private LinearLayout ll;
private final int numClass = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
}
public void createT(View view) {
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
for (int i = 0; i < numClass; i++) {
final LinearLayout temp_ll = new LinearLayout(this);
final EditText temp1;
final EditText temp2;
temp_ll.setOrientation(LinearLayout.HORIZONTAL);
temp1 = new EditText(this);
temp2 = new EditText(this);
temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
temp1.setHint("class name");
temp2.setHint("grade");
temp_ll.addView(temp1);
temp_ll.addView(temp2);
frame.addView(temp_ll);
}
ll.addView(frame);
}
public void deleteT(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
}
}
And the layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/main_linearlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="createT" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
android:onClick="deleteT" />
</LinearLayout>
I hope it helps!
So right now I have a text field with a button (add+) below it.
I would like to make it so every time text is entered into the Text Field, and the Add button is pressed, a new text view is added to a vertical layout below it with the text that the user typed in the field.
I do not want to simply make a text view invisible, then visible when clicked, because I would like them to be able to add more than one text view with whatever text they type.
This code contains what you want. (The view show an EditText and a Button, after you click on the button the text will add to the LinearLayout)
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
And the xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+"
/>