I have an EditText, and when a button is clicked, I want the text to be displayed on the screen. And the EditText can be used again to add other text and on and on. Thanks!
Something like:
EditText et = (EditText)findviewById(R.id.et1);
TextView display = (TextView)findviewById(R.id.tv1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
tv1.setText(tv.getText().toString()+"\n"+et.getText().toString());
//prints the text from edittext and concatinates it to the textview previous text
}
});
Please use below code for that, it will solve your problem.
EditText editText1 = (EditText)findviewById(R.id.editText1);
TextView textView1 = (TextView)findviewById(R.id.textView1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
textView1.append("\n"+editText1.getText().toString());
// OR
textView1.setText(textView1.getText().toString()+"\n"+editText1.getText().toString());
}
});
You can add Views to your Layout by using its addView() Method. So you could add a TextView upon each button-click which contains the content of your TextField.
Related
I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method
Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="#+id/text_thingsToDo"
android:layout_width="#dimen/ThingsToDo_EditText_Width"
android:layout_height="#dimen/ThingsToDo_EditText_Height"
android:layout_margin="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_EditText_Color"/>
<Button
android:id="#+id/save_thingsToDo"
android:layout_toRightOf="#+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="#dimen/Standardize_Margin"
android:layout_marginTop="#dimen/Standardize_Margin"
android:layout_marginBottom="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_Save_Button"
android:textColor="#color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!
I have an EditText box that appears and the hint in it is actually a question that needs to be answered. Once the user enters the value and hits next, how can I make a different EditText box take the originals place. I want to do this so there is a new hint(question) and I can manipulate that input differently than with the first input.
In my example, my first EditText is enter_one and I want it to change to enter_two after the next_button is clicked.
Thank you for your help!
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
}
});
Create two edit text in XML layout, and align both in same position, one over another. Then declare the android:visibility="gone" for edittext enter_two.
Then onClick of next should be:
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
final EditText getInput_two = (EditText)findViewById(R.id.enter_key_two);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newValue = getInput.getText().toString().trim();
int input = Integer.parseInt(newValue);
System.out.println("Your value is " +input);
getInput_two.setVisibility(View.VISIBLE);
getInput.setVisibility(View.GONE);
}
});
You can just change the hint and clear the EditText :
final EditText getInput = (EditText)findViewById(R.id.enter_key_one);
Button clickButton = (Button) findViewById(R.id.next_button);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getInput.setHint(newQuestion);
getInput.setText("");
}
});
Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....