How do I add a LinearLayout inside a RelativeLayout, programmatically? - android

I've a RelativeLayout which consists of an ImageView and TextView. Now, to this RelativeLayout I want to add a LinearLayout, which should be aligned below the TextView. Right now, the LinearLayout is added to the RelativeLayout, but it is not aligned below the TextView. Here is my code :
void addRatingToImage(RelativeLayout relativelLayout, Movies movieObject) {
ImageView ratingImageView;
LinearLayout ratingLayout = new LinearLayout(mContext);
ratingLayout.setOrientation(LinearLayout.HORIZONTAL);
double roundedRating = roundUpRating(Double.parseDouble(movieObject.mRating));
for(int i = 0; i < 5; i++) { //TODO: Check
ratingImageView = new ImageView(mContext);
if(i < roundedRating) {
ratingImageView.setBackgroundResource(R.drawable.star_2);
ratingLayout.addView(ratingImageView);
}
else {
ratingImageView.setBackgroundResource(R.drawable.star_1);
ratingLayout.addView(ratingImageView);
}
}
RelativeLayout.LayoutParams ratingLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView movieNameTextView = (TextView)relativelLayout.getChildAt(2);
ratingLayoutParams.addRule(RelativeLayout.BELOW, movieNameTextView.getId());
ratingLayout.setLayoutParams(ratingLayoutParams);
relativelLayout.addView(ratingLayout);
}
I have one doubt here. Does RelativeLayout.BELOW work, when it is applied to a different kind of layout, nested in a RelativeLayout? Thanks.

Yes, RelativeLayout.BELOW will work. It is recognized by parent RelativeLayout no matter what the child view class is.
According to your problem, I suppose that RelativeLayout is behaving that way because you've set fill_parent for your TextView movieNameTextView's layout width.

Related

Add Textview one under the other in loop

I'm trying to add textviews one just bellow another but when I run the code, it all gets stacked together. Here is the code:
RelativeLayout constraintLayout;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
private void createTable() {
RelativeLayout textRelativeLayout = new RelativeLayout(this);
relativeLayout.addView(textRelativeLayout);
for (int i = 1; i <= 5; i++) {
TextView textView = new TextView(this);
textView.setText("TextView " + String.valueOf(i));
setTextViewAttributes(textView);
textView.setId(i);
params.addRule(RelativeLayout.ALIGN_BOTTOM, textView.getId());
textRelativeLayout.addView(textView);
}
}
Since first I'm trying to make it work, I'm only setting dummy text for now. Is it my code that is wrong? Or did I miss a Param?
You didn't use param.
It should be textRelativeLayout.addView(textView , param);
Or use textView.setLayoutParams(params)
And I think the rule should be (I'm not tested this ):
params.addRule(RelativeLayout.BELOW, previousTextView.getId());
Also it is better to use a LinearLayout with vertical orientation to add view under other view.
You should use LinearLayout instead of RelativeLayout.
Make sure you set the orientation to 'vertical' for the LinearLayout as well.
No need to add rules to params like you're doing for the LinearLayout. They will get order from top to bottom automatically.

LinearLayout's child view change line according to device's width issue

Hi friends I am using LinearLayout with orientation horizontal to fill TextView's inside it programmatically.
Code:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout1);
for (int i = 0; i < 20; i++) {
TextView textView = new TextView(this);
textView.setText("Hello " + i);
linearLayout.addView(textView, i);
}
now, i am getting very weird look on the screen:
and I want this type of view:
views inside the LinearLayout should change their line according to device width. Anyone has any idea to achieve this, please help.
add text views to grid layout with setNumColumns().

Adding Buttons dynamically in RelativeLayout to LinearLayout

When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);

Trying to create nested multiple linear layout programmatically

I am trying to create nested linearlayouts dynamically and setting it to activity layout as
setContentView(createLayout()); in oncreate().
But I am getting nothing on the screen but a blank screen. Can someone help in pointing out if I am doing it in wrong way?
private LinearLayout createLayout() {
Log.d(TAG,"calling cretaelayout");
LinearLayout main = new LinearLayout(getApplicationContext());
main.setOrientation(LinearLayout.VERTICAL);
int k =0;
for(int i=0 ;i < MainActivity.height*10;i++) {
LinearLayout row = new LinearLayout(getApplicationContext());
row.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j< MainActivity.width*10;j++)
{
Log.d(TAG,"creating layout element");
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setBackgroundColor(Color.BLACK);
ll.setId( k++);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
ll.setOnClickListener(myhandler);
row.addView(ll);
}
main.addView(row);
}
return main;
}
There's nothing anywhere that takes space. You have a LL inside an LL inside an LL, with the last LL set to wrap_content. But it has no content inside it, so its size will be 0 in both directions. Elements with no size don't appear. Try making the inner most LLs fixed size, and you should see something.

Dynamic RelativeLayout in Android: Scroll not Working

I want to put a scroll in vertical orientation in a Relative Layout that i created programmatically. But my scroll do not work. Can anyone help me?
Here is the code that i'm using:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.screen1);
for (i = 1; i < 20; i++) {
RelativeLayout.LayoutParams p = new
RelativeLayout.LayoutParams(
150,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
layout.setScrollContainer(true);
ScrollView vscroll = new ScrollView(this);
vscroll.setFillViewport(true);
layout.setVerticalScrollBarEnabled(true);
layout.addView(vscroll);
p.addRule(RelativeLayout.BELOW, i-1);
p.addRule(RelativeLayout.CENTER_HORIZONTAL);
Button buttonView = new Button(this);
buttonView.setId(i);
buttonView.setText(i);
buttonView.setLayoutParams(p);
buttonView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Dialog(((Button)arg0).getId());
} });
layout.addView(buttonView, p);
}
I think you need to be adding your buttonViews to the ScrollView instead of the layout. ScrollView is a container View (like RelativeLayout). I think what your code is doing is adding a 0 height ScrollView to the top of your RelativeLayout, then a button after that. Since the button is not in the ScrollView, your 20 buttons won't scroll.
Try as below it may help
vscroll.addView(button);

Categories

Resources