I am using an intent to display a web page on a button click:
link1Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
Uri uri = Uri.parse( "http://www.youtube.com" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
}
Is there a way to use an intent to display some text?
sending side
linkbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent n = new Intent(youractivity.this,secondactivity.class);
n.putExtra("param", "your string to pass");
startActivity(n);
}
});
Receiving side
TextView tv = (TextView)findViewById(R.id.textview);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String val = extras.getString("param");
tv.setText(val);
}
Related
im kinda new to programming and would like to make some sort of like a login and register page. so from my login page i click register and it would go to the register page and i want the username/password i get from the register page to be used in the previous login page to login into the app. But i cant seem to set the username/password using the result. Only able to set the textview. pls help heres the code
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.etName);
Password = (EditText)findViewById(R.id.etPass);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
Info.setText("No Of Attempts Remaining: 5");
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Name.getText().toString(),Password.getText().toString());
}
});
}
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
else{
counter--;
Info.setText("No Of Attempts Remaining: " + String.valueOf(counter));
if(counter == 0){
Login.setEnabled(false);
}
}
}
public void facebooklogin(View myview){
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
}
public void register(View myview) {
Intent i = new Intent(this, RegisterActivity .class);
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String reginame = data.getStringExtra("NAME");
String regipass = data.getStringExtra("PASS");
Name.setText("" + reginame);
Password.setText("" + regipass);
}
}
}
How do i set the
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
to be equal to the onActivityResult reginame and regipass
your condition is wrong it should be like this:-
private void validate(String userName, String userPassword){
if(!userName.equals("") && !userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
First Activity.
Send information
Intent send = new Intent(MainActivity.this, Main2Activity.class);
send.putExtra("login",editText.getText());
send.putExtra("password",editText1.getText());
startActivity(send);
Second Activity.
Get Information
if(getIntent()!=null){
Intent intent = getIntent();
editText.setText(intent.getStringExtra("login")) ;
editText1.setText(intent.getStringExtra("password")) ;
}
I am creating an app in which a user can create events that are happening on campus. I would like to have a way for a user to select a location on a map on a button push. The button would open an Google Maps intent which the user could then place a marker or drag one to their desired location. Once selected the coordinates would return as a pair of doubles. I will be then storing these in a database which displays the points on another activity.
I need to know how to select a location on a map intent and return the selected coordinates. Here is the code for this activity. I have removed some of the extraneous methods for date picking and such.
public class CreateEvent extends Activity {
EditText startTimeField;
EditText endTimeField;
EditText startDateField;
EditText endDateField;
EditText locationField;
Calendar startTime;
Calendar endTime;
ImageView mImageView;
Bitmap imageBitmap;
Button capture;
Button locationButton;
static final int REQUEST_IMAGE_CAPTURE = 1;
SimpleDateFormat time_format = new SimpleDateFormat("hh:mm a", Locale.US);
SimpleDateFormat date_format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
startTime = new GregorianCalendar();
endTime = new GregorianCalendar();
endTime.set(Calendar.HOUR, endTime.get(Calendar.HOUR) + 1);
startTimeField = (EditText)this.findViewById(R.id.startTimeField);
endTimeField = (EditText)this.findViewById(R.id.endTimeField);
startDateField = (EditText)this.findViewById(R.id.startDateField);
endDateField = (EditText)this.findViewById(R.id.endDateField);
locationField = (EditText) this.findViewById(R.id.locationField);
startTimeField.setKeyListener(null);
endTimeField.setKeyListener(null);
startDateField.setKeyListener(null);
endDateField.setKeyListener(null);
startTimeField.setText(time_format.format(startTime.getTime()));
endTimeField.setText(time_format.format(endTime.getTime()));
startDateField.setText(date_format.format(startTime.getTime()));
endDateField.setText(date_format.format(endTime.getTime()));
mImageView = (ImageView) this.findViewById(R.id.eventPhoto);
if(savedInstanceState != null){
imageBitmap = savedInstanceState.getParcelable("eventPhoto");
if(imageBitmap != null){
mImageView.setImageBitmap(imageBitmap);
}
}
capture = (Button) findViewById(R.id.photoButton);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
});
locationButton = (Button) findViewById(R.id.locationButton);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
#Override
public void onSaveInstanceState(Bundle outstate){
outstate.putParcelable("eventPhoto", imageBitmap);
super.onSaveInstanceState(outstate);
public void makeEvent(View view){
Intent intent = new Intent();
intent.putExtra("Event", new Event(imageBitmap, ((EditText)findViewById(R.id.eventField)).getText().toString(),
((EditText)findViewById(R.id.locationField)).getText().toString(), startTime.getTime(),
endTime.getTime(), ((EditText)findViewById(R.id.descriptionField)).getText().toString()));
// intent.putExtra("Bitmap", );
// intent.putExtra("Name", );
// intent.putExtra("Location", );
// intent.putExtra("Start", );
// intent.putExtra("End", );
// intent.putExtra("Description", );
setResult(RESULT_OK, intent);
finish();
}
}
All other questions regarding this have very outdated answers. Thanks for any help.
EDIT
Here is the code I am currently using for the intent:
locationButton = (Button) findViewById(R.id.locationButton);
locationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("geo:42.273856,-71.805976?z=16");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
}});
in My Main Activity I am pasing data using the data using intent using the putextra()
method
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
in the next Activity I am receiving that intent
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
and The application crashes
am I doing wrong with intents?? which is the right way to pass data between
activities?? suggestions and advises are needed...
thanks!!
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//While get this data:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
Try this
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
and
Bundle bndl = this.getIntent().getExtras()
I have a borderless button on in one of my layouts. When it is clicked, I want the intent to transfer both the button's tag and the text it contains. I am having trouble doing this.
This is what I have for when the button is clicked:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
This is what I have in the ViewGoal class:
public class ViewGoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
It throws an error at me and I am not sure why. Any help is appreciated!
why don't you just do this, just put your EXTRA instead of using bundle:
...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....
Later on in the other activity:
....
// get the message from the intent
Intent intent = getIntent();
String id = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
In onClick button, instead of this used classname.this
Some of my errors are removed by this. Try if it helps you.
public void openGoalWeek (View view) {
Intent intent = new Intent(className.this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
I have created a "Contact Us" page on my app and the idea is you have the option to send a picture to an already predetermind email address. The problem I have is its taking all the images from the gallery of the phone and sending them all in the email. All I want to do is send one picture. I cant seem to work out what to change to be able to just send one image. Is there anyone who could help?
Here is my code:
public class EmailActivity extends Activity {
Button send;
EditText address, subject, emailtext;
protected static final int CAMERA_PIC_REQUEST = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
send=(Button) findViewById(R.id.emailsendbutton);
address=(EditText) findViewById(R.id.emailaddress);
subject=(EditText) findViewById(R.id.emailsubject);
emailtext=(EditText) findViewById(R.id.emailtext);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if
(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
}
File pngDir = new File(
Environment.getExternalStorageDirectory(),
"Android/data/com.random.jbrefurb/quote");
if (!pngDir.exists())
pngDir.mkdirs();
Uri pngUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "random#yahoo.co.uk"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
emailIntent.setType("image/jpeg");
EmailActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// fire intent
finish(); // finish current activity
Intent austinIntent = new Intent(view.getContext(),
ContactActivity.class);
startActivityForResult(austinIntent, 0);
}
});
Button camera = (Button) findViewById(R.id.button2);
camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK){
Bitmap x = (Bitmap) data.getExtras().get("data");
((ImageView)findViewById(R.id.imageView1)).setImageBitmap(x);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.BUCKET_ID, "test");
values.put(Images.Media.DESCRIPTION, "test Image taken");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
x.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (FileNotFoundException e) {
//
}catch (IOException e){
//
}
} }
}
Many thanks in advance
this is code i used in my application try if you need help..
private OnClickListener shareemail=new OnClickListener(){
#Override
public void onClick(View v) {
String address = "your emailaddress";
File filee;
if(address.length()==0)
{
AlertDialog.Builder ab=new AlertDialog.Builder(null);
ab.setMessage("Email Address must not be empty!");
ab.setPositiveButton("OK", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
ab.show();
}
else
{
ArrayList<Uri> uris = new ArrayList<Uri>();
Uri u;
Intent emailSession = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailSession.putExtra(Intent.EXTRA_SUBJECT,"your subject");
emailSession.setType("images/*");
emailSession.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {address});
emailSession.putExtra(android.content.Intent.EXTRA_TEXT,"body text");
FileWriter fw;
BufferedWriter bw;
try{
filee = new File(path of image you want to send);
if(filee.exists())
{
Uri u1 = Uri.fromFile(filee);
uris.add(u1);
emailSession.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailSession);
}
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}}
};