Add Buttons Into layouts one by one on serval Lines - android

I want to develop the Messaging application like native Application. The Problem is when i add buttons after adding two or three buttons depending on name size will become like this
My Code is as below
RelativeLayout tr = (RelativeLayout) findViewById(R.id.contacts_div);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(MainActivity.this);
b.setText(name);
b.setId(id);
tr.addView(b,params);
if(prev_id!=0)
{
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
}
I want if the enough space is not available the it will add on Next Line.
i try many posts in stack overflow but fail to solve this problem
Thanks in advance

You should use a gridview for this.

try setting your button position through yout code once the text is set then you can see if the total width of your buttons is too big. Then you'll know when to place a button on the next line.
And as #Snicolas said use a GridView, the placement will be easier.
I hope this will help you.

You should use FlowLayout. It's not standart android component, so you should find implementation you like. E.g. this one is nice.

Related

Android LinearLayout fill with buttons

I want to fill the LinearLayout with buttons.
The way I want it, is by filling the top of the screen first until the end of the line then if there is no space left, go to the next line and so on, like in the below picture.
I tried this code but it fill the screen vertically
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.FILL);
Button bt1 = new Button(this);
bt1.setText("A Button");
bt1.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
layout.addView(bt1);
Any help or suggestions would be appreciated.
Or you're looking for FlowLayout for Android. Some implementations are here.
https://github.com/ApmeM/android-flowlayout
https://github.com/blazsolar/FlowLayout
What you are looking for is a FlexboxLayout.
Are you looking for one of this?
MultiTextTagView
TokenAutoComplete
Android Chips
AndroidTagView
Chips Android

How to make a zig-zag layout?

I want to create zig-zag layout same as following attached image:
I tried a lot by creating diagonal lines and arranging them with icon but couldn't make it same.
I implemented diagonal lines with the help of accepted answer from following questions:
Diagonal line across view
How rotate line in Android XML?
However I'm stuck to arrange lines with icons exactly same as in image.
I created this custom ZigZagLayout.java file to cater your requirement. You just have to update the package name in the 1st line.
It basically extends RelativeLayout, so you can use it in your layout-xmls just like any other ViewGroup class. Once you have instantiated this layout, just add child-views to it like it is done for RelativeLayout via addView(View child).
Example code snippet with dynamically created view:
ZigZagLayout zigZagLayout = (ZigZagLayout) findViewById(R.id.layout_zigzag);
Button btn = new Button(this);
btn.setText("Test Button");
btn.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
zigZagLayout.addView(btn);
I've also added few interfaces to this ZigZagLayout for your easy interaction like ability to set the connector-line stroke width, visibility, color, margins, etc.
Try it out and let me know if it suffices your requirement. Cheers.
If you have layout for each circular item , you may use relative layout to align them, using align_below, align_left with margin, align_right with margin tags.
Please provide further detail, what are the lines connecting them and exactly what all are requirements for UI and functionality.

Optimize Performance for creating views at runtime

I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.

RelativeLayout alignment/positioning not working

I've had a look around, and not been able to find a fix to this problem. Yes it is something that has been asked before, but none of the solutions I've found has corrected my problem.
So basically, my problem is that I need two views to align to the right of their container, one next to the other. I can't stop them from overlapping however.
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int) (screenWidth*0.30), LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newSpinner.setLayoutParams(param);
newSpinner.setId(-101);
param = new RelativeLayout.LayoutParams((int) (screenWidth*0.10), LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
param.addRule(RelativeLayout.LEFT_OF, newSpinner.getId());
newButton.setLayoutParams(param);
The above code results in this:
http://i.imgur.com/o1GlZLV.png
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
While removing the above line results in this: http://i.imgur.com/25vlmrw.png
So what's the fix?
You are using the same object 'param' for both spinner and button.
Try using like param1 and param2 for each in the first place.
Secondly try using
param.addRule(RelativeLayout.ALIGN_PARENT_LEFT); for one item and
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); for the other to avoid overlapping.
Also try this solution. Create a relative layout with param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); and then add spinner and button in this relative layout, use ALIGN_PARENT_RIGHT for one item and LEFT_OF for the other. Also make sure to use the appropriate width of the items so that they should not overlap
Remove:
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
From newButton.

Building a simple Group Chat View in Android

I'm building a very simple online chat room App. What I have achieved is something like below right now:
Robert:blah..
Tom: yes blah..
David(You): That sounds cool!
Lily: Do you know blah...
Robert:blah..
Robert:blah..
David(You): Wow! Blah...
The new feature/problem I'm facing is that I want current user's talks to show on the right.
Like below(this is what David see on his screen):
Robert:blah..
Tom: yes blah..
That sounds cool! : David(You)
Lily: Do you know blah...
Robert:blah..
Robert:blah..
Wow! Blah... : David(You)
Each line above is a TextView, and I'm dynamically creating TextView whenever there's new message, then adding them to the LinearLayout that contains these TextView. In order to make David(current user)'s talk on the right, I tried to set align right and I change the LinearLayout to RelativeLayout. But then I realize once I use RelativeLayout, all talks are in the same line overlapping each other since I didn't set their height.. Can anyone shed some light on how to achieve this please? My codes below:
...
//new messages are stored in lines[]
for (int i = 0; i < lines.length; i++) {
TextView newLine = new TextView(getBaseContext());
newLine.setText(lines[i]);
// check if the speaker of this line is user himself
if (speakerName.equals(userName)) {
//change the layout of this textView to make it align right..
}
myLinearView.addView(newLine);//add to linearView that contains these talks
}
You definitely need to use a ListView, as Chor WaiChun mentioned. However, the way you would do this with a relative layout would be to set the RelativeLayout.layoutParams for the view with a rule that sets it to layout_below the previous view. Just like you would in XML.
Also, even if you insist on adding 1 view per line (which as previously stated is wrong, use a ListView), there's no reason not to use the LinearLayout. You can have a LinearLayout with right justified text, just set the gravity to right.

Categories

Resources