if(layout == null) {
layout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
ViewGroup parentView = (ViewGroup) getView().getRootView();
parentView.addView(layout, params);
}
FlexNativeAd unit = new FlexNativeAd();
unit.adId = adId;
unit.x = unit.y = 0;
unit.w = unit.h = 4;
unit.tracking = new View(getActivity());
layout.addView(unit.tracking, new RelativeLayout.LayoutParams(unit.w, unit.h));
i want to click dynamically on view layout
like in button we use doClick();
You can use layout.performClick();
Related
I have to dynamically build views and I have used GridLayout as the parent layout to populate 2 views in a row. The code works perfectly fine in the platforms below Nougat. But in nougat version, the views are not populated.
Here is the code section below
private void populateAnswer(List<Answer> answerList, LinearLayout parent) {
GridLayout gridLayout = new GridLayout(context);
gridLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
gridLayout.setAlignmentMode(GridLayout.ALIGN_MARGINS);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
gridLayout.setLayoutParams(layoutParams);
gridLayout.setColumnCount(2);
Answer temp;
RadioGroup radioGroup = new RadioGroup(context);
if (answerList != null) {
for (int i = 0; i < answerList.size(); i++) {
temp = answerList.get(i);
if (temp.getaType().equals(RADIO_BUTTON)) {
RadioButton radioButton = new RadioButton(context);
radioButton.setTextColor(getResources().getColor(R.color.colorPrimary));
radioButton.setHighlightColor(getResources().getColor(R.color.colorPrimary));
GridLayout.LayoutParams rbLayoutParams = new GridLayout.LayoutParams();
rbLayoutParams.setGravity(Gravity.CENTER);
rbLayoutParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
rbLayoutParams.width = GridLayout.LayoutParams.MATCH_PARENT;
radioButton.setLayoutParams(rbLayoutParams);
radioButton.setText(temp.getaDesc());
radioGroup.addView(radioButton);
} else if (temp.getaType().equals(CHECK_BOX)) {
CheckBox checkBox = new CheckBox(context);
checkBox.setTextColor(getResources().getColor(R.color.colorPrimary));
checkBox.setHighlightColor(getResources().getColor(R.color.colorPrimary));
checkBox.setText(temp.getaDesc());
gridLayout.addView(checkBox);
} else if (temp.getaType().equals(FREE_TEXT)) {
EditText editText = new EditText(context);
editText.setHint(temp.getaDesc());
editText.setHintTextColor(getResources().getColor(android.R.color.darker_gray));
gridLayout.addView(editText);
}
}
}
if (radioGroup.getChildCount() > 0) {
gridLayout.addView(radioGroup);
}
parent.addView(gridLayout);
}
I have attached the screenshots for different devices.
Using:
GridLayout.LayoutParams layoutParams = new ViewGroup.LayoutParams();
instead of:
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
I have LinearLayout in that I have RadioGroup in RadioGroup I am adding layout dynamically using addview() method of LinearLayout. I am able to add view but my view not getting full width.
here is my code
radiogroup_ans.setOrientation(LinearLayout.VERTICAL);
radiogroup_ans.removeAllViews();
radiogroup_ans.clearCheck();
for (int i = 0; i < subjectDetailMain.getSubjectdetail().get(0).getPackageDetailArrayList().size(); i++)
{
LinearLayout lnr=new LinearLayout(this);
RadioButton rb_answer = new RadioButton(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rb_answer.setLayoutParams(p);
rb_answer.setId(i);
rb_answer.setTag(i);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.packagelist_layout,(ViewGroup) null);
p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1.0f);
v.setLayoutParams(p);
lnr.addView(rb_answer);
lnr.addView(v);
radiogroup_ans.addView(lnr);
}
here is output hows its looks
image
here is image how i want it
image2
LinearLayout lnr=new LinearLayout(this);
LayoutParams LParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lnr.setLayoutParams(LParams);
Set Layout params to lnr also
Use this code. I hope it will help
radiogroup_ans.setOrientation(LinearLayout.VERTICAL);
radiogroup_ans.removeAllViews();
radiogroup_ans.clearCheck();
for (int i = 0; i < subjectDetailMain.getSubjectdetail().get(0).getPackageDetailArrayList().size(); i++)
{
LinearLayout lnr=new LinearLayout(this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); //add this line
lnr.setLayoutParams(parms); //add this line
RadioButton rb_answer = new RadioButton(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rb_answer.setLayoutParams(p);
rb_answer.setId(i);
rb_answer.setTag(i);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.packagelist_layout,(ViewGroup) null);
p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1.0f);
v.setLayoutParams(p);
lnr.addView(rb_answer);
lnr.addView(v);
radiogroup_ans.addView(lnr);
}
I have a problem with removing views from relative view. The code below programatically adds phone fields and then appends switches below. When adding fields it's all good, but I can't find the way to easily remove them so i.e. there are three fields and I want to delete second or third the ones below are left without view they anchored to.
Code:
private int lastCreatedView;
private int tokensIteration;
private int addButtonId, switch1Id, switch2Id;
private ArrayList<Integer> editTextsIds = new ArrayList<>();
private Contact contact;
private void setPhoneField(String number){
EditText editText = new EditText(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
editText.setId(View.generateViewId());
editText.setText(number);
editText.setHint(R.string.contact_details_activity_hint_phone_number);
editText.setTextColor(getResources().getColor(android.R.color.white));
editText.setEms(10);
editText.setInputType(InputType.TYPE_CLASS_PHONE);
editText.setImeOptions(tokenizer != null && tokenizer.hasMoreTokens()
? EditorInfo.IME_ACTION_NEXT : EditorInfo.IME_ACTION_DONE);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(editTextParams);
editTextsIds.add(editText.getId());
TextInputLayout til = new TextInputLayout(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
til.setId(View.generateViewId());
RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int viewToAttach = lastCreatedView == 0 ? R.id.input_layout_company : lastCreatedView;
textInputLayoutParams.addRule(RelativeLayout.BELOW, viewToAttach);
textInputLayoutParams.addRule(RelativeLayout.END_OF, R.id.contact_photo);
til.setLayoutParams(textInputLayoutParams);
til.setTag(editTextsIds.size());
lastCreatedView = til.getId();
ImageView phoneButton = new ImageView(this);
phoneButton.setImageResource(R.drawable.ic_phone_black_48dp);
phoneButton.setPadding(5,5,5,5);
phoneButton.setScaleType(ImageView.ScaleType.FIT_END);
phoneButton.setColorFilter(Color.argb(255, 255, 255, 255));
RelativeLayout.LayoutParams phoneButtonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
phoneButtonParams.addRule(RelativeLayout.START_OF, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
phoneButton.setLayoutParams(phoneButtonParams);
phoneButton.setTag(editTextsIds.size());
phoneButton.setOnClickListener(view -> {
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
startActivity(call);
});
til.addView(editText);
contactContainer.addView(til);
contactContainer.addView(phoneButton);
ImageView removeNumber = new ImageView(this);
removeNumber.setId(View.generateViewId());
removeNumber.setVisibility(tokensIteration > 0 ? View.VISIBLE : View.GONE);
removeNumber.setImageResource(R.drawable.minus);
RelativeLayout.LayoutParams removeNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
removeNumberParams.addRule(RelativeLayout.END_OF, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
removeNumber.setLayoutParams(removeNumberParams);
removeNumber.setTag(editTextsIds.size());
removeNumber.setOnClickListener(view -> {
for(View v : getViewsByTag(contactContainer, view.getTag()))
contactContainer.removeView(v);
});
if(tokensIteration > 0) contactContainer.removeView(findViewById(addButtonId));
ImageView addNumber = new ImageView(this);
addNumber.setId(View.generateViewId());
addNumber.setImageResource(R.drawable.plus);
addNumber.setPadding(tokensIteration > 0 ? 5 : 0, 0, 0, 0);
RelativeLayout.LayoutParams addNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
addNumberParams.addRule(RelativeLayout.END_OF, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_TOP, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, removeNumber.getId());
addNumber.setLayoutParams(addNumberParams);
addNumber.setTag(editTextsIds.size());
addNumber.setOnClickListener(view -> {
//Toast.makeText(this, "Function not available", Toast.LENGTH_SHORT).show();
setPhoneField("");
setSwitches();
}
);
addButtonId = addNumber.getId();
contactContainer.addView(removeNumber);
contactContainer.addView(addNumber);
setSwitches();
}
void setSwitches(){
RelativeLayout.LayoutParams recordCallsParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
recordCallsParams.addRule(RelativeLayout.BELOW, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_START, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_END, lastCreatedView);
if(switch1Id != 0 && switch2Id != 0){
contactContainer.updateViewLayout(findViewById(switch1Id), recordCallsParams);
return;
}
Switch recordCalls = new Switch(this);
recordCalls.setText(R.string.contact_details_activity_record_calls);
recordCalls.setTextColor(getResources().getColor(R.color.white));
recordCalls.setSwitchPadding(5);
recordCalls.setId(View.generateViewId());
recordCalls.setLayoutParams(recordCallsParams);
recordCalls.setChecked(contact != null && contact.getIsRecordCalls());
switch1Id = recordCalls.getId();
Switch switchPrivate = new Switch(this);
switchPrivate.setText(R.string.contact_details_activity_private_contact);
switchPrivate.setTextColor(getResources().getColor(R.color.white));
switchPrivate.setSwitchPadding(5);
switchPrivate.setId(View.generateViewId());
RelativeLayout.LayoutParams switchPrivateParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switchPrivateParams.addRule(RelativeLayout.BELOW, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_START, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_END, recordCalls.getId());
switchPrivate.setLayoutParams(switchPrivateParams);
switchPrivate.setChecked(contact != null && contact.getIsPrivateContact());
switch2Id = switchPrivate.getId();
contactContainer.addView(recordCalls);
contactContainer.addView(switchPrivate);
}
private static ArrayList<View> getViewsByTag(ViewGroup root, Object tag){
ArrayList<View> views = new ArrayList<>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
Do you really need to completely remove the view? If you just want to hide some view without messing with the layout you can set it to INVISIBLE:
phoneButton.setVisibility(View.INVISIBLE);
BTW: I'd recommend you use layout resource files. It is much easier to work with especially when your layouts get more complex and you get the idea to make your app compatible with multiple screen sizes, screen orientations etc.
I have a Class that used in it below code
this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
gaugeNeedle.setLayoutParams(params);
AbsoluteLayout deprecated so When I want to use RelativeLayout I have not replcement for
AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
Because it accept two args like below
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
What should I do?
Just add this code instead of yours
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
params.topMargin = needleY;
params.leftMargin = needleX;
Do it like this
public class CodeLayout extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
try this code,
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
I have a method that adds a textview dynamically in a linearLayout everytime I click on a button, it works good in this case, but in some other cases i wanted to call it progmmatically without clicking on a button but it doesn't work.BUT when I click on the button again, the textviews appear.
This method is contained in a fragment by the way.
Here is my code :
private void add_part() {
int width = bar.getWidth();
int child_count = ((ViewGroup) bar).getChildCount();
if (child_count == 0) {
minus_btn.setEnabled(true);
LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
part.setGravity(Gravity.CENTER);
bar.addView(part);
} else if (child_count == 1) {
minus_btn.setEnabled(true);
View nextChild = ((ViewGroup) bar).getChildAt(0);
LayoutParams newparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
LayoutParams lparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
} else if (child_count > 1 && child_count < 10) {
minus_btn.setEnabled(true);
for (int i = 0; i < child_count; ++i) {
View nextChild = ((ViewGroup) bar).getChildAt(i);
LayoutParams newparams = new LayoutParams(width
/ (child_count + 1), LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
}
LayoutParams lparams = new LayoutParams(width / (child_count + 1),
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
}
}
and I called it this ways :
add_part();
or
plus_btn.performClick();