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);
Related
private LinearLayout clmnView;
LinearLayout clmnView = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = DimensionsHelper.getMargins(getContext(), DimensionsHelper.DimensionType.BALL);
params.setMargins(0, margin, 0, margin);
clmnView.setLayoutParams(params);
clmnView.setOrientation(LinearLayout.VERTICAL);
clmnView.setGravity(Gravity.CENTER_VERTICAL);
TextView historyTextView = new TextView(getContext());
historyTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
historyTextView.setText("this is history");
clmnView.addView(historyTextView);
No matter what I do ,the historyTextView is not display inside my clmnView.
Could you please help? Thank you in advance.
Okay everything was working as it should. But I was displaying white text in white background.
Changed the color of the text like this :
historyTextView.setTextColor(Color.parseColor("#000000"));
and my text appeared.
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 have RelativeLayout inside ScrollView.. and I am trying to add TextView 'tv' below textView22 ...
item : is an object.
When I run the program, I don't see the new textView though there are NO errors in my code.
Any suggestions?
RelativeLayout s = (RelativeLayout) findViewById(R.id.RL) ;
RelativeLayout.LayoutParams p = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(Post.this);
tv.setText(item.getString("comment"));
p.addRule(RelativeLayout.BELOW , R.id.textView22 );
tv.setTextSize(18) ;
tv.setTextColor( 0xaaaaff );
s.addView(tv , p );
EDIT 2
You can see there is a value of this textView (in the left, look at 'mtext' in the table) but I don't see it in screen!
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.
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 ?