0

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

6 Answers 6

5

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); 
Sign up to request clarification or add additional context in comments.

Comments

1
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) 

Comments

1

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

Comments

1

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.

Comments

0

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.

Comments

0
on startup textView.setVisibility(View.GONE); on button click textview.setVisibility(View.VISIBLE); edittext.setVisibility(View.GONE); button.setVisibility(View.GONE); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.