Use phone number outside the app? - java

I don't know if it's possible but in my app I got an activity that show a phone number (retrieve from the web).
Can I send this number to the main phone app of android? For example to call it or save it?

You can create a tel:... Uri, where the ... is replaced by your phone number, then use that Uri with ACTION_DIAL or ACTION_CALL. For example:
package com.commonsware.android.dialer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class DialerDemo extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void dial(View v) {
EditText number=(EditText)findViewById(R.id.number);
String toDial="tel:"+number.getText().toString();
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));
}
}
The code shown above is from this sample project.

Related

Save markers to csv file

I am wondering how to when the user closes the app, save the markers to a file then load them back in when the app is opened again
Here is my code:
package com.example.mapapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends Activity
{
MapView mv;
StringBuffer filePath;
File file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// This line sets the user agent, a requirement to download OSM maps
Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
setContentView(R.layout.activity_main);
mv = (MapView)findViewById(R.id.map1);
ItemizedIconOverlay<OverlayItem> items;
mv.setBuiltInZoomControls(true);
mv.getController().setZoom(11);
mv.getController().setCenter(new GeoPoint(53.3710,-1.4502));
}
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.addpoi)
{
// react to the menu item being selected...
Intent intent = new Intent(this,AddPoi.class);
startActivityForResult(intent,0);
return true;
}
Intent intent = new Intent(this,AddPoi.class);
startActivityForResult(intent,0);
return false;
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_mad_assignment, menu);
return true;
}
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
if(requestCode==0)
{
if (resultCode==RESULT_OK)
{
Bundle extras=intent.getExtras();
String poiName= extras.getString("poiName");
String poiType= extras.getString("poiType");
String poiDesc= extras.getString("poiDesc");
ItemizedIconOverlay<OverlayItem> items = new ItemizedIconOverlay<>(this, new ArrayList<OverlayItem>(), null);
OverlayItem marker = new OverlayItem(poiName, poiType,poiDesc, new GeoPoint(53.3710,-1.4502));
items.addItem(marker);
mv.getOverlays().add(items);
Log.d("NAME",poiName);
Log.d("NAME",poiType);
Log.d("NAME",poiDesc);
}
}
}
}
Add marker code:
package com.example.mapapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import com.example.luke.madassignment.R;
public class AddPoi extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_poi);
Button addPoiButton = (Button)findViewById(R.id.addPoi);
Button cancelButton = (Button)findViewById(R.id.cancel);
addPoiButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle=new Bundle();
EditText poiName = (EditText)findViewById(R.id.editTextPoiName);
EditText poiType = (EditText)findViewById(R.id.editTextPoiType);
EditText poiDesc = (EditText)findViewById(R.id.editTextPoiDesc);
String poiNameString = poiName.toString();
String poiTypeString = poiName.toString();
String poiDescString = poiName.toString();
bundle.putString("poiName",poiNameString);
bundle.putString("poiType",poiTypeString);
bundle.putString("poiDesc",poiDescString);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}
}
The problem is i cant figure out a way to save the users marker to a file if the app is closed and then load back in the markers when the app is opened again
Use built in sharedPref, simple key value storage, to save data in persistence mode. Check doc here
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types. You can use SharedPreferences to save any primitive data:
booleans, floats, ints, longs, and strings. This data will persist
across user sessions (even if your application is killed).
https://developer.android.com/guide/topics/data/data-storage.html#pref
Using OSMBonusPack, you can grab your markers in a KML structure, then save the KML file locally (in onStop).
And load back this KML file when the app is opened (in onCreate).

How to forward edit text box to intent url in ADT?

So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes

#override error: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.

Android Development: "Cannot be resolved"

Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project

Asking name once and storing it afterwards

What I want is that when Activity starts first time, it first checks if nimiolemas is true. Since it just starts, then it can't be true. So, it will automatically starts new activity and also asks to get me info. In activity 2 person can type their name and when they press Ok, info will be sent back to activity one. Now, I don't know really how to change there Boolean to true and send that as well, so for now I told to change nimiolemas true before launching activity 2.
After pressing ok, it sends back to activity one and does the check again. Since it should be now true and also able to retrieve information about persons name, then it will go to true condition and print that name on screen in first activity. Now whenever program is launched, it will skip asking name and will straight show the person name :).
But it doesn't work exactly as I want. Before i put boolean, it actually went to second activity, but I couldn't get data so well. I have been working on solution for too long and I really appreciate help. If I find mistakes I can study from that more then searching on solution all over internet for next 10 hours :(.
I might have made some things very wrong, so please let me know and teach me! I really want to get better in this! So far I have done:
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainStuff extends Activity {
String tyybinimi;
TextView tere;
Boolean nimiolemas;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.TERE);
Intent i = new Intent(this, nimekysija.class);
tyybinimi = i.getStringExtra("nimi");
if (nimiolemas = true) {
System.out.print(tyybinimi);
} else {
startActivity(i);
nimiolemas = true;
finish();
}
}
}
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
Intent resultIntent;
EditText nimi;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
Button kysOk = (Button) findViewById(R.id.bNimekysija);
nimi = (EditText) findViewById(R.id.etNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
String nimiS = nimi.getText().toString();
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("nimi", nimiS);
startActivity(new Intent("viimane.voimalus.MAIN"));
finish();
}
});
}
}
Instead of using a boolean, you should be using Shared Preferences.

Categories