I keep getting a null pointer error, but I can't figure out why. Is there something very obvious I'm missing?
final Dialog d = new Dialog(Start.this);
// dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
d.requestWindowFeature((int) android.view.Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.popuptwo);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
TextView totaltxt = (TextView) findViewById(R.id.totaltxt);
totaltxt.setText("Test text");
If I remove totaltxt.setText("Test text"); then the program doesn't crash. Ideas?
The text view belongs to the dialog..so you should use the dialog's view..
TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
Just do..TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
totaltxt.setText("Test text");
Because it unable to get the view for dialog so,
Replace this line--
TextView totaltxt = (TextView) findViewById(R.id.totaltxt);
by this--
TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
here d is your dialog object.
Related
I'm try to create programmatically textView for a project. My problem is that I don't now how to set up the position of the textview on the screen.
I tried with the code below but instead of moving the textview it moves all the margin. The code is this:
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint);
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(50);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
constraintLayout.getLayoutParams();
params.setMargins(100, 100, 100, 100);
valueTV.setLayoutParams(params);
constraintLayout.addView(valueTV);
the code is inside the oncreate. Thanks for help
You can try something like that:
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(100);
constraintLayout.addView(valueTV);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) valueTV.getLayoutParams();
layoutParams.rightToRight = constraintLayout.getId();
layoutParams.topToTop = constraintLayout.getId();
layoutParams.bottomToBottom = constraintLayout.getId();
layoutParams.leftToLeft = constraintLayout.getId();
valueTV.setLayoutParams(layoutParams);
I'm having a problem with LinearLayout where I want to display a TableLayout after a TextView. I've tried modifying the LayoutParams however I can't seem to make it wrap (which I'm assuming I want it to do).
This is simplified version:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
//This is displayed
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
//This is not displayed - but if I remove the above view it will display...
TableLayout tl = new TableLayout(this);
ll.addView(tl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
By specifiying Orientation as Vertical it worked for me:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
TableLayout tl = new TableLayout(this);
ll.addView(tl);
I am writing an android app,
I have an activity, inside it i have a button, and it's on click listener opens a dialog box from a custom XML using the following code:
I would like to add that dialog box another button that is not set in it's XML file.
All the components are excising in the XML and working just fine, the dialog opens but I can't add the b button.
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LinearLayout layout = findViewById(R.id.dialog_layout_root);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
I tried to use this example but it does not work for me. What am i doing wrong here?
Thanks!
Just try below code
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
You were using only findViewById(R.id.dialog_layout_root); instead of d.findViewById(R.id.dialog_layout_root);
Here ll_button is the id of layout which contains the two xml buttons you have.
LinearLayout ll_button = d.findViewById(R.id.ll_button);
LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll_button.addView(b, layoutParams);
Remove your last line.
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
How to add EditText programmatically?
Here's what I wrote:
View RV = inflater.inflate(R.layout.fragment__dummy,container, false);
LinearLayout LL = new LinearLayout(getActivity());
EditText ET = new EditText(getActivity());
ET.setId(5);
ET.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LL.addView(ET);
return RV;
Thank You!
LinearLayout layout=(LinearLayout)view.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edttext= new EditText(this);
edttext.setId("edittext");
edttext.setLayoutParams(params);
layout.addView(edttext);
You are creating the LinearLayout LL dynamically too.
You need to add it to RV like
((ViewGroup)RV).addView(LL);
Also you should set the LayoutParams for LL too
In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.
The code runs fine but is not placing the new TextView to the right of previous TextView instead the new TextView is positioned at margin (0,0,0,0) i.e. upper right corner of the screen:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);
textView[0] = new TextView(this);//creates first textview
textView[0].setId(0);
textView[0].setText("1");
textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(textView[0]);//creates another textview
textView[1] = new TextView(this);
textView[1].setBackgroundResource(R.drawable.shape);
RelativeLayout.LayoutParams relativeLayoutParams=
new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
textView[1].setText("2");
relativeLayout.addView(textView[1], relativeLayoutParams);
Try the following:
Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer)
Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOP
The following worked for me:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;
TextView[] textView = new TextView[2];
// 1st TextView
textView[0] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(textView[0], relativeLayoutParams);
// 2nd TextView
textView[1] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[1].setText("2");
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
textView[0].getId()); // added top alignment rule
relativeLayout.addView(textView[1], relativeLayoutParams);
Just a guess: can you try to use a different id than 0 ?