Add multiple custom views to layout programmatically - android

If I for example have empty layout like this:
layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
And some other Layout like this:
layout2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"/>
</LinearLayout>
I would like to add layout2.xml to layout1.xml programmatically. I would need to have multiple different version layout2.xml which I would add when I want. Each one would have different text on TextView and Buttons.
In case of regular Android View I would usually do it similarly like this with addView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
my_linear_layout.addView(tv1);
Button btn2 = new Button(this);
bt2.setText("WORLD");
my_linear_layout.addView(btn2);
}
How can I do it if I don't have something simple like TextView or Button, and if I have my own XML defined View?
Would it be possible to somehow put all the views inside adapter like for a ListView and then get it when needed and just call addView on those Views?

You can inflate the layout2.xml file, edit the texts, and add it to the first layout:
public class MyActivity extends Activity {
private ViewGroup mLinearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
addLayout("This is text 1", "This is first button", "This is second Button");
}
private void addLayout(String textViewText, String buttonText1, String buttonText2) {
View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);
TextView textView = (TextView) layout2.findViewById(R.id.button1);
Button button1 = (Button) layout2.findViewById(R.id.button2);
Button button2 = (Button) layout2.findViewById(R.id.button3);
textView1.setText(textViewText);
button1.setText(buttonText1);
button2.setText(buttonText2);
mLinearLayout.addView(layout2);
}
}
You may want to change android:layout_height of the layout2.xml root view to wrap_content.
If you are using ViewBinding, here is how it would look like for the addLayout function :
MyLayoutBinding binding = MyLayoutBinding.inflate(getLayoutInflater(), mLinearLayout, false);
binding.getTextView1().setText(textViewText);
binding.getButton1().setText(buttonText1);
binding.getButton2().setText(buttonText2);
mLinearLayout.addView(binding.getRoot());

layout1.xml contains ScrollView as parent layout and main LinearLayout as child if no row item more screen size ScrollView handle overflow item with scroll:
layout1.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Use LayoutInflater to add row item to parent LinearLayout:
private LinearLayout my_linear_layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
my_linear_layout = (LinearLayout) findViewById(R.id.my_linear_layout);
for (int i = 1; i <= 5; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.layout2, null);
TextView button1 = (TextView) view.findViewById(R.id.button1);
Button button2 = (Button) view.findViewById(R.id.button2);
TextView button3 = (TextView) view.findViewById(R.id.button3);
button1.setText("HELLO " + i);
button2.setText("HELLO " + i);
button3.setText("HELLO " + i);
my_linear_layout.addView(view);
}
}

Try this method
Add id to LinearLayout 'layout1.xml':
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
In onCreate:
LinearLayout firstlayout = (LinearLayout) findViewById(R.id.firstlayout);
LinearLayout secondlayoout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.layout2, null); // inflating view from xml
TextView btn1 = (TextView) secondlayoout.findViewById(R.id.button1);
btn1.setText("TEST");
firstlayout.addView(secondlayoout);

You can achieve this for view binding by using below.
val layoutBindingView: LayoutBinding =
LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "TextOne"
val layoutBindingView: LayoutBinding =
LayoutBinding.inflate(layoutInflater)
layoutBindingView.textView.text = "Text Two"

Related

how to set inflate layout center which added dynamically in linear layout Android

I am adding inflate layout dynamically in linear layout,i need to set layout center based on number of layouts.suppose if layouts are two , then it start showing from center.
following is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/hsv_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
and following are my class
public class ScrollTest extends Activity {
HorizontalScrollView hsv_category_list;
LinearLayout ll_placeHolder;
View layoutView[];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scroll_test);
hsv_category_list = (HorizontalScrollView)
findViewById(R.id.hsv_category_list);
ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
layoutView = new View[2];
for (int i = 0; i < 2; i++) {
layoutView[i] =
LayoutInflater.from(this).inflate(R.layout.category_list_item, null);
layoutView[i].setId(i);
View parent = layoutView[i].findViewById(i);
TextView tvTime = (TextView)
parent.findViewById(R.id.tv_arrival_time);
tvTime.setText("" + i);
ll_placeHolder.setGravity(Gravity.CENTER);
ll_placeHolder.addView(layoutView[i]);
}
}
}
i need to set the inflate layout start from center.
Please help me to solve this.Thank you.
Set layout gravity to center_horizontal in xml file like this
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:orientation="horizontal">
</LinearLayout>

Android place view dynamically relative view

