Reputation: 30601
I have a situation wherein I need to add layouts at runtime as follows:
1. A Button
layout is already defined in XML:
2. When the user clicks the Button
, an EditText
and another Button
are dynamically added to the existing layout:
It works correctly on Gingerbread (Android 2.3.5):
And doesn't work on Kitkat (Android 4.4.2):
Does someone know a workaround for this problem ? Here's the code I've used:
public class MainActivity extends Activity {
private boolean comment = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.commentbutton);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(comment == true){
comment = false;
LinearLayout l = (LinearLayout)findViewById(R.id.comment_layout);
l.setOrientation(LinearLayout.VERTICAL);
final EditText e = new EditText(v.getContext());
e.setId(R.id.commentbox);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(250,100);
lp.setMargins(0, 20, 0, 0);
e.setLayoutParams(lp);
e.setSingleLine(true);
e.setImeOptions(EditorInfo.IME_ACTION_DONE);
l.addView(e);
final Button b = new Button(v.getContext());
b.setId(R.id.submitbutton);
lp = new LinearLayout.LayoutParams(150,33);
lp.setMargins(0, 20, 0, 0);
b.setLayoutParams(lp);
b.setText("Submit");
b.setTextColor(Color.BLACK);
b.setTypeface(null, Typeface.BOLD);
b.setBackgroundColor(Color.parseColor("#CCCCCC"));
b.setFocusable(true);
b.setFocusableInTouchMode(true);
l.addView(b);
e.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
b.performClick();
return true;
}
return false;
}
});
LinearLayout ll = new LinearLayout(v.getContext());
ll.setId(R.id.productbottomlayout);
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,25);
ll.setLayoutParams(lp);
ll.setFocusable(true);
ll.setFocusableInTouchMode(true);
l.addView(ll);
ll.requestFocus();
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
LinearLayout l = (LinearLayout) findViewById(R.id.comment_layout);
EditText e = (EditText) findViewById(R.id.commentbox);
l.removeView(e);
Button b = (Button) findViewById(R.id.submitbutton);
l.removeView(b);
LinearLayout ll = (LinearLayout) findViewById(R.id.productbottomlayout);
l.removeView(ll);
MainActivity.this.comment = true;
}
});
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here's the XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/comment_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="25dp"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="@+id/commentbutton"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="Add Comment"
android:textColor="#000000"
android:textStyle="bold"
android:background="#CCCCCC"
android:onClick="showCommentBox"/>
</LinearLayout>
</RelativeLayout>
Also, when we add layouts programmatically like this, we need to set an ID manually for each layout element.
I've done that. Here they are:
<resources>
<item type = "id" name = "commentbox"></item>
<item type = "id" name = "submitbutton"></item>
<item type="id" name="productbottomlayout"></item>
</resources>
Upvotes: 1
Views: 1147
Reputation: 24848
// Try this way,hope this will help you to solve your problem...
Define this method in your class
public int convertSizeToDeviceDependent(int value) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
return ((dm.densityDpi * value) / 160);
}
Try to replace this line:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(250,100);
With this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(convertSizeToDeviceDependent(250),convertSizeToDeviceDependent(100));
Try to replace this line:
lp = new LinearLayout.LayoutParams(150,33);
With this:
lp = new LinearLayout.LayoutParams(convertSizeToDeviceDependent(150),convertSizeToDeviceDependent(33));
Try to replace this line:
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,25);
With this:
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,convertSizeToDeviceDependent(25));
Upvotes: 0
Reputation: 3438
Add this method and use it to convert all pixels to dpi:
public static int dpi(int i) {
Resources r = getResources();
int value = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, i,
r.getDisplayMetrics());
return value;
}
source: https://stackoverflow.com/a/6327095/1977021
Upvotes: 0
Reputation: 1299
The problem is that you are setting the dimension in a pixel measurement unit. Use DP, or LayoutParams.WRAP_CONTENT to make your app scalable and responsive.
So, that's how I'll set my layoutParams:
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
Upvotes: 1