rtmatchbox
rtmatchbox

Reputation: 29

set textview programmatically in Android

I wanted to set textview programmatically, the below code doesn't seem to work

TextView textView = new TextView(this);  
textView.setText("game");  
setContentView(textView);

Upvotes: 0

Views: 72

Answers (4)

zohreh
zohreh

Reputation: 1045

create LayoutParams as following

LayoutParams  param=new LayoutParams(w, h);
           textView.setLayoutParams(param);

Upvotes: 0

venu
venu

Reputation: 2979

try this

TextView textView = new TextView(this);  
textView.setText("game");  

textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
setContentView(textView);

Upvotes: 0

Looking Forward
Looking Forward

Reputation: 3585

Change

TextView textView = new TextView(this);

to

TextView textView = new TextView(Youractivity.this);

Upvotes: 1

johntheripp3r
johntheripp3r

Reputation: 989

remove setContentView(....); anywhere before TextView textView = new TextView(this);

Upvotes: 0

Related Questions