How to Activate Button? - java

my context menu has two options Silly and Cool. I want to provide one item named activate to list item silly and second item named deactivate to list item cool.
How can i do that.??????
Help

1.If you want to start the second activity(and activate the settings) when Activate button is pressed you can use Intent to send a String or Integer(0 or 1) to know in the second activity that button was pressed.
You can find more information about sending data with Intent here
2.If you don't want to start the second activity when button is pressed but still activate the settings, you can make another class(Utils.java) where you can define all your settings(vibration, volume, ringtone) in methods and call them like that:
Utils mUtils = new Utils();
mUtils.activateVibration();
mUtils.playRingTone();
mUtils.setVolume(volumeLevel);
Hope it helps!

You can use intent.putExtra - put some information there, for example some object, String, whatever you want.
Then in your second activity just get those extras by
Bundle extras = getIntent().getExtras();
And for example if u have some boolean stored then you do:
boolean something = extras.getBoolean("keyToThatBoolean");

Related

pass variable from view to activity

I have been studying some code trying to learn more, the basic code is very simple. There is a xml layout file, and activity file and view file.
Eg.
simpleprog_layout.xml, SimpleProgView.java and SimpleProgActivity.java
I was looking at how a number of things are done and was trying to work out how a value could be passed from the view file for use in the activity file.
Can some one point me in the right direction?
Value being passed from View to Activity .
If it's to do with attaining reference of the View . It can be done using 2 Approaches
FindviewById()
ViewBinding
And if you'd wish to share a View's value to Another Activity , or to another view of Any other activity it can be achieved majorly in two ways .
Intents
Global Variables
Let's say for example there exists a String "name" and you wish to pass it to the ProfilePage of your application
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
intent.putExtra("user_name", username); // where Tag is user_name and the variable that holds value is username
startActivity(intent);
and the activity is able to receive the value using :
String userNameFromAnotherActivity= getIntent().getStringExtra("user_name"); // same Tag should be used
And with respect to global variable , any Activity is able to access the value with held in that variable.

My app skips starting an intent and executes the next code lines?

So I have the following currently:
public void addProject(View v) {
String pn = "";
Bundle extras = new Bundle();
Intent i = new Intent(mMainContext, AddProject.class);
mMainContext.startActivity(i, extras);
if(extras.getString("projectName") != null) {
pn = extras.getString("projectName");
}
}
This is just an example, the first bit is correct though the if statement may not be but this isn't about that block.
What I have noticed is that if I place breakpoints on the new intent, start activity and then the if statement,
Android seems to skip past the startActivity and go onto the if statement.
If I then remove the if statement and run it again, it then launches the intent.
So what I've realised is that it runs to the end of the addProject block and then actually launches the intent...
Why is this? And more importantly, how I do stop it doing that?
Intended outcome is as follows:
1) user presses "Add Project" button
2) intent launched and user inputs project name and presses submit
3) calling intent then receives the p name for use later in the function.
Why is this?
startActivity() is asynchronous. When it returns, the activity is not yet started.
how I do stop it doing that?
You can't.
Intended outcome is as follows: 1) user presses "Add Project" button 2) intent launched and user inputs project name and presses submit 3) calling intent then receives the p name for use later in the function.
Having separate activities may not be the best plan for this (compared with using fragments).
If you wish to use activities, the most natural approach is:
Use startActivityForResult() instead of startActivity()
Have AddProject use setResult() to create a result Intent to deliver back to your first activity
Have your first activity override onActivityResult(), where it can receive a copy of that result Intent and get the results from it
Note that this still will all happen after addProject() returns. However, you will (eventually) get your results.
The Android documentation does not cover this very well, but this is what it has on the subject.
you must use onActivityResult for such task.
From your main Activity use: startActivityForResults(intent) in place
of startActivity();
In your AddProject Activity when user inputs project name and press the
button .
Intent i = new Intent();
i.putExtra("projectName",userInputTextFromEditText);
setResults(RESULT_OK,i)
In your MainActivity
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data){
pn = intent.getExtras.getStringExtra("projectName","");
}
this way you will find your result

Reading string values under if/else commands

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);}
}

how to start a new activity in one page on clicking a button in other page

I have created an remote controller app in android. In the main page,there are few keys which on pressing sends a signal from the mobile. First of all it asks for a configuration file and parse the file and save the control options in a spinbox. When a particular key is pressed he corresponding control from the spinbox is selected and the signal is sent.
In next screen i would like to have only the keys which on pressed should select the control in the main screen and it should send the signal. In short i should be able to access all the elements in my main_screen.java.
In this you can access your keys in second screen by sending keys from first screen by click on button through this code
Intent in=new Intent(this,yournextActivity.class)
e.g:- my current class is hello.java and next class is Applet.java then through intent
Intent in=new Intent(hello.this,Applet.class)
to pass data to next class use this...
in.putExtras(key,value);
e.g:- my value is String s="Welcome" then i can pass this s to next class like this
in.putExtras("Yours",s);
key should be any text.....
on second class receive this String through this code
Intent in=getIntent();
String m=in.getStringExtras("Yours");
where m is receiving string and "Yours" is the key that you pass from first class...
if you want to pass data from one activity to another, you can do that with the help of intent
to pass data use putExtras()
Intent intent = new Intent(this, yourSecondActivity.class);
intent.putExtra("key", "Value");
startActivity(intent);
and to receive it in second activity use:
getIntent().getExtras().getString("key");
Note i am explaining above as assuming that i am passing data of string type ! so while receiving data, it depends on type of data you are receiving get that accordingly like i used geString("key");
you could pass things by using intents, but have you considered using fragments instead of multiple activities ?
it could even be better if you actually wish the app to work on large screens.

Android Home Shortcut loses flags after reboot

My Android app defines an activity with an intent filter of android.intent.action.CREATE_SHORTCUT, which lets me show up in the list of shortcuts that the user can add to their home page, when they select "Add Shortcut" from the menu or long-click the home page.
In this activity I have the following code (actually happens in a click event after they pick which shortcut to add):
Intent shortcutIntent = new Intent(this,MyActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.myicon);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppName");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
The shortcut works as I would expect, until I reboot the device. I'm actually testing on the emulator, not real device. The shortcut is still there after a reboot, so I know I didn't wipe user data or anything like that, but it acts like it no longer has the FLAG_ACTIVITY_NEW_TASK setting when clicked.
Example steps to recreate (assume my app is an email inbox for clarity):
Create the shortcut
Launch my inbox activity from my main menu acitvity, which uses the NEW_TASK flag.
From the inbox activity, click a message to open the view message activity.
Press HOME key
Click the shortcut -- at this point it brings the entire "task stack" back to the front, with the view message activity on top of the stack, clicking back goes back to the inbox activity, as I would expect
Reboot the device
Repeat steps 2 through 5.. now when I click the shortcut, instead of bringing the view message activity to the front, it brings the task to the front, but then adds a new inbox activity on top of the stack. So pressing BACK once goes back to the view message activity, and back again to the inbox activity.
I also tried setting different properties such as singleTask for my inbox activity in the app manifest, but haven't had any luck. Is this a known issue that flags are not saved with shortcuts?
I think I'll try adding a new stub activity that does nothing but launches the real activity with the NEW_TASK flag and then exits, and have my shortcuts point to that instead. However, seems like a lot of overhead, so hopefully someone has a better answer.

Categories