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.
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/
ReplyDeleteLeather short pants are a trendy and bold choice for those who want to make a fashion statement. Made from premium leather, these short pants provide a unique combination of durability, comfort, and style. Whether you're aiming for a casual look with a t-shirt or a more edgy vibe with a leather jacket, these versatile bottoms elevate your outfit instantly. Available in various cuts, colors, and designs, leather short pants are perfect for music festivals, casual outings, or adding a daring touch to your wardrobe. They're the go-to choice for anyone seeking a sleek, modern look with a hint of attitude.
ReplyDeletehttps://goodluckleather.com/collections/all
Craft for Art is about creating beautiful and meaningful items by hand. It combines skill and creativity to make things like pottery, jewelry, paintings, or handmade decorations. People use crafts to express their feelings, share stories, or bring beauty into everyday life. It's also a great way to relax and enjoy making something unique. https://craftforart.com/
ReplyDeleteBolly4u is a popular platform for movie lovers who enjoy Bollywood, Hollywood, South Indian, and Punjabi films in HD quality. With a vast collection of the latest movies and web series, it offers a seamless streaming experience for viewers worldwide. Whether you're searching for Hindi dubbed films or trending international hits, Bolly4u has something for everyone. Stay entertained with high-speed streaming and easy navigation. https://bolli4u.org/
ReplyDeleteMeka Classic Engine Parts is a company based in the Netherlands that specializes in providing engine parts for classic cars. With over 65 years of experience, they offer a wide range of products, including valves, pistons, and water pumps, suitable for various classic car brands and models https://www.mekaengineparts.com/
ReplyDeleteMcDo Breakfast Menu offers a delicious and convenient way to start the day, featuring a variety of satisfying options. From the classic Egg McMuffin and Sausage McMuffin to fluffy hotcakes and savory hash browns, there’s something for every breakfast lover.
ReplyDeleteMcDo Breakfast Menu offers a delicious and convenient way to start the day, featuring a variety of satisfying options. From the classic Egg McMuffin and Sausage McMuffin to fluffy hotcakes and savory hash browns, there’s something for every breakfast lover.
ReplyDelete