First 5 buttons are added programatically and last one via XML. Both ways I use same parameters. Why are dynamically added buttons text's cut off?
Programatic:
Button b = new Button(getActivity());
b.setText(text);
b.setAllCaps(false);
b.setBackgroundResource(R.drawable.button_tag_rect);
b.setTextColor(getResources().getColor(R.color.colorWhiteText));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
dpToPx(28));
int marginInPx = dpToPx(2);
params.setMargins(marginInPx, marginInPx, marginInPx, marginInPx);
tagCloudTwitter.addView(b, tagCloudTwitter.getChildCount()-1, params);
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_margin="2dp"
android:textAllCaps="false"
android:background="#drawable/button_tag_rect"
android:textColor="#color/colorWhiteText"
android:text="test"/>
EDIT: solved! See my answer bellow.
Funnily enough, what helped was setting a "dummy" padding:
b.setPadding(1,1,1,1);
¯\_(ツ)_/¯
Try setting the button height and width;
b.setWidth(10);
b.setHeight(100);
Related
I had seen a couple of examples for setting the relative position through program (from java) for views in android. But in my particular case i have 2 buttons (which are not views) say "button_tag" and "button_rate" which are made via xml and are arranged such as "button_tag" above "button_rate" by default. If at any point of time is there a mechanism by which i can make "button_tag" below "button_rate" dynamically.
<Button
android:id="#+id/button_tag"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#drawable/img_whitebackground"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:text="#string/string_tag"
android:onClick="click_addscreen" >
</Button>
<Button
android:id="#+id/button_rate"
android:layout_marginTop="1dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#string/string_rate"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:onClick="click_addscreen"
android:background="#drawable/img_whitebackground" >
</Button>
The xml part of the two buttons are as shown
You can use LayoutParams object and add rules to that object as per your requirement. and then setlayoutparam to your button.
For example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dp
params.addRule(RelativeLayout.LEFT_OF, R.id.btnAdd);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.setMargins(0, 0, 20, 60);
btnMyLocation.setLayoutParams(params);
You can add rules to set position dynamically.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.BELOW, R.id.button_rate);
buttonTag.setLayoutParams(p);
I'm trying to programmatically add many TextView to a RelativeLayout but I am unable to do that when TextView reach the end of the display right next TextView inflate in a new line.
RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/tag_cloud"
android:padding="10dp">
</RelativeLayout>
Code:
if (categoriesCursor.moveToFirst()){
do {
TextView tagElement = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
tagElement.setText(categoriesCursor.getString(2));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, pixels, pixels); // llp.setMargins(left, top, right, bottom);
tagElement.setLayoutParams(llp);
tagCloudLayout.addView(tagElement);
} while (categoriesCursor.moveToNext());
}
tag.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#ff000000"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
Thanks
It´s not really clear what You are asking, but If I understand You the right way, You want to have only one line with textViews, or? Also, You are using wrong Layout Params, if You want to add some views to a relativeLayout side by side, I think You will get it with:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
Where the last parameter stands for the layout weight attribute. With LayoutWeight and MATCH_PARENT, all views will be drawn with equal size.
Consider using a ListView which contains TextView, and that way you'll be able to take advantage of cell re-use etc. Here's a good tutorial
Finally I put one LinearLayout vertical oriented and inside LinearLayouts horizontal oriented with three TextView inside. Isn't the best solution but it works for me.
I am trying to dynamically create buttons, and they will be of varying size and in varying positions.
I have the code to create a button of varying size, but I am stuck on changing the position.
I am using linearlayout and am trying to use setMargins to move the button around, but it seems to be changing the margin within the button. My code is as follows:
public void button(int a, int b) {
newButton = new Button(this);
newButton.setText("HELLO");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.width = a;
params.height = b;
layout.requestLayout();
layout.addView(newButton, layoutParams);
}
Here is my manifest for this bit:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="58dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="50sp"
android:layout_height="40sp"
android:orientation="vertical" >
</LinearLayout>
Do you understand what a LinearLayout is and why you're using it? Every child of the layout snaps in place. If I have a LinearLayout that's vertical and it has 3 children they will be on top of each other. I can change their gravity so they are attracted to different margins but to "change" its position is impossible depending on what you mean by "change".
Check out the other layouts. You may want to use a RelativeLayout.
Why don´t you use button.setX(float x) and button.setY(float y)? You'll need to use RelativeLayout instead of the LinearLayout. It's more easy but it's only available since api 11...
I have an XML layout containing a EditText and 2 buttons. If I click on the plus button, a new edittext is programatically added. This works, but the edittext looks different. According to the XML the edittext defined in XML does not have any special attributes, so I believe its not a particular layout setting.
My question is how do I make my programmatically added EditText's look the same?
The EditText's containing the numbers are my programmatically added edittext's. The empty ones are creating in the XML.
(source: tozz.nl)
Code:
LinearLayout baseLayout = (LinearLayout) findViewById(R.id.baseLayout);
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setId(100 + numPlayers);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
EditText editText = new EditText(getApplicationContext());
editText.setText(editText.toString().substring(25, 30));
ImageButton delButton = new ImageButton(getApplicationContext());
delButton.setImageResource(R.drawable.ic_delete);
linearLayout.addView(editText);
linearLayout.addView(delButton);
baseLayout.addView(linearLayout);
My XML is as following:
<LinearLayout
android:id="#+id/linearPlayer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editPlayer1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical" />
<ImageButton
android:id="#+id/addPlayer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_input_add" />
</LinearLayout>
Luksprog answered my question:
pass the Activity Context and not the Application Context when creating the new views.
Adding those views with the correct LayoutParams should make the EditText be like the initial one from the layout:
linearLayout.addView(editText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
linearLayout.addView(delButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
baseLayout.addView(linearLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
I'm trying to make a View in a ListView (through a ListAdapter) with full width.
Here is the current test code :
LinearLayout result = new LinearLayout(context);
TextView testView = new TextView(context);
testView.setText("Aaah");
TextView test2View = new TextView(context);
test2View.setText("Eeeeh");
result.addView(testView, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
result.addView(test2View, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
// To make it easier to see borders
result.setBackgroundColor(Color.BLUE);
(It has to be put in the ListAdapter's createView)
Unfortunately, the two TextViews are small and both stuck on the left.
The limiter is actually the result view itself, not taking full width, so the two TextViews simply share this little space.
Doesn anyone know how to make them take full width ? Thanks ! :)
EDIT : simplified (and almost identical) problem :
Code :
http://pastebin.com/6pn1hXnT
I want the TextView to be full-sized, and I center the text so I can see when it is full-width and when it is only wrapping content.
This code shows the text on the left, so the MATCH_PARENT is not doing anything...
What should I do ?
Thanks !
I faced the similar problem and I've followed the advice on this thread. The linked topic refers to a ListView inside a dialog, but I've experienced the same behaviour in a pop-up list. In order to make the TextView full-width, I had to wrap it inside a RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="14dip"
android:paddingRight="15dip"
>
<TextView
android:id="#+id/list_dialog_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
</RelativeLayout>
Why don't you add them with fill_parent, but with '0'?
result.addView(testView, new LinearLayout.LayoutParams(FILL_PARENT ,
LayoutParams.WRAP_CONTENT, 1));