I'm creating some 50 button dynamically.
Text is getting set as followed:
btn.Text=result.Rows[i]["Col1"].ToString()+"\n"+result.Rows[i]["Col2"].ToString()
+"\n"+result.Rows[i]["Col3"].ToString();
where result is DataTable & btn is object for button.
Now the problem is some of the buttons are not getting displayed appropriately.
Referring to screenshot below,
in img1 - An unnecessary blank line is getting displayed after the first row.
in img2 - Text is not center aligned.
in img3 - TATAMOTORS is not getting displayed in single line even though there is a space on either side of t he button.
Please note that I'm not setting padding which can be the reason for this.
Any idea how to solve this?
Also, how alignment of text of a button can be set programmatically?
I know that this is not the Best of the question, but after spending hours on it, I'm unable to crack it.
Any help appreciated...
You can set the gravity of the button to customize how the text is aligned. This is exposed on the button by using the Gravity property. From the docs:
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.
The values you can assign are found in the GravityFlags enum. For example:
button.Gravity = GravityFlags.Center;
Alignment of button content can be set with .setGravity(int)
In the original question, I wonder if the source text contained some extra space characters? That would explain img1 and img2.
Anyway, my need to left-align the text in a button led me to this page, and here's the solution I ended up with, based on Alix Bloom's answer:
Button myButton = new Button(getActivity());
myButton(Gravity.LEFT);
Button myButton = new Button();
myButton.setGravity(Gravity.RIGHT); //LEFT
Hope this help you. Worked for my (i was also strugling with this issue for some time)
Related
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
I've seen many of questions for how to add TextView in a RelativeLayout programatically, but everybody adding it in LinearLayout with orientation vertical. Can any one suggest me or give me a link that how to add multiple TextView in RelativeLayout at right until it has space and then change the line.
I want a layout like this..
May be this works for you !
You can customize chips-edittext-library for your need. Customizing by setting background to transparent, and editable to false.
-> Or you can use any other library which is used for displaying emoticon in EditText and customize it according to your need. Like android-emoticon-edittext-spike
You can use the Flow Layout library that manages the view arrangement itself.
When there is no space left the added View is moved to the next line
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.
I am trying to fit two EditTexts horizontally. What I am trying to accomplish is make the text keep scrolling right without the EditText extending in anyway when there is no more space in that specific EditText. This happens when the EditText is set to match_parent (width), but when I set an actually width or wrap_content, it breaks to the next line. Not the behavior I am hoping for. Any ideas?
If I understood your question correctly, you're saying you want the edit text to be a single line each? If so, have you tried using the xml attribute
android:singleLine
Or you can accomplish the same thing with:
setSingleLine() //called on the edit text
I just tested it out with 2 edit texts in a horizontal linear layout set to 100dp width each and I think it accomplishes the behavior you want.
You can try this:
Set both to fill_parent and layout_weight=1. This will align them side by side.
I need to create an animation in Android. I have 3 TextViews placed in an LinearLayout. All I want to do is the text field must come on to the screen as though it has been pulled out from the left end of the screen. Something similar to a marquee, however the text field must come from left end of screen one character at a time to occupy the left end of screen.
you must textView set textview property in your xml layout android:ellipsize="marquee" and you oncreate() method to set textview like youtextview.setSelected(true); now your textview running with marquee effect perfectly work for me!
another way you can add dynamically textview in coding you can follow this code:
yourtextview.setEllipsize(TruncateAt.MARQUEE);
yourtextview.setSelected(true);