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+"
/>
Related
I want to move button up and textview down. Button is moving up but it's comin on textview.
Textview is not coming down.
Button b;
TextView t;
public void btnclick(View v){
b= (Button) v;
t= (TextView) findViewById(R.id.tv);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) t
.getLayoutParams();
params.addRule(RelativeLayout.BELOW, b.getId());
b.setLayoutParams(params);
}
basically, textview and button same dimension in RelativeLayout.
try this code
activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton;
private TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
mTextView = (TextView) findViewById(R.id.textview);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTextView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, v.getId());
mTextView.setLayoutParams(params);
break;
}
}
}
layout:
<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" tools:context=".MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
/>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</RelativeLayout>
Seem you are trying to apply the rule on Button b, instead try applying it on TextView t.
TextView t = (TextView) findViewById(R.id.tv);
public void btnclick(View v){
b = (Button) v;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) t.getLayoutParams();
params.addRule(RelativeLayout.BELOW, b.getId());
t.setLayoutParams(params); // Changed this
}
i have a custom dialog box, it has a textview an edittext and 2 buttons. i'm trying to create a new textview that containes the text of the edittext everytime the user presses on the 'add' button,and locate it beneath the buttons. how can i do that? here's what i tried:
ingDlgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//showDialog(INGS_DIALOG);
final Dialog customIngsDlg = new Dialog(context);
customIngsDlg.setContentView(R.layout.custom_ings_dialog);
customIngsDlg.setTitle("Ingredients");
TextView ingsDlgTitle = (TextView)findViewById(R.id.ingsDialogTitleTv);
final EditText ingsEt = (EditText)customIngsDlg.findViewById(R.id.ingsDialogEt);
final Button ingsAddBtn = (Button)customIngsDlg.findViewById(R.id.ingsAddButton);
final Button ingsFinishBtn = (Button)findViewById(R.id.ingsFinishButton);
ingsAddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ArrayList<String> ingsTmp = new ArrayList<String>();
String ingredient = ingsEt.getText().toString();
ingredients.add(ingredient);
ingsEt.setText("");
TextView ingItemTv = new TextView(customIngsDlg.getContext());
ingItemTv.setText(ingredient);
RelativeLayout layout = new RelativeLayout(OwnRecipeAdding.this);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW,ingsEt.getId());
layout.setLayoutParams(parms);
customIngsDlg.addContentView(ingItemTv,parms);
}
});
// ingsFinishBtn.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
// customIngsDlg.dismiss();
// }
// });
customIngsDlg.show();
}
});
I would suggest adding the text view to your custom layout in the .xml file and then add text to that when the add button is pressed.
This needs a bit of styling, but it's functional. You'll likely need a scroll view in there when the list becomes too long.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_fragment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/dialog_textview_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Add Ingredients" />
<EditText
android:id="#+id/dialog_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_add"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="#+id/button_finish"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
</LinearLayout>
<TextView
android:id="#+id/dialog_textview_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="" />
</LinearLayout>
DialogFragment class:
public class TestDialogFragment extends DialogFragment {
private Button buttonAdd;
private Button buttonFinish;
private TextView addTextView;
private EditText editText;
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_fragment, null);
buttonAdd = (Button)dialoglayout.findViewById(R.id.button_add);
buttonFinish = (Button)dialoglayout.findViewById(R.id.button_finish);
editText = (EditText)dialoglayout.findViewById(R.id.dialog_edittext);
addTextView = (TextView)dialoglayout.findViewById(R.id.dialog_textview_2);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text = addTextView.getText().toString();
text = text + "\n" + editText.getText().toString();
addTextView.setText(text);
editText.setText("");
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout)
.setTitle("Ingredients")
.setCancelable(false)
.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
return dialog;
}
}
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 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));
}
}}
I have designed a layout as in the image below:
After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:
I want to show the Button on the right side of the TextView. How should I do this?
Another question, how should I remove corresponding View when user clicks a button? The code:
public class ExampleActivity extends Activity {
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(new OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText()
.toString()));
mLayout.addView(createNewButton());
}
});
}
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;
}
private Button createNewButton() {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(lparams);
button.setText(" - ");
return button;
}
}
The TextViews and Buttons are stacked because you probably use a LinearLayout with the orientation vertical. You could wrap your TextView + Button into a LinearLayout and then add this LinearLayout to your own layout or you could use a TableLayout like below(I've added some ids so you can delete the rows you want):
public class SomeActivity extends Activity {
private EditText mInput;
private TableLayout mTable;
private static int sCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addButton = (Button) findViewById(R.id.add);
mInput = (EditText) findViewById(R.id.editText1);
mTable = (TableLayout) findViewById(R.id.table1);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTable.addView(addRow(mInput.getText().toString()));
}
});
}
private TableRow addRow(String s) {
TableRow tr = new TableRow(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
TableRow.LayoutParams blparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
}
where the main layout file is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
</ScrollView>
If for some reason, you don't like the TableLayout use a LinearLayout to wrap you TextView and Button with the layout file above(and of course remove the TableLayout):
//...
ll = (LinearLayout) findViewById(R.id.parent);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//where ll is the LinearLayout with the id parent
ll.addView(addRow(mInput.getText().toString()));
}
});
}
private LinearLayout addRow(String s) {
LinearLayout tr = new LinearLayout(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams tlparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
LinearLayout.LayoutParams blparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
ll.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
LinearLayout having the property Orientation to align control either Vertically/Horizontally
so just set Orientation of the same
http://developer.android.com/reference/android/widget/LinearLayout.html
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.setOrientation(LinearLayout.HORIZONTAL);
Updated
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText"
android:layout_width="293dp"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+" />
</LinearLayout>