I'm adding a linearlayout dynamically to my screen as the user clicks on add button, so that he'll know what he has typed previously. I have a reset button on the action bar. When the user clicks on the reset button I'm removing all the dynamically added views from screen. I'm able to do that by removing the entire dynamically created view. (In my code it's LinearLayout's container object)
Then again the user will type something and then click on add button. Now my issue is I'm able to remove all the item in the view, but after removing when I type anything and click on Add button, the dynamic view are not getting created. Can anyone solve this issue. I want to add the views again after "resetting" the screen.
Here is the row.xml file which contains the linearlayout that I'm adding dynamically.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView_Attribute"
android:layout_width="100dp"
android:layout_height="20sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text=""
android:textStyle="bold" />
<TextView
android:id="@+id/textview_Choosen"
android:layout_width="100dp"
android:layout_height="20sp"
android:layout_below="@id/textView_Attribute"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="" />
<Button
android:background="@android:color/transparent"
android:id="@+id/btn_Remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="@string/remove" />
</RelativeLayout>
Here is the main.xml on which I want to add the row.xml dynamically.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="200dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Click on me to add dynamic layout" />
</LinearLayout>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout> </ScrollView>
Here is the activity_main_action.xml (put in res/menu) in which i've the refresh button for action bar. (sorry I can't send png image of it here)
<?xml version="1.0" encoding="utf-8"?>
menu xmlns:android="http://ift.tt/nIICcg" >
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="Refresh">
</item></menu>
Here is the MainActivity.java in which I'm adding the code to add the row.xml on my main.xml.
public class MainActivity extends Activity {
LinearLayout container;
Button myButton, buttonRemove;
EditText myEditText;
TextView tv_Title, tv_Choosen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the container for the dynamic text viewing
container = (LinearLayout) findViewById(R.id.container);
myButton = (Button) findViewById(R.id.button1);
myEditText = (EditText) findViewById(R.id.editText1);
// we took the layout on which we want to show the dynamic textview(s)
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
buttonRemove = (Button) addView.findViewById(R.id.btn_Remove);
tv_Choosen = (TextView) addView.findViewById(R.id.textview_Choosen);
tv_Title = (TextView) addView.findViewById(R.id.textView_Attribute);
myButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
buttonRemove = (Button) addView.findViewById(R.id.btn_Remove);
tv_Choosen = (TextView) addView.findViewById(R.id.textview_Choosen);
tv_Title = (TextView) addView.findViewById(R.id.textView_Attribute);
tv_Title.setText("You typed:-");
tv_Choosen.setText(myEditText.getText().toString());
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh
RefreshItems();
return true;
default:
return super.onOptionsItemSelected(item);
}
}// refresh all the TextView which are created dynamically (DEO & SP)
private void RefreshItems() {
// TODO Auto-generated method stub
View myView = container;
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
}}
No comments:
Post a Comment