I am quite new to Android, and is having problem in creating a Button dynamically. I have it working by creating in the Xml layout and working fine. Hope someone can help, to write code to create the button below during runtime, and after that Add it into a TableRow.
Note: I wanted to create a Button which is equivalent to the Xml below, but NOT using findViewById(), because this button doesn't exist. I know how to new a Button, but I am having difficulties on setting all the attributes below. Especially the layout_weight, background and drawableTop.
<Button
android:id="#+id/BtnRating1"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/Rating_1"
android:drawableTop="#drawable/face_1"
style="?android:attr/borderlessButtonStyle"
android:background="?android:selectableItemBackground"
android:gravity="center"
android:onClick="OnRating_1" />
Assuming the above xml is named button_view.xml
View view = LayoutInflater.from(context).inflate(R.layout.button_view);
Button button = (Button) view.findViewById(R.id.BtnRating1);
This way you can inflate directly from xml
I gave you code how to create a button on dynamic time, you can set all property which you want for this button.
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.BtnRating1);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
This below line will create the button programetically
Button btn = new Button(ActivityContext);
Then you can also add button properties as below
RelativeLayout.LayoutParams btnparamLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, 90);
btn.setGravity(Gravity.CENTER_VERTICAL);
btnparamLayoutParams.height = adjustedDp;
btn1paramLayoutParams.setMargins(0, 0, 0, 100);
btn.setLayoutParams(btnparamLayoutParams);
btn.setBackgroundColor(Color.WHITE);
at the end add your button to the parentlayout.
LinearLayout ll = (LinearLayout)findViewById(R.id.linID);
ll.addView(btn)
You can do it in two way
create a table row layout with button and inflate it.
you can create button dynamically and add it in your table row see below code for creating buttons dynamically.
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
params.weight = 1;
params.gravity = Gravity.CENTER;
tablerow.addView(button,params);
Here this is context of activity.
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.
I am creating button on runtime like below...
Button newCategoryButton = new Button(this);
newCategoryButton.setText(catName);
newCategoryButton.setWidth(30);
newCategoryButton.setBackgroundResource(R.drawable.bnt);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(newCategoryButton, lp);
But my problem is that it disappear when I back from that activity.
I want this permanent. And another problem is that how to set this button below on other button??
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
Button newCategoryButton=new Button(this)
newCategoryButton.setText("Push Me");
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.addView(view, newCategoryButton);
You should put this code in the onResume(), not on the onCreate() method ;-)
If you want to use the button under another one, you will need to use a LinearLayout with a vertical orientation!
LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
myll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myll.setOrientation(LinearLayout.VERTICAL);
myll.addView(view, Button1);
myll.addView(view, Button2);
setContentView(myll)
I need to create a button programatically and have it centered on the layout, both horizontally and vertically. I am trying with the following code:
LinearLayout ll = (LinearLayout)findViewById(R.id.layoutItem);
Button b = new Button(this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
b.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
b.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
ll.addView(b);
But it's not working. The button comes out on top all to the left.
Any clues on how to fix this?
I would do something like:
LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();
ll.gravity = Gravity.CENTER;
b.setLayoutParams(ll);
see if that works.
Or you can use a RelativeLayout as your parent View and do the following:
this.testButton= (Button) this.findViewById(R.id.testButton);
RelativeLayout.LayoutParams testLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
testLP.addRule(RelativeLayout.CENTER_IN_PARENT);
this.testButton.setLayoutParams(testLP);
You can set several rules for the RelativeLayout.
I have to add a custom layout in a relative layout on a button click.
Again on another click add that same layout with another values above the previous inflated layout.
and it will continue like this.
I don't want to use list view.
I can add dynamically my custom layout but how to place it above the previous added.
on click of ADD button a new row will be added to my relative layout in xml, like ROW 1, ROW 2, and this will continue with ROW 3, ROW 4 etc.
you can use the addRule method of RelativeLayout.LayoutParams class. For example:
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
Doing this, you can set programmatically all params you would set in your layout xml file.
on button click event add this code:
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1=new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
linearLayout.addView(layout);
Define the layout having id #+id/layout and orientation Vertical in XML file.You will find the button layout being added on every click event.
I want to create a relative Layout dynamically through code with 2 Textviews one below the other.How to implement android:layout_below property through code in Android.
can anyone help me in sorting out this issue.
Thanks in Advance,
final TextView upperTxt = (...)
upperTxt.setId(12345);
final TextView lowerTxt = (...);
final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(this, null);
params.addRule(RelativeLayout.BELOW, 12345);
lowerTxt.setLayoutParams(params);
Here is my solution for my special Problem.
In case the username wouldn't be found in the db i had to create a RelativeLayout that looks like the xml-generated one.
// text view appears on top of the edit text
enterNameRequest = new TextView(mainActivity.getApplicationContext());
// fill the view with a string from strings.xml
enterNameRequest.setText(mainActivity.getResources().getString(R.string.enterNameRequest));
// edit text appears below text view and above button
enterName = new EditText(mainActivity.getApplicationContext());
enterName.setId(667);
// button appears at the bottom of the relative layout
saveUserName = new Button(mainActivity.getApplicationContext());
saveUserName.setText(mainActivity.getResources().getString(R.string.useUserName));
saveUserName.setId(666);
// generate the relative layout
RelativeLayout layout = new RelativeLayout(mainActivity.getApplicationContext());
layout.setId(668);
// set a background graphic by its id
layout.setBackgroundDrawable(mainActivity.getApplicationContext().getResources().getDrawable(R.drawable.background_head_neutral));
// runtime told me that i MUST use width and height parameters!
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ABOVE, 666);
enterName.setLayoutParams(params2);
LayoutParams params3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ABOVE, 667);
enterNameRequest.setLayoutParams(params3);
LayoutParams params4 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 668);
saveUserName.setLayoutParams(params4);
// add views
layout.addView(enterNameRequest);
layout.addView(enterName);
layout.addView(saveUserName);
/* todo: set button action */
mainActivity.setContentView(layout);
What i found out additionally:
It is not so good to manipulate the layout manually from within java!
You should better use a new Activity and set a new layout in it.
This way, the application-code is readable a lot better!
I even tried to set several layouts (not manually, but wit setContentView) in one activity, and it turned out that i didn't know where what was accessing what else... Also, i had a great problem in adding onClickListeners... so you better use -- android:onClick="myButtonMethod" -- in your button tag in the xml and have a method in your according activity, which uses the layout, like this:
public void myButtonMethod(View v){
// do stuff
}
This improves performance because you are not using additional Listeners - but you use the already available Listener that is bound to your activity in every case.
u can try this
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);``
leftMarginParams.leftMargin = 50;
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)