Send float from activity to activity - android

I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !
Here is my code from the main :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the StatsActivity.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_main);
Intent intent = getIntent();
float consumOn = intent.getFloatExtra("consumOn", 0);
float consumOff = intent.getFloatExtra("consumOff", 0);
EditText editTextA = (EditText)findViewById(R.id.editText1);
editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);
EditText editTextB = (EditText)findViewById(R.id.editText2);
editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
}
When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.

Replace the getFloatExtra with
Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");

I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
with
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
because you haven't actually defined their type anywhere and they aren't variables.

You should pass bundle with your floats to intent and only then extract them

I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_about:
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

Open Activity by clicking in the menu button- Android Studio

I am a beginner, I am a few days with this problem
I didn't find a solution. I have a menu that appears in an activity and when I click, I want it to open a new activity.
My question is, what to put in the activity with menu, and what to put in the new activity?
This is my code
Menu_chat.xml (my menu)
android:id="#+id/salva_vida"
android:icon="#drawable/salva_vida"
android:title="#string/save_life"
app:showAsAction="always" />
ChatActivity.java (this is the activity with menu)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
??????? (What put here?)------------------
break;
tab2.java (this is the new activity- I want to open this)
public class tab2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab2);
}
}
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
Your ChatActivity.java will look like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
//Start Activity here
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
break;
Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent);
#Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; case R.id.salva_vida: //Start Activity here Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent); break;
Don't forget to add your tab2 activity to manifest.

How to perfom an image clear_cache in android

I am getting and displaying images after doing Parse Queries and caching images. My problem is that when i click on the clear cache it doesn't delete the images and my pageviewer with i use to display them. It displays duplicates.I'm using Image Fetcher https://developer.android.com/samples/DisplayingBitmaps/src/com.example.android.displayingbitmaps/util/ImageFetcher.html from Caching Bitmaps from http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html saving files to the phone memory
private ImageFetcher mImageFetcher;
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.logout:
ParseUser.logOut();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish(); // logout code
return true;
case R.id.prefs:
Intent i = new Intent(this, SharedPrefsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
// intent = new Intent(this, SharedPrefsActivity.class);
// startActivity(intent);
// finish(); //logout code
return true;
case R.id.clear_cache:
mImageFetcher.clearCache();
Toast.makeText(this, R.string.clear_cache_complete_toast,
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}

Displaying error

I have encountered a problem. When I'm at this page (the main menu) and I try to click the calculate button it will go back to the login page. However, the calculate and records page are all also opened, and can be accessed by clicking the back button.
Button Calculate;
Button Records;
Button Logout;
Calculate = (Button) findViewById(R.id.buttonCalculate);
Records =(Button) findViewById(R.id.buttonRecords);
Logout = (Button) findViewById(R.id.buttonLogout);
Calculate.setOnClickListener(this);
Records.setOnClickListener(this);
Logout.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
public void onClick(View view)
{
Intent i = new Intent(this,Calculate.class);
startActivity(i);
Intent f = new Intent(this,Records.class);
startActivity(f);
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
}
}
Try this way:
public void onClick(View v)
{
switch(v.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
default:
break;
}
}
and make sure you have to added all the Activities in your manifest.xml file
try this..
public void onClick(View view)
{
Switch(view.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
}
}

Android action bar navigation

I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity

Android Buttons open up the wrong activities

I have three buttons on my main page. Something weird happens when I try to click on them. For example, when I click on the NewGame button, it first displays what the scores button should display, and then if I click the back button it will proceed to display the activity that it was meant to. With the About button, I have to click back twice (it displays the newGame activity and the scores activity. Is there a reason why this is happening?
public class Sakurame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//set up click listeners for buttons
View HighScoreButton = findViewById(R.id.highscore_button);
HighScoreButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any)
}
return false;
}
public void onClick(View v){
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
//break;
// more buttons go here (if any)
}
}
Try adding break; after each startActivity within the onClick method.
Edit to clarify. This ensures that once the case has been met, the switch statement is broken from instead of moving on to the next case statement.
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
break;
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
break;

Categories

Resources