Harsh Lakhani
Harsh Lakhani

Reputation: 125

programmatically create buttons without respective layout

I am new to android. I need to develop a basic demo which creates a button dynamically without creating respective layout. I have two activities included. In my first activity i have button with relative layout, when i click that button i should be able to enter into the second activity without creating respective layout in second activity and by creating a dynamically button there i should be able to return back to my first activity. can a button be created likewise ?

LinearLayout layout = new LinearLayout(this); layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
button.setText("Previous");
layout.addView(button);

And when i write the same code in eclipse i get line marked on the FILL_PARENT ? why is it so ?

Upvotes: 1

Views: 408

Answers (2)

Mohit Verma
Mohit Verma

Reputation: 3025

After your code ends, just add:

setContentView(layout);

Upvotes: 1

sl4mmer
sl4mmer

Reputation: 371

FILL_PARENT is depricated, use MATCH_PARENT

Upvotes: 0

Related Questions