Source for activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Add View" />
<Button
android:id="@+id/remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Remove View" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Source for hidden.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hiddenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Text View"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Radio Button1" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button2" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button3" />
</RadioGroup>
</LinearLayout>
Source for MainActivity.java
package com.jitesh.usinglayoutinflaterdynamically;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View buttonAdd = findViewById(R.id.add);
buttonAdd.setOnClickListener(this);
View buttonRemove = findViewById(R.id.remove);
buttonRemove.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
//Check if the Layout already exists
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.hiddenLayout);
if(hiddenLayout == null){
//Inflate the Hidden Layout Information View
LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearLayout2);
View hiddenInfo = getLayoutInflater().inflate(R.layout.hidden, myLayout, false);
myLayout.addView(hiddenInfo);
}
//Get References to the TextView
TextView myTextView = (TextView) findViewById(R.id.textView1);
// Update the TextView Text
myTextView.setText("This is not the original Text defined in the XML layout !");
break;
case R.id.remove:
View myView = findViewById(R.id.hiddenLayout);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
break;
// More buttons go here (if any) ...
}
}
}
No comments:
Post a Comment