OpenSrcFTW
OpenSrcFTW

Reputation: 211

Programatically set View width on button click

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

Upvotes: 0

Views: 192

Answers (6)

Pankaj Arora
Pankaj Arora

Reputation: 10274

on startup
textView.setVisibility(View.GONE);

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

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

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

Upvotes: 5

Sanket Shah
Sanket Shah

Reputation: 4382

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.

Upvotes: 1

Ritaban
Ritaban

Reputation: 773

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.

Upvotes: 0

Naroh
Naroh

Reputation: 625

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

Upvotes: 1

KOTIOS
KOTIOS

Reputation: 11194

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)

Upvotes: 1

Related Questions