I am using following code to open Android Contacts
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts = (TextView) findViewById(R.id.txt_contacts);
btnContacts.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
txtContacts.setText("");
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
//display picked contact data.
}
}
}
Now I want to put Button at the top of this Contact activity when opened or add my own Menu in this Activity
Can any one guide me? Is this possible or not? If yes then please tell how to achieve this?
I don't believe that is possible as each Activity in Android is working on its own and by starting the Intent, you are basically giving the new Activity the focus (and control).
One way to do something like this would be to build a custom contact list activity that uses the public data providers to access the contacts and simply lists them then. Then you could add as many custom functions as you like or even add Intents for original actions (like viewing a contact's details).
Related
hi i am currently working on QR code scanner. I want to ask if it is possible to open another activity after the QR code have been scanned rather than getting it displayed as a toast?
below is my scanner file code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtexthello=(TextView)findViewById(R.id.textview_hello);
scanbtn=(Button)findViewById(R.id.btn1) ;
final Activity activity=this;
scanbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator l=new IntentIntegrator(activity);
l.setDesiredBarcodeFormats(l.QR_CODE_TYPES);
l.setPrompt("scan");
l.setCameraId(0);
l.setBeepEnabled(false);
l.setBarcodeImageEnabled(false);
l.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult res= IntentIntegrator.parseActivityResult(requestCode, resultCode,data);
if(res!=null)
if(res.getContents()==null)
{
Toast.makeText(this,"u cancelled scanning",Toast.LENGTH_LONG).show();
}
else
{
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
// Toast.makeText(this,res.getContents(),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
At a first reading it sounds as if the issue is how you're attempting to start the next activity, if not the activity itself.
Start a new app, and use a simple Button as the trigger for the new activity. Once you have the syntax and requirements correct there, apply what you've learned to the QR app.
If you're able to get the Toast to display from your existing code, then your difficulty has nothing to do with the QR code.
Maybe what you really need is to look at something like this (Start an Activity with a parameter) where you pass a parameter to the second activity.
In my Android app I am giving the possibility to tap on a Google +1 button.
This works fine with the following code from the developpers source material (https://developers.google.com/+/mobile/android/recommend):
mPlusClient = new PlusClient.Builder(this, this, this)
.clearScopes()
.build();
mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
Now I would like to detect when the user taps on this button, so I tried this:
mPlusOneButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(this, "Thanks for your +1", Toast.LENGTH_LONG).show();
}
});
But it doesn't do anything, it doesn't display the toast message.
As an alternative, I was thinking of having the player tap twice on the button : first being a regular Button object that would make the plus one button visible, second being the plusone button. but it is an awckward user experience
How can I detect user's tap on this button?
Don't use setOnPlusOneClickListener (or any listener) with SDK 13 (and perhaps future versions), it induces a bug (infinite rotation of the progress indicator in the +1 button when you click on it).
Use ActivityForResult to trigger the user choice.
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
startActivityForResult(intent, 0);
}
});
Check the result code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == -1) {
// OK or SHARE
} else {
//UNDO
}
}
Plus one button provides a listener function to detect button click. Here is the code.
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
Log.w(tag,"plus one click");
}
});
Suppose i wrote a camera app by using and intent(MediaStore.ACTION_IMAGE_CAPTURE) and i notice that when i run the app, the option menu displays "Edit shortcuts", how can i add on to this option menu or completely override the option menu to my own?
Below are snippet of my code:
public class GPSCam extends Activity implements LocationListener,
GpsStatus.Listener {
private Intent camIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpscam);
...
...
this.startCamera();
}
protected void startCamera() {
...
// create camera intent
camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// define where to keep the photo
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// this method will call onActivityResult() upon finishing an activity
// i.e. image captured
startActivityForResult(camIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
...
...
...
}
}
You cannot modify the options menus of other applications. If you do not want to use another application for taking photos, you are welcome to use the Camera object and take photos yourself.
I am having difficulty in framing this question but anyways here it goes, I have made two applications basically the TextToSpeech app and the SpeechToText application. They are working fine, now I am trying to merge them. Basically I want the the text to speech part to say the text entered in a text field , only if the user says the word "speak".That would be done using the speech to text part. Now the problem I am having is that android displays a list of outputs when the user says something, but I only one output that is "Speak" . How can I get only one output instead of a list.
Initially the app becomes active on button click.
The code I am trying is as follows:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "system Activated");
startActivityForResult(i, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == check && resultCode == RESULT_OK) {
String command = data
.getStringExtra(RecognizerIntent.EXTRA_RESULTS).toString();
if (command.equals(command_verify)) {
speak();
} else
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
public void speak() {
String text_to_speak = message_field.getText().toString();
talk.speak(text_to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
The Voice Recognizer returns you a list of guesses, where the first guess is the most accurate. You can use it calling list.get(0). Hope this helps.
First post, so please go easy.
I have an app with a handful of tabs, the first is opened on running the app.
One of the tabs is 'My Account' (a ListActivity) showing account options. I switch to this and if the user is not logged in, it, in turn, runs a UserLogon activity using the following:
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
To catch the result, I use the following code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
MainBlock ta = (MainBlock) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
finish();
}
if (requestCode == 100)
{
showAccountOptions();
}
}
In the UserLogon class, I have the usual fare; TextView's and Button's. The intention is that if the user cancels the login, it will revert to the initial tab, or if login is successful, it will show the Account Options. And this is indeed what does happen.
All good so far.
The problem I'm having is that if I cancel the login and return to the first tab, when I select the Accounts tab again, I'm not presented with the UserLogon activity. I was under the impression that finish() would close the UserLogon activity, and the Accounts activity but it would appear not.
So, my question is simply, how do I, in effect, restart the Accounts activity so that the user would be presented with the option to login once more.
We're good people and all willing to help ;-) I'll give it a shot. Still, I'm not quite sure I get that all right.
Basically you have an TabActivity which you setup and where you do something like that:
myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("account") && !loggedIn) {
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
}
}});
You're basically saying that the first Activity start of UserLogon works, but the second one doesn't, right? Did you debugged to that point to check whether you reach the code which starts the activity again?
Update based on comment
Your UserLogon should always provide a result information, here's a blueprint:
public class UserLogon extends Activity {
public void onCreate(Bundle bundle) {
// ... do something ...
// if activity is canceled this will be the "last" result
setResult(RESULT_CANCELED);
}
public void checkLoginOrSomethingLikeThat() {
Intent result = new Intent();
// put your results in this intent
setResult(RESULT_OK, intent);
// close activity since we have the information we need
finish();
}
}
The parent activity which is waiting for the result should do it like that:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// it's good practice to use a constant for 0 like LOGIN_REQUEST
// otherwise you will ask yourself later "What the hell did 0 stand for?"
if(requestCode == 0){
if (resultCode == RESULT_OK) {
// get needed data from data intent and react on that
} else {
// no success, react on that
}
}
// ... I don't know what your resultCode 100 is but handle itwith the same pattern
}