Basic question regarding setting the text of a programatically created button. As seen in my code below I've done the basics in terms of creating the button but my button appears as seen in my attached image. Basically the text in the button doesn't appear as expected. Any ideas why?
Note: I've declared button as a public instance variable right above my onCreate() and has been added correctly to my relative layout using addView();
// Create User button
btnUserAdmin = new Button(this);
// Customise the UserAdmin button
btnUserAdmin.setBackgroundColor(Color.BLUE);
btnUserAdmin.setTextSize(13.7f);
btnUserAdmin.setTextColor(Color.parseColor("#FFCC00"));
btnUserAdmin.setText("USER ADMINISTRATION");
btnUserAdmin.setGravity(Gravity.LEFT);
Thanks.
You should specify the dimensions of the button, otherwise the size could be unexpected. For instance
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT );
btnUserAdmin.setLayoutParams(lp);
also, you can directly set them when you add the buttom
yourRelativeLatout.addView(btnUserAdmin, lp);
Also remember that numeric values for the dimensions (of the bottom or the layout) usually are evil. As you can, use only WRAP_CONTENT and MATCH_PARENT
Related
I am creating an android layout in which I will place 3 buttons vertically with some space between them. But based on the requirements, the button count can come down to 2, and I need the buttons to repositioned based on the available screen space.See the screens below.
now the second case with two buttons should be like
How should I go about doing this ?
You can just set visibility of buttons depending upon your condition just like the following:
button.setVisibility(View.GONE);
When you set visibility to gone then the space will considered available for other layouts and your will be set as per your desire.
When you set visibility to invisible then the space will considered as allocated.
You can try below code for achieving the same based on your condition for adding buttons to your layout
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
You can define all three buttons in the layout XML and then :
if you want to show the third button
thirdButton.setVisibility(View.VISIBLE);
if you want to hide
thirdButton.setVisibility(View.GONE);
at least if using a Linear or Relative Layout it should repositione by itself.
I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.
There are plenty of similar questions asked in SO, but then also, please take time to read my question.
I need to create a UI programmatically that would have multiple ImageView, all positioned at different location of the screen and having their associated click events (its a game app). I found that FrameLayout is appropriate choice, where I can set margins on my own and have it positioned at desired location.
Now, I'm confused whether to have FrameLayout for every single ImageView I create, or to keep single FrameLayout and add all ImageViews within it, but set each imageview at different position.
In either of the case, how can I add FrameLayout, and ImageView within it, programmatically and also set its margin such that it can be placed anywhere on the screen.
Note that my main canvas, which will carry all these ImageViews has background, and the canvas is a LinearLayout set via XML, so my onCreate() already has setContentView(R.layout.game_canvas);, and I'd be using addContentView() to add additional views, but this method too accepts LayoutParams object as it second parameter, so what exactly should I set for this, when I add my FrameLayouts using this method?
My question might be confusing itself, so please let me know if I need to elaborate.
FrameLayouts are designed to only hold one View, so this isn't really the appropriate choice.
Use a RelativeLayout to hold all of your ImageViews. You can position each ImageView by setting the margins in their LayoutParams.
E.g. The following code would place an ImageView at coordinates 50,50:
RelativeLayout imgLayout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.an_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(50, 50, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imgLayout.addView(iv, lp);
You can then add this RelativeLayout to your main LinearLayout using its addView() method.
I have a relativeLayout like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/parent" >
<ListView
android:layout_width="360dp"
android:layout_height="600dp"
android:id="#+id/list"
android:inputType="text"
android:maxLines="1"
android:layout_margin="50dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
In the java code, I want to add a view to the left of the listview, but it didn't worked:
m_relativeLayout = (RelativeLayout)findViewById(R.id.parent);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, m_listView.getId());
Button button2 = new Button(this);
button2.setText("I am button 2");
m_relativeLayout.addView(button2, layoutParams);
only if I set the listview to alignParentRight, it will work. Is this an android bug or I'm missing something?
I always try addView(View child, int index, LayoutParams params), but it might only work in the linearlayout. So is there an normal solution to make the RelativeLayout.LEFT_OF work?
EDIT
I have tried RelativeLayout.BELOW and RelativeLayout.RIGHT_OF, and they worked perfectly, so it means I don't have enough place to get the button? I tried to give more space, but it still not work.
I use Toshiba AT100 (1280*800) and landscape, so the space is enough. Test below and right just same as the left. I think If i put an control A in the relativelayout, then I add control B and decalare it's on the left of the control A, the result should be the control B will push the control A to its right, right?
I think If i put an control A in the relativelayout, then i add control B and declare it's on the left of the control A, the result should be the control B will push the control A to its right, right?
Your assumption is incorrect, the control A will not be pushed to the right unless you specified this with a RelativeLayout.LayoutParams rule. RelativeLayout places its children one one top of each other starting at the top-left corner of the screen if you don't specify placement rules for them. When you add the View A to the RelativeLayout without any rules(like layout_alignParentRight) it will be placed starting from the top-left corner of the screen. Then, when you add the View B, the rule to_leftOf will apply to this View position but this rule doesn't mean anything for the View A who will maintain its position on the screen. This will make View B to be place to the left of View A but outside of the screen as View A bounds start from the left border of the screen.
The Button will be placed to the left of the ListView when you use layout_alignParentRight="true" because there is now space to actually see the Button(it's not outside anymore). addView(View child, int index, LayoutParams params) works in a LinearLayout because the LinearLayout arranges its children in a row or column(depending on orientation) so when you add a View at a specific position, it will push the other Views after it to the right or below(depending on orientation)(there is no relative positioning of the views in a LinearLayout, the only rule is that the children come one after the other).
Starting with the ListView without any rules set on it, here is an example on how to make the Button to appear on the left of the ListView:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button2 = new Button(this);
button2.setText("I am button 2");
button2.setId(1000);
m_relativeLayout.addView(button2, layoutParams);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView
.getLayoutParams();
rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
The Button will be added as normal to the screen and it will appear starting from the top-left corner of the screen. Without the two lines from the code above the Button and ListView will overlap as this is the normal behavior of RelativeLayout for children without any rules on them. We then explicitly modify the position of the ListView to move it to the right(with the last two line from the code above).
If your variable names are indicative, it's because you are adding the widget to a LinearLayout, so tags for a RelativeLayout get ignored.
This line is the one I'm talking about:
m_linearLayout.addView(button2, layoutParams);
EDIT
You say alignParentRight works... the only difference there is that ot doesn't take an anchor parameter. Perhaps m_listView.getId() isn't returning the proper id. You could step through with the debugger and see if it's returning a proper value.
Maybe you could try calling the id specifically...
layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.list);
To perform it, use predefined view ID or declare one. In values folder create ids.xml then add a Item like this:
<item name="imageViewID" type="id"/>
use this id in your code where you are creating new Instance of view like this:
RelativeLayout layout=new RelativeLayout(context);
ImageView imageView = new ImageView(context);
imageView.setId(R.id.imageViewID);
RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(imageView, layoutParams);
TextView textView = new TextView(context);
RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
layout.addView(nameView, nameLayoutParams);
or we can directly use this function View.generateViewId() to perform the same. Like this:
imageView.setId(View.generateViewId());
I think you might have forgotten to add m_listView to the RelativeLayout or m_listView's visibility would be GONE.
Can you please check for that?
setId before align is called, especially for the new object view.
If you are using a custom id and not a regular generated Android id (eg. R.id.my_id), make sure that the id is not equal to 0 (or negative), otherwise the rule will be ignored.
I am new to android and stuck in a very basic problem.I am working on an application in which I need to swipe images on fling.On every image I have to add buttons dynamically.I am adding buttons using AddContentView() to add buttons.Everything is working fine but I want to set the position of buttons dynamically.I have read at many places,everyone is using addView() to add buttons and setting their positions.I have tried this
but it isn't working.Can anyone please tell me how to set the margins(position) of button using addContentView().Any help would highly be appreciated.
Setting a buttons margin using addView works for me. Be sure to pass the right LayoutParams object to the ViewGroup that should hold your button.
FrameLayout fl = new FrameLayout(context);
Button b = new Button(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
fl.addView(b,params);
should work.