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!
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 looking for a solution to solve my problem which all my TextViews overlaps on themselves, when are added to Relative Layout. In fact, I need to do put them after each. other I've read existed answers, I followed them but nothing could solve it yet. can someone tell me where I did wrongly?
here is my code:
for (int i=0;i<parts.length;i++)
{
valueTV[i] = new TextView(this);
valueTV[i].setText(parts[i]);
valueTV[i].setId(i);
valueTV[i].setWidth(300);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
linearLayout_Skills.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
linearLayout_Skills.setBackgroundColor(getResources().getColor(R.color.blue));
if(i>=1)
{
lparams.addRule(RelativeLayout.END_OF, valueTV[i-1].getId());
valueTV[i].setLayoutParams(lparams);
}else {
lparams.addRule(RelativeLayout.ALIGN_PARENT_START);
valueTV[i].setLayoutParams(lparams);
}
linearLayout_Skills.addView(valueTV[i]);
}
XML code:
<RelativeLayout
android:id="#+id/linearSkills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:paddingTop="5dp"
</RelativeLayout>
You can achieve this using Relative layout
So this is the main idea , first you define your relation layout
//layout variable is your relative layout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
Then you define a param variable like this
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
After that you define your textView with an id (in your case this id can be its position in the array)
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
The next textView will be declared like this
TextView tv2 = new TextView(this);
params1.addRule(RelativeLayout.BELOW, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
Finally you set your view using the params you defined
layout.addView(tv2, params1);
Here is a complete example you can check answer by #AndiM
I add 2 layout types (on top of the default one), one of which adds some names to the top of some rows and another layout to add buttons to the screen (and a black line).
However I want the bar at the top with the names to remain constant, and to add the buttons in a loop, that I can scroll through. So far the code for this I have written is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout mainApp = new RelativeLayout(this);
LinearLayout appLayout = new LinearLayout(this);
appLayout.setOrientation(LinearLayout.VERTICAL);
appLayout.setClipBounds(null);
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.button_layout, appLayout, false);
Button StudentSubmission = (Button) view.findViewById(R.id.button1);
StudentSubmission.setText("Button 1");
StudentSubmission.setBackgroundColor(Color.LTGRAY);
Button SoundButton = (Button) view.findViewById(R.id.button2);
SoundButton.setText("Button 2");
SoundButton.setBackgroundColor(Color.LTGRAY);
appLayout.addView(view);
}
scrollPictures.addView(appLayout);
View topmenuview = LayoutInflater.from(this).inflate(R.layout.topofmenu, mainApp, false);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
setContentView(mainApp);
}
However, with this code the bar with the names of columns is placed ON TOP of the list of buttons rather than being ABOVE them, resulting in the top button being cut off. Is there a way to get the buttons to appear UNDER the names of the columns?
q.addRule(RelativeLayout.BELOW, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
should be
q.addRule(RelativeLayout.BELOW, topmenuview.getId());
mainApp.addView(topmenuview, p);
mainApp.addView(scrollPictures, q);
right?
I have a RelativeLayout to which I add Views.
I added a button to it, and the button always appears in front of all the other Views that are added to it, regardless of the order in which things were added. How come?
I'm coding purely in Java, no XML.
Here's a simple example, the button will appear here in front of the text, even though the text was added last:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you're experiencing.
To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don't.
XML:
android:stateListAnimator="#null"
Java:
Button button = new Button(this);
button.setText("Button");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.setStateListAnimator(null);
}
More details:
Android 5.0 android:elevation Works for View, but not Button?
In the Android 5.0 (API 21) and above, you must add android:elevation into the view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
button.setElevation(3.0f); // add this line, you could try with values > 3.0f
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
From android developer docs :
By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
http://developer.android.com/guide/topics/ui/layout/relative.html
Try the following snippet :
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setId(View.generateViewId());
text.setId(View.generateViewId());
button.setText("Button");
text.setText("Text");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, text.getId());
button.setLayoutParams(params);
layout.addView(button);
layout.addView(text);
The button appears to float to the forefront of the RelativeLayout it's in so...
Try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Button");
RelativeLayout groupContainingButton = new RelativeLayout(this);
groupContainingButton.addView(button);
TextView text = new TextView(this);
text.setText("Text");
RelativeLayout activityLayout = new RelativeLayout(this);
activityLayout.addView(groupContainingButton);
activityLayout.addView(text);
setContentView(activityLayout);
}
Check the button's state (enabled/disabled):
loginButton.setEnabled(false);
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 ?