Android - Add Textview to Layout When Button Is Pressed

Android - Add textview to layout when button is pressed

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+"
/>

Adding the textview on button click

Ok I have changes a bit of your code and added an extra layout which you can remove after..

Items_momo.java

public class Items_momo extends Activity {
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items_momo);

ArrayList<String> list = new ArrayList<String>();
list.add("Veg Steam Momo");
list.add("Veg C-Momo");
list.add("Veg Fry Momo");
list.add("Veg Kothey Momo");
list.add("Buff Steam Momo");
list.add("Buff Fry Momo");
list.add("Buff Kothey Momo");
list.add("Buff C-Momo");
list.add("Chicken Steam Momo");
list.add("Chicken Fry Momo");
list.add("Chicken Kothey Momo");
list.add("Chicken C-Momo");
list.add("Paneer Steam Momo");
list.add("Paneer Fry Momo");
list.add("Paneer Kothey Momo");
list.add("Paneer C-Momo");

CustomAdapter Adapter = new CustomAdapter(list,this);
ListView lView = (ListView) findViewById(R.id.momo_list);
lView.setAdapter(Adapter);
}
public class CustomAdapter extends BaseAdapter{

private ArrayList<String> list =new ArrayList<String>();
private Context context;
public CustomAdapter(ArrayList<String> list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int pos) {
return list.get(pos);
}

@Override
public long getItemId(int pos) {
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.group_items, parent,false);
}
TextView listItemText = (TextView)view.findViewById(R.id.text);
listItemText.setText(list.get(position));

final String text = list.get(position);

Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter=0;
counter++;
LinearLayout lv=(LinearLayout)findViewById(R.id.lnear_layout);
lv.addView(createNewTextView(text));
}
public TextView createNewTextView(String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(context);
textView.setLayoutParams(lparams);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setText("Added Item " + text + " Quantity = " + counter);
return textView;
}
});
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
return view;
}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relate_momo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b2b3b7"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.efas.admin.efasrestaurant.Items_momo" >

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >

<ListView
android:id="@+id/momo_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/linearLayout"
android:layout_toRightOf="@+id/linearLayout"
android:orientation="vertical" >

<TextView
android:id="@+id/lnear_momo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>

</RelativeLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />

<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delete_btn"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Add" />

</RelativeLayout>

OutPut

Sample Image

Add TextView, when button is pushed Kotlin

You can try something like this

private LinearLayout mLayout;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
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("New Text"));
}
};
}

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;
}

How do I add new Textviews each time a button is pressed?

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;

}

add this in xml

<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+"
/>

How to add a new Textview evertime Button is clicked

In Your button onClickListener You need to create new textview and then add it to Your LinearLayout. Look at this StackOverflow: response. Linked thread shows similar problem.

Be aware that if You want to have a lot of those TextViews, then You should consider using RecyclerView.

How to add a plain text view with the pressing of a button in android?

Constraint layout needs extra information other than just adding a view to the layout, that where to attach the view in the layout.

EditText newToDo =  new EditText(MainActivity.this);
newToDo.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newToDo.setHint("enter new to do");
constraintLayout.addView(newToDo);

After adding this view to the layout, set constraints for this view. For adding view directly below default Text view, we need the id of that Text View and after that all other subsequent views which we'll generate dynamically.

 int lastEditTextId = 0;
boolean isFirstEditText = true;
ConstraintLayout constraintLayout = findViewById(R.id.constraintLayout);
Button buttonAddNewToDo = findViewById(R.id.addNewToDo);

buttonAddNewToDo.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

EditText newToDo = new EditText(MainActivity.this);
newToDo.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newToDo.setHint("enter new to do");
constraintLayout.addView(newToDo);
newToDo.setId(View.generateViewId());

//Setting constraint set for the new view
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);

constraintSet.connect(
newToDo.getId(),
ConstraintSet.START,
ConstraintSet.PARENT_ID,
ConstraintSet.START
);
constraintSet.connect(
newToDo.getId(),
ConstraintSet.END,
ConstraintSet.PARENT_ID,
ConstraintSet.END
);

if (isFirstEditText) {
constraintSet.connect(
newToDo.getId(),
ConstraintSet.TOP,
YOUR_DEFAULT_TEXT_VIEW.getId(),
ConstraintSet.BOTTOM
);
isFirstEditText = false;
} else {
constraintSet.connect(
newToDo.getId(),
ConstraintSet.TOP,
lastEditTextId,
ConstraintSet.BOTTOM
);
}
lastEditTextId = newToDo.getId();
constraintSet.applyTo(constraintLayout);
});

Attempting to add textview on button click with scrollview

After adding your new TextView, you can call scrollView.fullScroll(ScrollView.FOCUS_DOWN);

In your XML layout code:

<ScrollView
android:id="@+id/scrollview"
<!-- this adds an id for your scrollview -->
<!-- rest of your XML layout below -->

In your activity code:

private ScrollView scrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add the scrollView reference
this.scrollView = (ScrollView) findViewById(R.id.scrollview);
...rest of onCreate

private View.OnClickListener onClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.addView(createNewTextView(editText.getText().toString()),0);
// Scroll the scrollView to the bottom
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
};
}

Source: How to scroll to bottom in a ScrollView on activity startup

Tip: Instead of adding TextViews into a ScrollView, you can use a ListView or a RecyclerView.



Related Topics



Leave a reply



Submit