I am using 2.3.3 version of the Android SDK and I'm trying to pass data between two activities using the following code
Intent myIntent = new Intent(MainAct.this, FriendsActivity.class);
myIntent.putExtra(USER_NAME, ((EditText)findViewById(R.id.username)).getText());
MainAct.this.startActivity(myIntent);
In the FriendsActivity i'm retrieving the value using
Bundle b = getIntent().getExtras();
String user = b.getString(MainAct.USER_NAME);
But the user is null after these lines are executed. Not sure whats wrong here. Read similar questions on SO http://goo.gl/zOJfa but still the problem continue to be seen.
You need to pass (EditText)findViewById(R.id.username)).getText().toString() instead. getText() on an EditText View doesn't return a string, it returns an Editable Object.
just do (EditText)findViewById(R.id.username)).getText().toString() you are able to get string value.
Related
Have you ever try to add a tag like that.
First i thought it is an ImageSpan which is built by SpannableString then set to builder.setTitle(input: Charsequence).
I already tried but not working.
Do you guys know how Spotify can do that? or am i missing something, pls correct me.
Big thank
This isn't something you put into the title, artist or other field, but an explicit flag you can example on each item.
As explained in the Display additional metadata indicators for Android Auto:
The following constants can be used in both MediaItem description extras and MediaMetadata extras:
METADATA_KEY_IS_EXPLICIT: indicates whether the item contains explicit content. To indicate an item is explicit, use this constant as the key and the long METADATA_VALUE_ATTRIBUTE_PRESENT as the value.
They go on to use an example:
val extras = Bundle()
extras.putLong(
MediaConstants.METADATA_KEY_IS_EXPLICIT,
MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT)
// Add in other extras
val description =
MediaDescriptionCompat.Builder()
.setMediaId(/*...*/)
.setTitle(resources.getString(/*...*/))
.setExtras(extras)
.build()
return MediaBrowserCompat.MediaItem(description, /* flags */)
I need to access current activity of my app. I checked this official code
If i use this code below as documentation says:
PythonActivity = autoclass('org.renpy.android.PythonActivity')
i can't access activity and it returns error. So after searching i found this:
PythonActivity = autoclass('org.kivy.android.PythonActivity')
There is 2 function below. First one is opening webbrowser just like in documentation and IT WORKS:
from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
intent = Intent()
intent.setAction(Intent.ACTION_VIEW)
intent.setData(Uri.parse('http://kivy.org'))
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.startActivity(intent)
Now i need to recode this for my own script. I want to add new contact to Contacts. I got 'WRITE_CONTACTS' permission for that. in buildozer.spec:
android.permissions = WRITE_CONTACTS
But my own script returns me error. My function:
from jnius import autoclass,cast
PythonActivity=autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
JS=autoclass('java.lang.String')
intent=Intent()
intent.setAction(Intent.ACTION_INSERT)
intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')
intent.putExtra('ContactsContract.Intents.Insert.NAME',JS('Example Name'))
intent.putExtra('ContactsContract.Intents.Insert.PHONE',JS('7777777777'))
currentActivity=cast('android.app.Activity',PythonActivity.mActivity)
currentActivity.startActivity(intent)
Log:
jnius.jnius.JavaException: JVM exception occurred: No Activity found
to handle Intent { typ=ContactsContract.RawContacts.CONTENT_TYPE (has
extras) } android.content.ActivityNotFoundException
I don't know that Is this error eccours because of activity or my wrong type selection. I would be glad if anyone help me to handle this. Like this contents on media is very few. So i hope like this topics let people learn quickly just like me.
I don't know the Python syntax, but I can tell you what the problem is:
You are missing the Intent ACTION. You need to set the ACTION in the Intent to ContactsContract.Intents.Insert.ACTION (which has the string value "android.intent.action.INSERT")
Also, it looks like you are sending Android constant names instead of the actual string values of these things, for example:
you do this:
intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')
which sets the type in the Intent to the string value "ContactsContract.RawContacts.CONTENT_TYPE", which you can see here:
jnius.jnius.JavaException: JVM exception occurred: No Activity found
to handle Intent { typ=ContactsContract.RawContacts.CONTENT_TYPE (has
extras) } android.content.ActivityNotFoundException
but the type needs to be set to the Android constant ContactsContract.RawContacts.CONTENT_TYPE which has the string value "vnd.android.cursor.dir/raw_contact"
As I said, I don't know Python syntax, but you probably need to do something like this:
intent=Intent()
intent.setAction(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
or, if Python supports it, Intent has a constructor that takes an ACTION as a parameter, so this might also work:
intent=Intent(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
Let me know if any of this makes sense.
FINALLY, I found solution to access activities and getting Contacts datas.
Accessing correctly PythonActivity:
PythonActivity = autoclass("org.kivy.android.PythonActivity")
Accessing Contacts Java Class:
Contacts = autoclass("android.provider.ContactsContract$Contacts")
Accessing ContactsContract.CommonDataKinds.Phone Java Class (same):
Kinds = autoclass("android.provider.ContactsContract$CommonDataKinds$Phone")
Creating getContentResolver():
cr = PythonActivity.mActivity.getContentResolver()
Now we need to create 2 query. In first, we are going to find ID and need to use it in second query to find equal NUMBER. Also need to check is contact has phone number? too. After that we can loop second query to find number:
name_number_dict= {} # Name:Number # Change as you wish..
while (que1.moveToNext()): #Query 1 Loop
if que1.getString(que1.getColumnIndex(Contacts.HAS_PHONE_NUMBER)): #If has number
que2 = cr.query(Kinds.CONTENT_URI, None, Kinds.CONTACT_ID + " = " + que1.getString(que1.getColumnIndex(CONTACTS_ID)), None, None) #Query 2 Loop
while (que2.moveToNext()):
name_number_dict[que1.getString(que1.getColumnIndex(Contacts.DISPLAY_NAME))] = que2.getString(que2.getColumnIndex(Kinds.NUMBER) # Get datas and put in dic.
If you need to check all ColumnNames of query, use query1.getColumnNames() >> Referance
I hope it helps you guys who need to use this in Python>Kivy applications..
I am new to Android programming and having a problem in passing a filepath from my main activity to second activity.
Here is my code in Main activity to pass two variables walletDir(contains path) and password.
private File walletDir;
private final String password = "pass";
Intent intent= new Intent(Create_Wallet.this,Wallet_Address.class);
intent.putExtra("EXTRA_WALLET_DIR", walletDir);
intent.putExtra("EXTRA_PASSWORD", password);
startActivity(intent);
Here is my code for second activity
Intent intent = getIntent();
String walletDir = intent.getStringExtra("EXTRA_WALLET_DIR");
String password = intent.getStringExtra("EXTRA_PASSWORD");
Now password is being passed but not walletDir its null and the error i am getting is:
W/Bundle: Key EXTRA_WALLET_DIR expected String but value was a java.io.File. The default value <null> was returned.
private File walletDir;
walletDir is an instance of File.
intent.putExtra("EXTRA_WALLET_DIR", walletDir);
This puts an instance of File into your Intent as an extra. This works because File implements the Serializable interface, and there is a putExtra() method that takes a Serializable.
String walletDir = intent.getStringExtra("EXTRA_WALLET_DIR");
The extra is not a String. Yet, you call getStringExtra() to read it. This is not going to work, because a File is not a String.
Change that line to:
File walletDir = (File)intent.getSerializableExtra("EXTRA_WALLET_DIR");
(it is possible that you do not need the cast — it has been quite some time since I did this in Java)
You might also consider whether you really should be using two activities here. Android is moving very strongly towards having fewer activities, with fragments or composables representing individual screens displayed by those activities.
Im new to android so im gonna need a little help.
I created four activities in my app.
First activity is the MainActivity.
I assigned a ListView in the first Activity.
From the listView you are redirected to the second activity with a string which says which item you clicked. It doesn't matter which item you selected, but you will be redirected to that second activity only. Only diffrence is that the values of the strings passed are different.
I used this code in the ListView's onItemClick function:
String item = (String) listView.getAdapter().getItem(position);;
Intent i = new Intent(MainActivity.this, Activity2.class);
i.putExtra("item_selected", item);
startActivity(i);
This code redirects me to the second activiy with the string without any problem.
In the second Activity there are two options "Launch Type 1" and "Launch type 2" inside a radioButton group and a button to perform the function.
So i used this code inside the button's onClick method to determine which Activity to go next:
Intent intent = getIntent();
String item = intent.getExtras().getString("item");
RadioButton launch1 = (RadioButton) findViewById(R.id.launch1);
//The problem code:..
if(launch1.isChecked()){
if(item=="ListView_Item1"){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item=="Item 1"){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}
In Eclipse it shows that there are no errors in the code. But when I run it in the Emulator it starts off fine till it reaches the second activity.
When i click the button nothing is done and it does not redirect to any new page...
:(
Please help me in constructing the "if/else" statements in the button...and please do tell me if there are any better ways to achive the task...
Thanks in advance.
Waiting for replies....
In Java, you compare strings using equals() and not ==.
if(item.equals("ListView_Item1"))
Using == you're comparing references and not content.
Use Object#equals() for checking if an object contains same data as another one and == for comparing if two references are referencing the same object.
use equals() for comparison of String or Objects in java !
so your code should be :
if(launch1.isChecked()){
if(item.equals("ListView_Item1")){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item.equals("Item 1")){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}
I'm trying to pass an int between two classes using a bundle. I have looked online and it seems like it should be easy to do, its just not working for me :( any help is much appreciated! I am trying to pass the int life from one page to another in a simple game.
here is the code for the first page where I am passing it from
int life = 5;
....
if(input.equalsIgnoreCase(teststr))
{
Intent i = new Intent("com.name.PAGETWO");
i.putExtra("com.name.PAGETWO.life", life);
startActivity(i);
mpButtonClick.stop();
}
And on the second page, trying to receive the int
Intent i = new Intent("com.name.PAGETWO");
Bundle extras = i.getExtras();
int temp2 = extras.getInt("life");
int life = temp2;
when i try to run it, it tries to go on to the second page and then just says the application has unexpectedly stopped, but when i comment out the code it continues running as normal, so i know i am definitely putting in the code here wrong somewhere. thanks in advance!
Why would a brand new intent object contain the int you've passed? In the receiver activity, use getIntent() to retrieve the intent that the activity was started with. That intent would contain your extras.
You're creating a brand new Intent on the second page, which of course doesn't have the bundle. Instead, you need to retrieve the calling Intent using getIntent().
Here's how your second page's code could look:
int life = getIntent().getExtras().getInt("life");
EDIT: Looking at your code again, make sure the key name of the extra is consistent on both ends. To be sure, I usually use a final variable for this. So in your second activity (let's say it's ActivityTwo), declare this at the top (outside of onCreate()):
final static public String EXTRA_LIFE = "life";
Then in the calling activity:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra(ActivityTwo.EXTRA_LIFE, life);
startActivity(i);
(You may have also been constructing your Intent incorrectly; compare my example).
And in the second activity, inside of onCreate():
int life = getIntent().getIntExtra(EXTRA_LIFE, 0);
(You can replace 0 with what you want the default value to be if the extra doesn't exist.)
That way you don't have to worry about typos and can rely on Eclipse's suggestions to make sure you're consistent.