I have a problem with removeAllViews and setContentView..... both are not working for me i don't know why.
I have a function in a class that return a view and i want to add that view to the screen and that view will be change after every 5 second.....
so after 5 second i call removeAllViews on the layout in which the view is added and then add it again.... but surprisingly the old view is still there and it add the new view at the end....
Same problem with the setContentView i try this also but it also work same as main layout it also add the view at the end not replace the whole screen.......
please help...... thanks in advance............
setContentView(logic.getView());
and
LinearLayout layout = blah blah blah;
layout.removeAllViews();
layout.addView(logic.getView(), layout parameters....);
Ok. With some of the review of your comments i got why this issue is created.
May be You are not removing the first View properly. Thats why while you are going to add another view, there are two views(1st is previous one and second is newly added view) instead of the one new View.
So remove the first view properly and then add the view as you want.
Hope it will help you.
Thanks.
Enjoy. :)
1. Using Visiblity...
Look at the setVisibility method to change the visibility options for
the view. Here
developer.android
- **` 2.Runtime Add And Delete View`**
LinearLayout layout;
TextView textview1 ,textview;
Button btn ;
layout =new LinearLayout(this);
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(params);
btn = new Button(this);
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
btn.setLayoutParams(param);
btn.setText("Change");
textview =new TextView(this);
textview.setLayoutParams(param);
textview.setText("Text 1...");
textview1 =new TextView(this);
textview1.setLayoutParams(param);
textview1.setText("Text 2 ...");
layout.addView(textview);
layout.addView(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Remove All View ", "REmove");
layout.removeAllViews();
layout.addView(textview1);
layout.addView(btn);
Log.i("Remove All View ", "REmove");
}
});
setContentView(layout);
Related
I want to add some ui elements to my android app, a Button for example!
But I can't find a complete tutorial! I found this code after a lot of searches:
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
Button btn = new Button(this);
btn.setText("Manual Add");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);
My first problem is first line! Can you explain it for me please? What is R.id.layout? I know R is an object for resources but I don't know what is layout!
Second problem is line 3, What is LayoutParams?
Thank you all!
You can create views using default constructors for example
Button button = new Button(context);
After that you should determine to which type of parent view you are going to attach it, and create corresponding layot params. Every parent view LayoutParams type has uniqe customize methods, for example rules of RelativeLayout.LayoutParams.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height)
//customize params here
button.setLayoutParams(params)
Attach view to your parent view
frameLayout.addView(button)
That is it.
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);// you are getting a refrence to your layout
Button btn = new Button(this); // creating a new object of button type
btn.setText("Manual Add"); //setting the button text
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)); //setting the width and height of the element
ll.addView(btn);//adding the button to your layout
R.id.layout is the name of your activity layout
It is the parent view which you are going to add your views. In your example it is a LinearLayout called layout.
The app I am trying to make, has got a lot of similar LinearLayouts and textViews that need to be created programmatically and placed on the screen in a specific order.
So I decided to define a method which returns one element, and for the furthur uses,I will put the method in some loop to produce the others. but when I create a view or layout this way, nothing shows up or sometimes the app crashes, as if it's been sent null to addView(). It only works when I create the View/Layout in onCreate() and then I use it right there afterwards.So , any ideas that I can use the method to creat my Layout/View? Because they are too many and it's not possible to create them one by one in onCreate()
Here's the method:
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
tv_day.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
tv_day.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tv_day.setGravity(Gravity.END);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
and this is onCreate() which doesn't add any linear layouts with a textView in it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_view);
LinearLayout ll= createLinearLayout();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_month_view);
mainLayout.addView(ll);
}
I think this should help
- add an empty linear layout in XML with some id.
- reference that layout in code
- add elements to that layout dynamically
Hey was just checking your code. Its working perfectly now just try this method.
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv_day.setLayoutParams(layoutParams);
tv_day.setGravity(Gravity.CENTER);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
I have an issue with ViewGroup.addView() method. I use this code to add new view to my layout:
TalbeLayout parent = (TableLayout)findViewById(R.id.this_does_not_matter);
parent.removeAllViews(); //this view group contains something at start
TextView tv = new TextView(this);
tv.setText("some text");
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
parent.addView(tv);
And after that I cant see my TextView. More than that, parent.getChildCount() returns correct values (1 if I try to add one child). In onClick() method of parent view I try to get width and height of TextView and all of this equals to 0. Calling requestLayout(), invalidate() and measure(500, 50) for TableView and parent has no effect. I even try to add new view with the help of view.post(Runnable), although this code is executed in UI thread.
I confused. I really don't understand what happens. Can someone explain me what I doing wrong?
One interesting moment:
setLayoutParams() has no effect. If I set params with width=500 and height=50, in onClick method I get params with width=-1 and height=-1.
Code after TableRow added:
TableLayout parent = (TalleLayout)findViewById(R.id.this_does_not_matter);
parent.removeAllViews(); //this view group contains something at start
TextView tv = new TextView(this);
tv.setText("some text");
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(500, 50);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(500, 50);
TableRow tr = new TableRow(this);
tr.addView(tv, tlp);
parent.addView(tr, lp);
parent.invalidate();
parent.requestLayout();
I found one more interesting thing. This Activity runs from TabHost. And "addView bug" appears only if the Activity is first selected in this TabHost. If at the first time I start Activity from another tab, all works fine.
I found solution. If switch off layout animation with android:animateLayoutChanges="false" all works fine. But I still have no idea why this happens. If anyone know reason of this behavior, it interesting for me.
I am trying to dynamically create a linear layout with a dynamic number of buttons based on certain parameters. So far I have some code that compiles but when it runs it does not display anything.
public void displayMenu()
{
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
//Test Button
Button btn1= new Button(this);
btn1.setText("Dylan");
lin.addView(btn1);
for (int i =0 ; i < 5; i++){
btnList.add(new Button(this));
btnList.get(i).setText("Hello:"+i);
lin.addView(btnList.get(i));
Log.i("CategoryMenu","Adding btn to view");
}
}
What am i doing incorrectly? I'm sure that i'm missing some thing silly here.
you have to add lin to the current hierarchy of view, or creating a new one calling setContentView(lin);
It seems that you're not adding that linear layout to a viewgroup parent.
After all your code you should add something like
myParentViewGroup.add(lin);
Before lin.addView(btn1);
You should define layoutparams for Button like:
// Defining the layout parameters of the Button
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Setting the parameters on the Button
tv.setLayoutParams(lp);
You have to add lin too the layout.
At present you have just created a Linearlayout, but you have not attached it to any layout.
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
I'm trying to create a View dynamically (on click). Any person have any idea why this does not work. It just opens a blank (BLACK) screen. When i click back it moves to previous screen as well. And i need to know the way i'm trying to set the team is correct.
public class Details extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setTheme(android.R.style.Theme_Dialog);
TextView label = new TextView(this);
label.setText("Hello This text");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
label.setTextSize(20);
label.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(this);
tr.addView(label);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tl.setGravity(Gravity.CENTER);
tl.addView(tr);
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sv.addView(tl);
setContentView(sv);
}
}
There is an msg in LogCat as following
couldn't save which view has focus because the focused view ##### has no id
Well one obvious thing is that you are using generic LayoutParams, not the correct parameters for the layout manager. For example you are not using a TableLayout.LayoutParams for the TextView so you are not actually telling it how to layout the text view within the table structure.
I also suggest using hierarchyviewer to look at what is going on with your view hierarchy.
Is the activity declared in the Manifest.xml?
You must to put
#Override
I had the similar problem