Phone call click for textview - java

Okay so I've looked and tried to make sense of some other code that I've found but nothing has been working out for me. I'm trying to get the user to click the textview and have it take them to their phones dial-er.
Here's the Java:
public class Info extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.moreinfo);
Button appoint =(Button) findViewById(R.id.btnAppoint);
TextView phone =(TextView) findViewById(R.id.phoneTxt);
String url = phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
appoint.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Info.this, EmailContact.class));
}
});
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+"+phone.getText().toString().trim()));
startActivity(callIntent );
}
});
}
}
I've also put android: clickable = true so I don't see if that's the problem. Any help would be appreciated!

Change phone.getText().toString().trim() to use the View object supplied to the onclick method:
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:+"+((TextView)arg0).getText().toString().trim()));
startActivity(callIntent );
}
});
Additionally, if you just want to show the dialer with a phone number loaded, you're using the wrong intent action, Intent.ACTION_DIAL instead of Intent.ACTION_CALL will show the dialer. The Intent.ACTION_CALL that you're using will actually initiate the phone call, and to make that work, you need to add the appropriate permission to your Manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />

Based on your description its red because if you're going to use an class method level object within an anonymous obejct, the object must be defined as final.
final TextView phone =(TextView) findViewById(R.id.phoneTxt);
And for further measure, ensure you are using the right permission to do the call action in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />

Related

Android Studio: Annotations are not allowed here error

Don't know why this error is coming. I have used the same logic of adding #Override in my previous apps (Which I learned from Udacity).
I'm currently doing the Multiscreen Apps course. Do let me know if anyone else have completed this course or having the same error.
Here's what I wrote:
//Find the view that shows family category
TextView family = (TextView) findViewById(R.id.family);
//Send a clicklistner on that view
family.setOnClickListener(new View.OnClickListener()
{
#Override //here's the error
public void onClick (View v){
// create a new intent to open the {#link FamilyActivity}
Intent familyIntent = new Intent(MainActivity.this, FamilyActivity.class);
// start the new activity
startActivity(familyIntent);
}
});
Thanks,
Kvaibhav01.
Did you try this?
family.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
As I remember textView doesn't have onClickListener, it has onTouchListener and maybe this's a problem

lab problems for corse

hey every one i am sorry but i have to ask this question, it seems like a very easy issue but i just stuck! i have spent the last 2 hours going through the form and android developer resource site and i cant find the problem with my code.
first of all the startActivityForResult() will not send me the text back.
second every time i click on the Implicit Activation button the app crashes.
here is the main activity file:
public class ActivityLoaderActivity extends Activity {
static private final int GET_TEXT_REQUEST_CODE = 1;
static private final String URL = "http://www.google.com";
static private final String TAG = "Lab-Intents";
// For use with app chooser
static private final String CHOOSER_TEXT = "Load " + URL + " with:";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = (TextView) findViewById(R.id.textView1);
// Declare and setup Explicit Activation button
Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button);
explicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startExplicitActivation() when pressed
#Override
public void onClick(View v) {
startExplicitActivation();
}
});
// Declare and setup Implicit Activation button
Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button);
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
startImplicitActivation();
}
});
}
// Start the ExplicitlyLoadedActivity
private void startExplicitActivation() {
Log.i(TAG,"Entered startExplicitActivation()");
// TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
Intent explicitIntent = new Intent (ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
// TODO - Start an Activity using that intent and the request code defined above
startActivityForResult(explicitIntent, GET_TEXT_REQUEST_CODE);
}
// Start a Browser Activity to view a web page or its URL
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
// TODO - Create a base intent for viewing a URL
// (HINT: second parameter uses Uri.parse())
Intent baseIntent = new Intent (Intent.ACTION_VIEW, Uri.parse(URL));
// TODO - Create a chooser intent, for choosing which Activity
// will carry out the baseIntent
// (HINT: Use the Intent class' createChooser() method)
Intent chooserIntent = null;
chooserIntent.createChooser(baseIntent, CHOOSER_TEXT);
Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
// TODO - Start the chooser Activity, using the chooser intent
startActivity(chooserIntent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text.
if (resultCode == RESULT_OK){
mUserTextView.setText(data.getStringExtra("resulttext"));
}}}
and here is the explicit intent file:
public class ExplicitlyLoadedActivity extends Activity {
static private final String TAG = "Lab-Intents";
private EditText mEditText;
String resulttext="still waiting";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explicitly_loaded_activity);
// Get a reference to the EditText field
mEditText = (EditText) findViewById(R.id.editText);
// Declare and setup "Enter" button
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(new OnClickListener() {
// Call enterClicked() when pressed
#Override
public void onClick(View v) {
enterClicked();
}
});
}
// Sets result to send back to calling Activity and finishes
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
resulttext= mEditText.getText().toString();
// TODO - Create a new intent and save the input from the EditText field as an extra
Intent returntrip = new Intent ();
returntrip.putExtra("wayback", resulttext);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, returntrip);
// TODO - Finish the Activity
finish();
}
}
thank you guys so much i know i am a bother!
You are sending an extra data called "wayback" not "resulttext"
returntrip.putExtra("wayback", resulttext);
this will fix your code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
if (resultCode == RESULT_OK){
mUserTextView.setText(data.getStringExtra("wayback"));
}
Well, you won't get the result back correct from your onActivityResult because you are not using the correct key. You look inside for data.getStringExtra("resulttext") but you originally set the key as "wayback". You need to grab with that key.

