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));
Related
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);
This question already has answers here:
Android Linear Layout Weight Programmatically
(4 answers)
Closed 3 years ago.
I have a vertical LinearLayout LL_p with two children horizontal LinearLayouts LL_1 and LL_2 which in turn have their own children. Based on the visible contents of LL_1 and LL_2, I want to dynamically change their relative weight inside LL_p. I already have an xml layout with a great deal of details that I do not wish to lose, so I only have to make the incremental change to the weights. How do I do that? Here is my xml
…
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=“0.2”
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_1”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:background="#drawable/some_image”
android:orientation="vertical" >
<!—- a number of includes —>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_2”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:layout_marginBottom="#dimen/dim_1”
android:background="#color/some_color”
android:orientation="horizontal" >
<!—- a number of children —>
</LinearLayout>
</LinearLayout>
</LinearLayout>
So in java activity class I figure I need the following method, but I need help completing it. Notice that I am not instantiating new layout params as that would cause me to lose my xml layout details; instead I am using getLayoutParams to grab the one set from xml. so how do I make weight change to the layout as obtained?
private void adjustMYLayout(boolean flip) {
LayoutParams layout1 = mLL1.getLayoutParams();
LayoutParams layout2 = mBLL2.getLayoutParams();
//now what?
if(flip) {//set one weight system
}else {
//set other weight system
}
}
UPDATE for #nKn
private void adjustMYLayout(boolean flip) {
if (flip) {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
} else {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
}
}
You can achieve that by declaring a LinearLayout.LayoutParams object
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter (1f) is the weight of the layout (in this case 1 (f stands for float)).
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 need to create one full screen android activity programatically as shown in the image below:
The two buttons should remain at the bottom of the screen.
Dummy content will consist of different components (textviews, radio buttons, checkboxes...) and will be populated dynamically.
This is the code I have so far:
//Main Layout
FrameLayout lLayout = new FrameLayout(this);
lLayout.setBackgroundColor(Color.parseColor("#0099cc"));
lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//Navigation layout
LinearLayout l = new LinearLayout(this, null, R.style.ButtonBar);
LayoutParams bottomLayout = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
l.setLayoutParams(bottomLayout);
l.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
l.setBackgroundColor(Color.parseColor("#66000000"));
l.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams buttLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//previous section button
previousButton = new Button(this);
previousButton.setLayoutParams(buttLayout);
previousButton.setText("Previous section");
previousButton.setOnClickListener(this);
l.addView(previousButton);
//next section button
Button nextButton = new Button(this);
nextButton.setLayoutParams(buttLayout);
nextButton.setText("Next section");
nextButton.setOnClickListener(this);
l.addView(nextButton);
//add components
TextView tView = new TextView(this);
tView.setText("Dummy text");
tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
lLayout.addView(l);
setContentView(lLayout);
Here is what the code produces:
Tthere are several points that do not work as intended:
1. Buttons are at the top and not the bottom.
2. Buttons do not spread out nicely
3. TextView I added as a test is shown behind the buttons. I will have many different widgets on the screen and expect them to be larger than one screen. I would like to have a scroll option but with all those widgets not to be seen behind the two buttons that are supposed to be at the bottom of the screen.
The following xml is exactly what you would need:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/dynamiclayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/navigationlayout"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="#+id/navigationlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Previous Section" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next Section" />
</RelativeLayout>
</RelativeLayout>
Now programatically inflate dynamiclayout and add all your dynamic views into it.
Your root view is a FrameLayout, which is intended for only one child View. It is also frequently used to create overlapping Views, as all of a FrameLayout's children will be drawn in the same place on screen.
Replace your FrameLayout with a RelativeLayout. Make sure you update your LayoutParams references to use RelativeLayout.LayoutParams. You will also need to set the navigation LinearLayout to align with the parent View's bottom like so:
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM, true);
Though I really would suggest using XML. It will make your life far simpler.
I have following UI hierarchy
Scroll View -->TableLayout-->Rows [each row has TextView+EditText]
Problem
Inside a row, each View should not exceed 50% of width of parent (TableRow). if an EditText is created to enter, say Username with max characters N; then width of EditText ideally should be exactly N chars long but must not exceed 50% limit.
I've tried many permutations and combination and could not get it right so far.
I have to do it programmatically as per the project requirement.
thanks.
Try using layout weight like this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textview1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
<EditText
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</TableRow>
To do it programmatically, try using LayoutParams to accomplish the same like this:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
TableLayout.LayoutParams trparams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(trparams);
textview1 = new TextView(this);
edittext1 = new EditText(this);
TableRow.LayoutParams fieldparams = new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tr.addView(textview1, fieldparams);
tr.addView(edittext1, fieldparams);
table.addView(tr);
i saw your comment to ask's answers.i think you can set the width of edittext or textview as wrap content.and set the max width property of those controls as 50% of the screen.then it's width will not exceed the 50% of the parent width.
I had to scrap TableLayout in order to be my own layouts effective. Layout params in TableRows seems ineffective. LinearLayout looks more flexible.
Now I have following Layout hierarchy and it works.
Scroll View -->LinearLayout instead of TableLayout--> [Individual fields encapuslated in LinearLayout instead of TableRows]