i am creating a simple android application consist from 3 activities.
- Main Activity
- Second Activity
- List Page
the first activity has a button that display the listPage using the intent after the user select an item from the list and click ok the system will transfer him to the second Activity that contain a edit box that the selected item will be display in it. and after the user click get back message button the system will transfer him to the MainActivity and display the selected item in a Toast .
can anyone help me to accomplish this tasks????
MainActivity.java
package com.devleb.intentmenudemoapp;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView txt1, txt2;
EditText edittxt1, edittxt2;
Button btn1, btn2, btn3, btn4;
String msgStr, msgStr2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
edittxt1 = (EditText) findViewById(R.id.edit1);
edittxt2 = (EditText) findViewById(R.id.edit2);
btn1 = (Button) findViewById(R.id.btn);
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.btn3);
btn3.setOnClickListener(this);
btn4 = (Button) findViewById(R.id.btn4);
btn4.setOnClickListener(this);
registerForContextMenu(edittxt1);
registerForContextMenu(edittxt2);
}
@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) {
// TODO Auto-generated method stub
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private CharSequence beautify(String originalText, String selectedStyle) {
// TODO Auto-generated method stub
Spanned answer = null;
if (selectedStyle.equals("BOLD"))
answer = Html.fromHtml("<b>" + originalText + "</b>");
else if (selectedStyle.equals("ITALIC"))
answer = Html.fromHtml("<i>" + originalText + "</i>");
else if (selectedStyle.equals("NORMAL"))
answer = Html.fromHtml("<normal>" + originalText + "</normal>");
return answer;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String value;
switch (v.getId()) {
case R.id.btn:
value = edittxt1.getText().toString();
txt2.setText(value);
break;
case R.id.btn2:
// value = edittxt1.getText().toString();
Intent i = new Intent(this, ListPage.class);
//i.putExtra("msgFromFirstActivity", value);
startActivity(i);
break;
case R.id.btn3:
Intent ii = new Intent();
ii.setType("image/pictures/*");
ii.setAction(Intent.ACTION_GET_CONTENT);
startActivity(ii);
break;
case R.id.btn4:
// get back message from second activity
Intent iii = new Intent(this, SecondActivity.class);
startActivityForResult(iii, 1);
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data.getStringExtra("MESSAGE");
Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
SecondActivity.java
/*get back the result from the list page and display it in the edit
text box then after clicking show the result in the first activity as
toast
*/
package com.devleb.intentmenudemoapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity {
TextView txt;
EditText edittxt;
String result;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/*
Intent iList = new Intent(this, SecondActivity.class);
startActivityForResult(iList, 1);
*/
edittxt = (EditText) findViewById(R.id.edit_txt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result = edittxt.getText().toString();
Intent i = new Intent();
i.putExtra("MESSAGE", result);
setResult(1, i);
finish();
}
});
}
/*@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data
.getStringExtra("MessageFromsecondActivity");
// Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
edittxt.setText(returnMSG);
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
ListPage.java
package com.devleb.intentmenudemoapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ListPage extends ListActivity {
TextView txt;
private static final String[] items = { "item1", "item2", "item3", "item4",
"item5", "item6", "item7", "item8", "item9" };
Button btn;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
txt = (TextView) findViewById(R.id.txt_list);
btn = (Button) findViewById(R.id.btn_list);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*
result = txt.getText().toString();
Intent i = new Intent();
i.putExtra("MessageFromsecondActivity", result);
setResult(1, i);
finish();
*/
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
txt.setText(items[position]);
super.onListItemClick(l, v, position, id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
}
until now this what i did from here how to continue or what i need to add and change to make my tasks
What here you can do is, To pass data from first activity to second activity
Now, get data in second activity
And then set this value to edittext