Upto Buckys tutorial 14...but my code isnt working plz go through and tell me

+1 Appi M · February 5, 2016
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.graphics.Color;
import android.widget.EditText;
import android.util.TypedValue;
import android.content.res.Resources;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout AppisLayout = new RelativeLayout(this);
AppisLayout.setBackgroundColor(Color.LTGRAY);

Button MyButton = new Button(this);
MyButton.setId(1);
MyButton.setText("Click here");
MyButton.setBackgroundColor(Color.GREEN);

EditText username = new EditText(this);
username.setId(2);

RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);



usernameDetails.addRule(RelativeLayout.ABOVE,username.getId());
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
usernameDetails.setMargins(0, 0, 0, 100);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);

Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,r.getDisplayMetrics());
username.setWidth(px);


AppisLayout.addView(username,usernameDetails);
AppisLayout.addView(MyButton,buttonDetails);
setContentView(AppisLayout);
// setContentView(R.layout.activity_main);
}



Working upto changing colour of the background but when I added EditText, it isnt showing. What did I do wrong?

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Whitney · February 5, 2016
Here s your problem

usernameDetails.addRule(RelativeLayout.ABOVE,username.getId());


kind of cyclical
  • 1

Java / Android Development

107,090 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator