have created a main activity with an editText and by clicking the button a TextView is created in the second activity and the text is passed to it. Now I've to go back to the 1st activity and give some new data so that the new data is also displayed along with the old data. But in my case instead of adding, a new page (with the new data) is displayed overlapping the old data. As a beginner i tried my level best but i cant find any solutions. Need some help...
Here's my main activity
public class MainActivity extends Activity
{
EditText txt1;
String value1;
Button button_1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1=(EditText)findViewById(R.id.editText1);
value1=txt1.getText().toString();
button_1=(Button)findViewById(R.id.button1);
button_1.setOnClickListener(onClick());
}
private OnClickListener onClick()
{
// TODO Auto-generated method stub
return new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Activity2.class);
i.putExtra("v",txt1.getText().toString());
startActivity(i);
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
My second Activity
public class Activity2 extends MainActivity
{
LinearLayout ll;
TextView txtview;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
ll=(LinearLayout)findViewById(R.id.linear);
txtview=new TextView(this);
txtview.setText(getIntent().getExtras().getString("v"));
//txtview.setText(String.valueOf(getIntent().getExtras().getString("1v")));
ll.addView(createNewTextView(txtview.getText().toString()));
Button exit=(Button)findViewById(R.id.button1);
exit.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
finish();
}
});
Button New=(Button)findViewById(R.id.button2);
New.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
private View createNewTextView(String text)
{
// TODO Auto-generated method stub
final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
final TextView txtview=new TextView(this);
txtview.setLayoutParams(lp);
txtview.setText("New text: "+text);
return txtview;
}
}
activity_main.xml file
<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="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"
tools:context="com.example.self.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text"
android:text="@string/e" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:text="@string/app_name" />
activity2.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="418dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/hl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button2"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:text="@string/New" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/exit" />
</LinearLayout>
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentmarklist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".act2"></activity>
</application>