Dynamically adding a custom layout one below other - android

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.

Related

Adding a dynamic view - Layout gravity problem

I am adding a dynamic layout view which looks like this.
I am appending this view on button click. Here is the code for this view on button click
public void addDynamicView() {
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lHori = new LinearLayout(this);
lHori.setOrientation(LinearLayout.HORIZONTAL);
//lHori.setWeightSum(3);
lHori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lVertical = new LinearLayout(this);
lVertical.setOrientation(LinearLayout.VERTICAL);
//lVertical.setWeightSum(2);
lVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)));
button.setVisibility(View.VISIBLE);
button.setText("button");
TextView txtEmpName = new TextView(this);
txtEmpName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
txtEmpName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpName.setText("txt1");
TextView txtEmpDropTime = new TextView(this);
txtEmpDropTime.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpDropTime.setText("txt2");
lVertical.addView(txtEmpName);
lVertical.addView(txtEmpDropTime);
lHori.addView(lVertical);
lHori.addView(button);
relativeLayout.addView(lHori);
linear.addView(relativeLayout);
}
The problem is I want all the buttons to be placed below the click button.
But I get output like this
How can I place all Button with text Button below the Click button?
If you two do with xml than it's so simple just make a layout which you want after click on click button after that set visibility gone for all layout accept click button next in Java on button click you can call visibility view to make that layout ...
Hope so it will works for you..
You can create a LinearLayout lButton:
LinearLayout lButton = new LinearLayout(this);
lButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
then set its gravity as Gravity.END
lButton.setGravity(Gravity.END)
and add the button into it
lButton.addView(button);
lHori.addView(lVertical);
lHori.addView(lButton);

Add UI elements dynamically in android

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.

Android dynamically creating a Button

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.

how to add dynamic image with horizontal scrollview/listview

I have to develop five to six horizontal scrolling view,in this scrolling image add dynamic using adapter.
Feature:
1) Image can be drag and drop one scroll view to another
2) image can be move one horizontal scrolling to another view
3) image can be selected/unselected
4) with with all version
i am using this library, but sometime scrolling is not that much smooth(chopping)
Check this links may be it's useful to you.
1) Insert a view dynamically in a HorizontalScrollView in Android
2) http://android-er.blogspot.in/2012/07/implement-gallery-like.html
3) Horizontal ListView like Google Catalogs
Using this code you can add android control programatically to linear layout and just add Horizontal Scrollview to Linear Layout through xml
You will get horizontally listview.
//My coding here.
String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;
myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);
for(int i =0;i<6;i++)
{
LinearLayout li=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params1.setMargins(30, 20, 30, 0);
//add textView
valueTV = new TextView(this);
valueTV.setText(""+name[i]);
valueTV.setId(5);
valueTV.setLayoutParams(paramsnew);
valueTV.setGravity(Gravity.CENTER);
// adding Button to linear
valueB = new Button(this);
valueB.setText(""+name[i]);
valueB.setId(i);
valueB.setLayoutParams(params);
valueB.setOnClickListener(this);
valueB.setGravity(Gravity.CENTER);
// adding Imageto linear
img = new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(paramsnew);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}

Android Different Ways To Add LayoutParams

I noticed there are two ways to add LayoutParams programmatically to any view and am curious to ask do they have different meanings as well.
Example 1
In this example, setting LayoutParams directly to the button.
LinearLayout parent = new LinearLayout(this);
Button btnNew = new Button(this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
button.setLayoutParams(params);
parent.addView(btnNew);
Example 2
In this example adding layoutparams to button when it's being added to the parent view.
LinearLayout parent = new LinearLayout(this);
Button btnNew = new Button(this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
parent.addView(btnNew, params);
What's different in both?
There is no difference. If you check the android source code, it specifies that if no layout params are specified, the defualts params of the view group are set. When you use the method
addView(child,params);
It calls another method named addViewInner which just sets the params object to the child object.
The source code from the ViewGroup.java can be seen here

Categories

Resources