I have created multiple button in a layout dynamically.Now,how can i remove this layout after used.
for example:-
LinearLayout parent = new LinearLayout(this);
parent.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 10; i++) {
Button b = new Button(this);
b.setText("Primary");
Drawable image = ContextCompat.getDrawable(
getApplicationContext(),
R.drawable.your_image);
image.setBounds(0, 0, 60, 60);
b.setCompoundDrawables(null, null, image, null);
parent.addView(b);
}
If you still have the instance of the linear layout, just call removeView(...) as you did for the addView(...).
you can set visibility of linearLayout to gone by parent.setVisibility(View.GONE);
or remove all the views from the linearLayout by parent.removeAllViews().
You'll have to use removeView(View) or removeViewAt(position) from the parent, depending on if you keep track of the indexes or the objects.
parent.removeView(button);
parent.removeViewAt(buttonIndex);
get the parent of the view and remove that view from its parent
((ViewGroup) parent.getParent()).removeView(parent);
Try this:
View button = view.findViewById(R.id.buttonid);
((ViewGroup) button.getParent()).removeView(button);
I had removed ParentView from its child (Textview's) click.
tvClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (llContainer.getChildCount() > 1) {
((LinearLayout) view.getParent()).removeView(view);
}
}
});
Related
I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class
When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}
I am trying to initialize all the navigation bar buttons and assign Black background color, when user clicks on one of the button, that one should change color and all other should remain Black.
But i get the following error:
Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object
reference
public void bottomNavIcons(View view){
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.bottom_nav_bar);
ImageButton imageButton = new ImageButton(getActivity());
imageButton.setClickable(true);
imageButton.setOnClickListener(onClickListener);
imageButton.setImageResource(Constants.bottom_nav_icon[i]);
imageButton.setBackgroundColor(Color.BLACK);
imageButton.setPadding(70, 0, 70, 0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageButton, layoutParams);
}
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
ImageView imageButton = (ImageView) linearLayout.getChildAt(i);
imageButton.setBackgroundColor(Color.BLACK);
}
v.setBackgroundColor(Color.BLUE);
}
};
I think this is the problem:
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
You are assuming the LinearLayout is inside the view which is clicked - my best guess is this is not true (you probably want to get the clicked view's parent, not child).
You have 2 options:
Start iterating the view hierarchy to the top (i.e. call v.getParent()) until you reach the LinearLayout.
Call findViewById on the containing activity (or fragment's root view) instead of the v object.
I want to create a function that takes in an array of text and creates buttons and adds them to the view.
This is my code.
It is working and creating the buttons but when i call the function twice it doesn't create two linear layouts it just shows the last one called as if it is deleting the first one.
How can i make it to create a new linear layout and add it to the View?
// Create a view
protected boolean CreateTheButtons(String[] names)
{
try
{
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// set LinearLayout as a root element of the screen
linLayout.setWeightSum(names.length);
setContentView(linLayout, linLayoutParam);
LayoutParams lpView = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
lpView.weight = 1;
for (int i = 0; i < names.length; i++) {
Button btn = new Button(this);
btn.setText(names[i]);
linLayout.addView(btn, lpView);
}
return true;
}
catch(Exception ex)
{
return false;
}
}
It is working and creating the buttons but when i call the function
twice it doesn't create two linear layouts it just shows the last one
called as if it is deleting the first one.
Your code removes the first LinearLayout resulted from calling the method because you use setContentView()(which will replace the current view of the activity(if any is found) with the view that you pass as a parameter). Instead you should remove the call to setContentView() and insert a holder ViewGroup for the LinearLayouts that you plan to add through that method.
<!-- This will be the content view of the activity -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/parent" />
Set the layout above as the content view for the activity, in the onCreate() method:
setContentView(R.layout.the_layout_above);
In the method you'll then have:
protected boolean CreateTheButtons(String[] names) {
try {
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linLayout.setWeightSum(names.length);
// assuming this method is in an Activity
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
parent.addView(linLayout, linLayoutParam);
LayoutParams lpView = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
lpView.weight = 1;
for (int i = 0; i < names.length; i++) {
Button btn = new Button(this);
btn.setText(names[i]);
linLayout.addView(btn, lpView);
}
return true;
} catch(Exception ex) {
return false;
}
}
I had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..