Reputation: 179
I want to set the button width during run time and I want it to be equal to 50% of the display width. I used the following but I have a problem "nothing appears in emulator" and says "Process stopped unexpectedly. Please try again" .java public class experiment extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Button btn = (Button) findViewById(R.id.btnmarwa);
btn.setWidth(width/2);
setContentView(R.layout.emailpopup);
}
}
When I commented this line
btn.setWidth(width);
There is no errors and the layout appears, So where to set the new width to the button in .java file??
Upvotes: 1
Views: 763
Reputation: 1641
Is not directly about your question, but you have to put this lines After setContentView() 'cause the findViewById method.
Like try this :
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.emailpopup);
Button btn = (Button) findViewById(R.id.btnmarwa);
btn.setWidth(width/2);
Upvotes: 3
Reputation: 4258
You should state what the exception in the log is, so that we can work out why it's crashing.
My guess is that btn is null, because findViewById is returning null, and so btn.setWidth() is causing a NullPointerException.
This is probably because you are setting setContentView after findViewById, so the view hasn't been inflated yet.
Change the order around, making sure setContentView has been called before you call findViewById.
Upvotes: 1