Dynamic RelativeLayout in Android: Scroll not Working - android

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);

Related

HorizontalScrollView - Adding Item at specific position

I want that the user can scroll throw my HorizontalScrollView and if he press my Button, a TextView will be shown on the current position of my HorizontalScrollView.
So far I already know how to present a TextView, but not on a specific position...
TextView textView;
textView = new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
linearLayout.addView(textView);
Any help is welcomed!
Do some thing like this ,
final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroller);
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView iv = new TextView(Act2.this);
iv.setText(new Date().toString());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 400);
params.leftMargin = scrollView.getScrollX();
params.topMargin = scrollView.getScrollY();
container.addView(iv, params);
}
});
And the relative layout should be inside the HorizontalScrollView. This will add a textview exactly in the current position , but you should also write code for avoiding the overlapping the TextViews.

how to implement vertical scroll on pop-up window in android?

I've tried with a piece of code but it is not displaying vertical scroll bar. my code is pasted below:
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
LinearLayout lt=new LinearLayout(this);
view=new ScrollView(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setBackgroundColor(Color.WHITE);
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
lt.addView(popupText);
layoutOfPopup.addView(insidePopupButton,350,35);
layoutOfPopup.setBackgroundColor(Color.BLACK);
view.addView(lt);
layoutOfPopup.addView(view);
Thank you in advance..:)
ScrollView cannot be combine with popupWindow in android, no matter what. Sad but true.
You need to add your views in this way:
LinearLayout linearLayout = new LinearLayout(this);
// add all your views to linearLayout(or RelativeLayout)
linearLayout.addView(popupText);
linearLayout.addView(insidePopupButton);
// add linearLayout to ScrollView instance
view.addView(linearLayout);
// add ScrollView instance to main layout
layoutOfPopup.addView(view);
ScrollView is a single element container.

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);

Button Fill Entire Layout Programmatically

I have a question for Android developers. I have a layout with a button (developed programmatically, not with xml) and I want the button to fill the entire layout right now but it currently doesn't and I'm not sure why, I thought I had everything set up correctly with the gravity of the button and the layout params but here's what I have. If you can point me in the right direction I would really appreciate it! Thanks.
LinearLayout bottom = new LinearLayout(this);
bottom.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.FILL;
bottom.setLayoutParams(params);
bottom.setBackgroundColor(Color.BLUE);
Button eqbttn = new Button(this);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
You've only applied the MATCH_PARENT size to the LinearLayout. You need to apply it to the button, too.
Button eqbttn = new Button(this);
LinearLayout.LayoutParams eqparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
eqbttn.setLayoutParams(eqbttn);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
This will force the button to fill the LAYOUT both vertically and horizontally. If you need the layout itself to take up the whole screen, change its WRAP_CONTENT to MATCH_PARENT as well.
Also, in this case, you do not require the Gravity.FILL layout parameter on the LinearLayout.

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

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.

Categories

Resources