In this post I will show you how to add and remove a view dynamically at run-time.
This will be explained in a very simple activity in which when you will click the Add button, a new View will be added in the main layout(screen visible to you), and when you will click the Remove button, the newly added view will be removed from the main layout.
1. First of all make a layout class for the view which you want to inflate at run time. Here I'm naming it as row.xml and add the following code to it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<TextView
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/remove"/>
</RelativeLayout>
2. Now in the MainActivity.java class just add the below code snippet:
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// code to inflate the view to be added at run time (row.xml)
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
}
}
3. The layout file for MainActivity.java class named activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add"/>
<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
That's all. Compile and run the app and after pressing the Add button you can see that a new view is added below to the Add button.
Share and comment if any issue.
This will be explained in a very simple activity in which when you will click the Add button, a new View will be added in the main layout(screen visible to you), and when you will click the Remove button, the newly added view will be removed from the main layout.
1. First of all make a layout class for the view which you want to inflate at run time. Here I'm naming it as row.xml and add the following code to it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<TextView
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/remove"/>
</RelativeLayout>
2. Now in the MainActivity.java class just add the below code snippet:
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// code to inflate the view to be added at run time (row.xml)
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
}
}
3. The layout file for MainActivity.java class named activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add"/>
<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
That's all. Compile and run the app and after pressing the Add button you can see that a new view is added below to the Add button.
Share and comment if any issue.
Brujos en Elgin refers to the presence of witchcraft or magic practitioners in Elgin, a town in Scotland. These individuals, often called brujos, are believed to have special skills in using herbs, spells, or rituals.
ReplyDeleteKirkland Signature Minoxidil 5% is a hair treatment solution designed to help people who are experiencing hair loss. It is easy to apply directly to the scalp and works by improving blood flow to the hair follicles, which helps to stimulate hair growth.
ReplyDeleteBoom Boom Nasal Stick is a natural inhaler designed to boost energy, mental clarity, and focus using a blend of essential oils. It provides an immediate refreshing sensation by delivering aromas like menthol, peppermint, and eucalyptus directly to the nasal passages. Compact and portable, the stick is commonly used by athletes and individuals looking for an easy way to rejuvenate themselves throughout the day. It is marketed as a healthier alternative to caffeine or sugar-based energy products, with quick and natural benefits.
ReplyDelete
ReplyDeleteAviator Game is a popular online betting game that brings a thrilling experience of flying and winning big rewards. The game centers around a plane that takes off, and players place bets on how far it will go before it flies away.
Botánica en Waukegan il is a shop where people go to find spiritual and religious items. These stores sell things like candles, herbs, oils, incense, and statues that people use for prayers, healing, and protection. Many customers visit botánicas to get supplies for Santería, Tarot readings, or other spiritual practices.
ReplyDeleteAn FBI agent in Washington, D.C., was recently carjacked, which means someone stole the agent's car by force. This incident happened in the city, highlighting a rise in carjackings in the area. The suspect approached the agent, demanded the vehicle, and then drove away.
ReplyDeleteSEO Backlinks are links from one website to another, helping to improve a website’s ranking on search engines like Google. They are important because search engines see them as a sign of trust and authority. When a website has many quality backlinks, it looks more reliable and valuable. To get good backlinks, websites can create helpful content, collaborate with others, or share their content on social media. However, it’s important to focus on quality, not just quantity, as bad backlinks can harm a website's ranking.
ReplyDeleteFive Guys has got you covered. Known for their quality ingredients and made-to-order meals, Five Guys sandwiches are packed with flavor and made just the way you like. Whether you prefer a classic BLT, a rich grilled cheese, or a veggie sandwich loaded with toppings, there’s something here to satisfy every craving.
ReplyDeletehttps://sgeats.net/wp-content/uploads/2024/01/FIVE-GUYS-SANDWICHES-MENU-SINGAPORE-1024x411.webp
Dónde comprar pistones para motores de autos antiguos en proveedores especializados en piezas de repuesto para vehículos clásicos. Empresas como Meka Engine Parts ofrecen pistones originales y personalizados que aseguran un ajuste perfecto y alto rendimiento. Además, puedes encontrar piezas para restauración, adaptadas a las necesidades específicas de los motores vintage.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteJack in the Box menu offers a wide variety of food items, making it a popular choice for many. They are best known for their burgers, like the Jumbo Jack and the Sourdough Jack, which are flavorful and filling. You can also find crispy chicken sandwiches, tacos, and breakfast items served all day. Their curly fries and regular fries are customer favorites, often paired with refreshing drinks or shakes.
ReplyDeletehttps://jackintheboxxmenu.us/
The Keg locations are across North America, offering convenient access to their popular dining experience. Whether you're in the U.S. or Canada, you can find a Keg restaurant near you for quality meals and a welcoming atmosphere. Each location is designed to provide excellent service and comfort...https://thekegmenu.org/locations/
ReplyDelete