I'm adding some views to a LinearLayout with a slight overlap in the top, here is the code:
viewHolder.linearLayout.removeAllViews();
for (int i = 0; i < conversation.getPreviousMessages().length; i++) {
View messageView = layoutInflater.inflate(R.layout.layout_previous_message_row, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, -5, 0, 0);
viewHolder.linearLayout.addView(messageView, layoutParams);
}
In this code some views are added to layout in order A, B, C, with A to the back and C to the front.
I would like to reverse the order, making A to the front and C to the back.
I have tried this, but nothing happens.
How can I reach this?
Please, could you people bring me some code?
Thanks in advance.
There is the capability to change the z-order of a view if that is the question that you are asking. And if so desired, you can usually get the effect of using the View.bringToFront() capabilities within that class.
View.bringToFront
After some punches against the wall, I finally found a solution:
1.- I changed the layout from being a LinearLayout to being a RelativeLayout because bringChildToFront has a weird behavior among LinearLayout.
2.- After adding the view to the RelativeLayout, I iterated backwards over the views and I made a bringChildToFront.
Here is the code:
for (int i = 0; i < conversation.getPreviousMessages().length; i++) {
View messageView = layoutInflater.inflate(R.layout.layout_previous_message_row, null);
messageView.setId(99+i);
viewHolder.relativeLayout.addView(messageView);
if (i > 0) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, -5, 0, 0);
layoutParams.addRule(RelativeLayout.BELOW, 99+i-1);
messageView.setLayoutParams(layoutParams);
}
}
for (int i = viewHolder.relativeLayout.getChildCount() - 1; i >= 0; i--) {
viewHolder.relativeLayout.bringChildToFront(viewHolder.relativeLayout.getChildAt(i));
}
I hope it help someone.
Related
Here is my code, I am trying to add few buttons in a row. Its working fine, But these buttons appear too close to each other, I want to apply horizontal margins to buttons. But not able to add. To achieve this I tried to keep button inside a Linearlayout and applied margins to it. But it somehow don't show any buttons, And when I comment out this line- ll.setLayoutParams(lp); Buttons can be seen again, But without any margin. Please let me know how can I make buttons at some distance from each other.
maintable = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
for (int z = 0; z < 3; z++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
LinearLayout ll = new LinearLayout(this);
//ll.setLayoutParams(lp);
Button b = new Button(savedLists.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
params.setMargins(0, 20, 0, 20);
b.setBackgroundResource(R.drawable.circular);
b.setText(Integer.toString(c));
b.setPadding(10,10,10,10);
ll.addView(b,params);
tr.addView(ll);
c++;
}
maintable.addView(tr);
R.drawable.circular is just creating a simple circular button. Please let me know, if I should post that too.
You haven't set any margin for the button here. You are setting it's padding! Here is how you can set the margin for a button!
Button b = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
yourLinearLayout.addView(b,params);
Can anyone tell me why this is happening, please?
What I'm trying to do is get them to all align centered, regardless of how many there are. It works lovely for quite a few, and even works with only 1 more, but 4 seems to do this. I'm adding them in code, here:
LinearLayout guessHolders = (LinearLayout)findViewById(R.id.guessHolders);
guessHolders.removeAllViews();
currentLetterPosition = 0;
final Bitmap emptyLetterHolder = BitmapFactory.decodeResource(getResources(), R.drawable.letter_holder);
for(int i=0; i<Globals.mUser.getLevel().getSolution().length(); i++)
{
final EmptyLetter tmp = new EmptyLetter(this, i);
tmp.setImageBitmap(emptyLetterHolder);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(Globals.defaultLetterSizes[mBlockSize],
Globals.defaultLetterSizes[mBlockSize], 1);
parms.weight = 1;
parms.gravity = Gravity.CENTER_HORIZONTAL;
tmp.setLayoutParams(parms);
tmp.setAdjustViewBounds(false);
tmp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("PP2", tmp.getId() + " << the clicked empty letter holder id");
currentLetterPosition = tmp.getId();
tmp.setImageBitmap(emptyLetterHolder);
setChosenLetter(tmp.getId(), '\u0000');
}
});
currentEmpties.add(tmp);
guessHolders.addView(tmp);
}
I've tried setting gravity, weightsum, width and height of the box views as well as the parent container LinearLayout, but nothing seems to shift them.
Any help is greatly appreciated!
Fixed it. With the following:
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(Globals.defaultLetterSizes[mBlockSize],
Globals.defaultLetterSizes[mBlockSize], 0);
parms.gravity = Gravity.CENTER_HORIZONTAL;
tmp.setLayoutParams(parms);
So, I set the weighting to zero and leave the rest to the OS. Works for all of them now. Marvelous.
Cheers
I'm a newbie to Android development, and I'm very much still learning Java too so be gentle!
I am creating an app that can take information about a task (I'm basing it around a sort of homework planner), store that info and then display it in a list. The program must be able to dynamically generate the list from the background files. I have managed all of this so far, but when I create a basic output for each task, containing the "subject" and "details" variables using a LinearLayout they appear on the screen overlapping. They all seem to be creating correctly, but they are all being put in the same place. Are there attributes I can set to make them display in a vertical list???
Here is the piece of code where I generate the viewgroups and display them. This is called from a loop in another part of the program which finds the number of files in internal storage.
TextView subjView;
TextView detailView;
RelativeLayout displayLayout;
LinearLayout taskDisplay = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
subjView = new TextView(this);
detailView = new TextView(this);
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
subjView.setText(subject);
detailView.setText(details);
layoutParams.setMargins(0, 0, 0, 0);
taskDisplay.addView(subjView, layoutParams);
layoutParams.setMargins(10, 0, 0, 0);
taskDisplay.addView(detailView, layoutParams);
displayLayout.addView(taskDisplay);
If I understand correctly, I think your issue is only that you are declaring and then changing the layoutParams margins which sets them both to the same, which is overlapping your TextViews.
Edit
Okay, I am still not 100% sure how you are doing all of this so my example may need to be tweaked. I tried to throw this together quickly so forgive me for any minor mistakes.
New mock up for dynamic layouts:
TextView subjView, detailView;
RelativeLayout displayLayout, rl;
// I am assuming this is your main layout
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
// Just using a for loop as an example of a loop event, not sure how you are accomplishing this
for(int i = 0; i < data.length(); i++) {
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 100);
if (i > 0) {
int rePositionRule = i;
rllp.addRule(RelativeLayout.BELOW, rePositionRule);
}
RelativeLayout taskDisplay = new RelativeLayout(this);
taskDisplay.setLayoutParams(rllp);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams2.setMargins(10, 0, 0, 0);
subjView = new TextView(this);
detailView = new TextView(this);
subjView.setText(subject);
subjView.setLayoutParams(layoutParams);
detailView.setText(details);
detailView.setLayoutParams(layoutParams2);
taskDisplay.addView(subjView);
taskDisplay.addView(detailView);
displayLayout.addView(taskDisplay);
}
Your displayLayout is a relativeLayout.. A relative layout, as the name implies, places element relative to each other. Normally you'd say "element A should go below element B" etc. Since you aren't providing any of these rules for the items you are creating they are just going to all go to the default position in a relative layout (which is the top of the screen.. hence the overlap)
If you don't want to deal with the hassle of changing your code to place things relatively simply switch your displayLayout to a LinearLayout in your xml and code and set its orientation to vertical. You'll probably want to wrap that in a scroll view if it runs off the screen
However, it sounds like what you really want is a ListView...
I need to separate tab buttons with space, I tried to set margin to views and then add them as tabs, but it does not work, I also thought of adding empty view as divider, but haven't tried it yet, is there any standard way of doing this, or any tweak that can achieve same effect?
Thanks!
Here's the way:
TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
final int tabChildrenCount = tabWidget.getChildCount();
View currentView;
for (int i = 0; i < tabChildrenCount; i++) {
currentView = tabWidget.getChildAt(i);
LinearLayout.LayoutParams currentLayout =
(LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(0, 5, 5, 0);
}
tabWidget.requestLayout();
This is really a good solution even for my problem! Many thanks for that! I used it to implement space before the first and after the last item in the widget to have the possibility to scroll them visible to the center without adding additional (and disturbing, because the widget does not excpect such silly things) invisible buttons.
//pump up space for first entry on the left and last entry on the right!
Display display = getWindowManager().getDefaultDisplay();
//Point size = new Point();
int width = display.getWidth();
View currentView = mTabHost.getTabWidget().getChildAt(0);
LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(currentLayout.leftMargin + width/2, currentLayout.topMargin, currentLayout.rightMargin, currentLayout.bottomMargin);
currentView = mTabHost.getTabWidget().getChildAt(mTabHost.getTabWidget().getChildCount()-1);
currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(currentLayout.leftMargin, currentLayout.topMargin, currentLayout.rightMargin + width/2, currentLayout.bottomMargin);
mTabHost.getTabWidget().requestLayout();
I am creating some buttons and add them to a linear layout which is defined as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mylayout">
</LinearLayout>
The buttons are created with
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(1, 1, 1, 1);
btn.setText("Button");
btn.setPadding(0, 0, 0, 0);
mylayout.addView(pv, lp);
}
These buttons always have a margin (about 3px) which I'd like to remove. Is there anything I am missing? If I use a custom view which I created there is no space between them.
Should I set
lp.setMargins(-3, -3, -3, -3);
which removes the margin? Is there a drawback with this?
I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:
http://developer.android.com/guide/developing/tools/draw9patch.html
which includes fiction margins. Here you can find more info about 9-Patch.
http://developer.android.com/guide/topics/resources/drawable-resource.html
In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).
Why are you using Layout Params .. simply add the view after its creation...
This will surely remove the problem
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
btn.setText("Button");
mylayout.addView(pv);
}