Similar to a to do app. I have a top bar that is set by the xml file and at run time I would like to create some textviews which go directly under it. Also there will be a button that adds new text views when clicked.
When I create a new textview from the button how do I make sure that the new Textview stays under the static textviews and/or layouts that I have created. Right now it's not and it's just overlapping.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button btn = (Button) findViewById(R.id.Add_new);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddNewTask(v);
}
});
}
I tried both options to add a new textview and both of them just overalpp and I can't find any documentation on a method such as under/below, most I can find are just alight top and align bottom which wouldn't be useful in this case
public void AddNewTask(View v) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.overallview);
TextView tv= new TextView(this);
tv.setText("TEST");
rl.addView(tv);
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/overallview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.robertli.hustle.MainActivity"
tools:showIn="#layout/activity_main"
android:background="#00095e">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:id="#+id/topbar"
>
<TextView
android:id="#+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Hustle"
android:textSize="25dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="#id/topbar"
android:id="#+id/Add_new"
android:background="#00095e"
android:textColor="#ffffff"
android:clickable="true"
android:textSize="50dp" />
Suggestion : You should take a look at ListView to do this !!! ListView help you dynamically add the textview as well.
if you still want to adding directly to Relativelayout
refer this example:
RelativeLayout relativeLayout = new RelativeLayout(context);
View view = new View(context);
relativeLayout.addView(view);
code to dynamically add alignment in RelativeLayout
RelativeLayout.LayoutParams viewLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
viewLayoutParams.addRule(RelativeLayout.BELOW, R.id.your_view);
take a look at documentation for more
https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Array of image button and textview in android

I am trying to create an array in which each item will have an image button and below that image button will be a text view in center . Can anybody tell me how to do this. I tried the below code but not getting it worked
but=new ImageButton(this);
but.setFocusableInTouchMode(true);
but.setId(1);
imbrelp= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rel.addView(but,imbrelp);
tv= new TextView(this);
tvrelp= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvrelp.addRule(RelativeLayout.BELOW, but.getId());
tvrelp.addRule(RelativeLayout.CENTER_HORIZONTAL, but.getId());
rel.addView(tv, tvrelp);
setContentView(rel);
I would avoid trying to create a RelativeLayout in code. It's going to be too messy for what you want to do. Instead, create an xml layout file for your individual item, and then inflate it for each item in your array.
Container layout in res/layout/main.xml:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
Item layout in res/layout/item.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image" />
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image_button"
android:layout_centerHorizontal="true"
android:text="#string/my_text" />
</RelativeLayout>
Code in your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
ViewGroup container = (ViewGroup) findViewById(R.layout.container);
LayoutInflater inflater = LayoutInflater.from(this);
Item[] items = getMyArrayOfItems();
for (Item i : items) {
View itemView = inflater.inflate(R.layout.item, container, false);
ImageButton button = (ImageButton) itemView.findViewById(R.id.image_button);
TextView textView = (TextView) itemView.findViewById(R.id.text_view);
// TODO set the text and images
container.addView(itemView);
}
}

Android setlayout visibility on button click

In the following code on click of the button i want to hide the relative layout rl1 and show rl2 but my app crashes down after on button click.What am i doing wrong
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/welcome_1">
<ImageView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="360dp"
android:layout_marginRight="4dp"
android:src="#drawable/button1" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="#drawable/menu_2"
></RelativeLayout>
</RelativeLayout>
Java code
public class mockupsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ImageButton img1 = (ImageButton)findViewById(R.id.button1);
ImageView img1 =(ImageView)findViewById(R.id.button1);
img1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
// Toast.makeText(TouchpointmockupsActivity.this, "test", Toast.LENGTH_SHORT).show();
LinearLayout rl1 = (LinearLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
LinearLayout rl2 = (LinearLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);
}
});
}
}
Change LinearLayout to RelativeLayout in your code. You might be getting a classcastException.
Edit:
Change OnClick method as below
RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);

Inflating TextView and LinearLayout programmatically

The problem here I don't get the same output view result in these two cases, I want to fix case 1 to get same output result as case 2:
Inflate textview and linearlayout, and then add textview to linearlayout programmatically.
Add textview to linearlayout in xml.
Code and Output For Case 1:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#000000"
android:text="1"
android:textSize="20sp"
android:background="#AAAAAA"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
/>
onCreate method in LayoutTestActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lt = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
lt.addView(tv);
setContentView(lt);
}
output view (Not Correct)
Code and Output For Case 2:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#000000"
android:text="1"
android:textSize="20sp"
android:background="#AAAAAA"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
/>
</LinearLayout>
onCreate method in LayoutTestActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
output view (Correct)
I'm not sure if it makes any difference - but my suggestion is this:
Provide the android:id="#+id/linearLayout"-tag for the LinearLayout in your main.xml.
Then do this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
lt.addView(tv);
}
You might also want to test if there is a difference between supplying null or your LinearLayout lt as the second parameter when inflating the TextView e.g.:
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, lt);
You have to set the LayoutParams in the first case. In the second this is done automatically since TextView is in the xml. Hope this helps!
I think in the first place that you have to set the main layout in the setContentView() method directly and afterwards make the inflate.
setContentView(R.layout.main);
LinearLayout mainLayout = (Linearlayout) findViewbyId(R.id.mainLayoutId);
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, mainLayout, false);
mainLayout.addView(tv);
I think this way it should work.
To keep the xml layout params you have to pass the last two parameters to the inflate() method.

Categories

Resources