Wifi Broadcast android inside android intent

I'm learning android and I'm stuck here:
The app which I'm writing scans for Wifi signals in background periodically. I am using android intent service for the same. The problem is, application never executes onReceive() method of BroadCastReceiver
Intent Code:
public class BackgroundIntent extends IntentService {
// Default Constructor
public BackgroundIntent() {
super("BackgroundIntent");
// TODO Auto-generated constructor stub
}
WifiManager mainWifi;
BroadcastReceiver receiverWifi;
private final Handler handler = new Handler();
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
// Get mainWifi
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (mainWifi.isWifiEnabled() == false) {
mainWifi.setWifiEnabled(true);
}
receiverWifi = new WifiReceiver();
doInback();
}
// Basically a thread which calls itself after 5000milli sec
public void doInback() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
mainWifi.startScan();
Log.i("Inside ", "doInBack");
// Call itself CODE GOES HERE :D
doInback();
}
}, 5000);
}
public class WifiReceiver extends BroadcastReceiver {
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void onReceive(Context c, Intent intent) {
// CODE NEVER GOES HERE :(
List<ScanResult> wifiList;
wifiList = mainWifi.getScanResults();
Log.i("Inside receiver", "yes");
}
}
}
MainActivity code which invokes android intent service
public class MainActivity extends Activity {
TextView texty;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent is called here
Intent myIntent = new Intent(MainActivity.this, BackgroundIntent.class);
startService(myIntent);
texty = (TextView) findViewById(R.id.textView1);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
return super.onMenuItemSelected(featureId, item);
}
}
Any idea what may be the cause? Is this wrong way to implement background wifi signal scan.
The same when I implement in Main Activity runs fine, so I'm guessing AndroidManifest.xml is correctly written..
Edit: I might've misinterpreted your goal here, If your just wondering why your onReceive() isn't firing, your activity has to register the receiver. Something like the following:
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
doInback();
Check out this link
So the issue was with the functions which I was using.
I was using onHandleIntent(intent) which is the wrong function. Right one is onStartCommand(intent, flags, startId) for service.
My bad sorry..

How to enter two activity links in another activity?

I know my question might be stupid but I am new in Android App development and the Eclipse things but reached to e problem that can't find solution in internet.
I am making multi-activity application and reached to a point where when i have two buttons in one of the activities and want each of them to lead to different other activities, the application crashes. When I lead them both to one activity, everything is fine. Here is my code and hope really my question not to be so stupid as I am thinking.
public class Home extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button myButton = (Button) findViewById(R.id.tables);
myButton.setOnClickListener(goToTables);
Button mySecondButton = (Button) findViewById(R.id.reservations);
mySecondButton.setOnClickListener(goToMenu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
private OnClickListener goToTables = new OnClickListener(){
#Override
public void onClick(View v) {
doButton();
}};
private void doButton()
{
startActivity(new Intent(this, Tables.class));
}
private OnClickListener goToMenu = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
doSecondButton();
}};
private void doSecondButton()
{
startActivity(new Intent(this, Menu.class));
}
}
The goToTables works perfectly but I am missing something important to change in goToMenu. My other activities are: Tables and Menu. Can somebody please tell me where I am wrong? Thanks in advance!
android:onClick="dobutton" try adding this in your button tag in xml code rather then using onclicklistner.
Try changing the name of your Menu activity or add the full name path of Menu.class in your intent, eg. com.myapp.Menu.class

How to get information about a button click on 1st activity and use it in the 3rd activity? - android

well there are 3 activities say Act1, Act2 and Act3.
in Act1 there are three buttons say But1, But2 and But3.
if we click any of the 3 three buttons it will end up showing Act2.
in Act2 there is only one button say Done.
my problem is when i click on the button Done in Act2 i should get the information about the button which is clicked in Act1 to show Act2 in a TextView in Act3.
i think i have made it clear..
i just need to know how to pass the information of the button click in Act1 to Act3
should i use bundles or something else? pls help me out with the logic and if possible a sample code? :)
You have a few options.
1) The Hardcore option would be to create a database and save / query the information.
2) You could also write to sharedPreferences.
SharedPrefExample
3) As was mentioned in the comment, if your only talking from one activity to another you can just intent.putExtra("key", value)
4) Lastly you can extend Application
Here is a good post that deals with global variables. How to declare global variables in Android?
Try to Use Intent
public void onClick(View arg0) {
Bundle bundle = new Bundle();
bundle.putInt("BtnAID", ID);
Intent It = new Intent();
It.setClass(A.this, B.class);
It.putExtras(bundle);
startActivity(It);
}
You can use SharedPreferences to store which button was pressed.
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefsEditor.putString("buttonPressed", "Button1");
prefsEditor.commit();
}
});
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefsEditor.putString("buttonPressed", "Button2");
prefsEditor.commit();
}
});
Button b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
prefsEditor.putString("buttonPressed", "Button3");
prefsEditor.commit();
}
});
And in the Activity in which you want to fetch which button was pressed:
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
String whichButton = appSharedPrefs.getString("buttonPressed", "");
if(whichButton.equals("Button1")
//do something
if(whichButton.equals("Button2")
//do something
if(whichButton.equals("Button3")
//do something

Categories