Launch intent with some sound file - java

I am making an Android application containing more than 60 buttons. Each button responds to an Activity. Each Activity contains a sound file sourced from a raw file, a TextView and an image. Is there any way that I can use Intent parameters for each button?
For example:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile", "FirstKeyValue");
myIntent.putExtra("image", "SecondKeyValue");
myIntent.putExtra("text", "ThirdKeyValue");
startActivity(myIntent);

//Make method let suppose
public void sendMysong(String songname,String imgUrl,String text)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",songname);
myIntent.putExtra("image",imgUrl);
myIntent.putExtra("text",text);
}
// Now in receiving Activity receive intent data
Bundle extras = getIntent().getExtras();
if (extras != null) {
String fname=extras.get("soundfile");
int resID=getResources().getIdentifier(fname, "raw", getApplicationContext().getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}

Sixty button in single layout is not a good design pattern, you must use either grid or List view.
Using List View
http://developer.android.com/design/building-blocks/lists.html
Using Grid View
http://developer.android.com/guide/topics/ui/layout/gridview.html
As far as the current problem is concerned
You can create a generalized Function as given below
public boolean startActivityFoo(String value1,String value2,String value3)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",value1);
myIntent.putExtra("image",value2);
myIntent.putExtra("text",value3);
startActivity(myIntent);
}

Related

How can I transfer data new intent

my Onbindview holder
Glide.with(holder.t1.getContext())
.load("http://example/example/images/" +data.get(position).getImage()).into(holder.img);
}
And my interface
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (iOtobusSaatleriInterface != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION);
iOtobusSaatleriInterface.onItemClick(position);
Intent intent = new Intent(context,deneme.class);
intent.putExtra("name",data.get(position).getName());
intent.putExtra("resim", String.valueOf(Glide.with(itemView)
.load("http://example/example/images/")));
context.startActivity(intent);
}
}
});
my new activity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int goruntu = intent.getIntExtra("resim",0);
imageView.setImageResource(goruntu);
textView.setText(name);
finally my photo is not coming. I just can't get the image data in the new activity. This data is registered in mysql and I am pulling from the directory with retrofit.
New display
my imageview display
And my xml
You can pass the image as a string from screen A to screen B.
The batter way is to pass full URL in intent as String and receive it on another Activity.
Intent intent = new Intent(context,deneme.class);
intent.putExtra("name",data.get(position).getName());
intent.putExtra("resim","YOUR_FULL_URL");
At Another Activity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String goruntu = intent.getStringExtra("resim");
String.valueOf(Glide.with(imageView)
.load(goruntu)
textView.setText(name);
Without knowing more about why you are trying to send a raster image over IPC between activities, I think the best advice I can give you is to not try to do that.
Instead, simply send the URL in the intent and have Activity 2 use glide to load and display the image.

App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?

Show text on other activity than the button clicked

I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

Intent betwen 2 applications

I ask if I can pass to an other application some data using intent. If it's possible, how can I do clicking a button and passing to an other application?
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intention = new Intent(?????);
startActivity(intention);
}
});
To pass data from one activity to another you need to add it to the Intent. E.g,
Intent intention = new Intent(this, DestClass.class);
int value = 10;
intention.putExtra("KEY", value);
startActivity(intention);
and in your DestClass's onCreate(), you get it from the Intent with,
Bundle extras = getIntent().getExtras();
if (extras!= null) {
extras.getInt("KEY");
}
To send it to another application. Similarly you create an Intent as shown in the Android docs.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Note that in this case the Android system allows apps to register to receive Intents and if there are multiple apps that have registered for these Intents then the system lets the user choose which App they would like to handle the Intent.

How to clear an activity from the second activity going back to home screen in android?

example scenario is:
from login screen - main screen - then when i clicked a hide button the app will go to home screen, and when im going to click the app again the main screen would be called
Fire an intent when you want to display the home screen
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
So this will be fired on the pressing of your hide button
I think you can use you can use FLAG_ACTIVITY_CLEAR_TOP
FirstActivity is the first activity in the application:
public static void home(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(intent);
}
}
And If you want to exit from the whole application,this help you in that.
public static void clearAndExit(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putBoolean("exit", true);
intent.putExtras(bundle);
ctx.startActivity(intent);
} else {
((Activity) ctx).finish();
}
}
i Really Hope this helps.

Categories

Resources