Basic onCreate method question - Android Software Development

How would I go about saving the state of the rating of the stars when I click the button? I need it to save how many stars it has up when I reopen the app. Here is what I have so far...
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class HelloAndroid extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Toast.makeText(HelloAndroid.this, "State saved", Toast.LENGTH_SHORT).show();
}});
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(HelloAndroid.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
}
});
}
}

I would suggest http://developer.android.com/guide/topics/data/data-storage.html. I haven't used it yet but it should work for what you are doing.

Related

[Q] Playing new sound on each click

I've run into a little trouble and I'm wondering if someone can help me out.
I have a button with several sound effects that go with it. I'm wanting to play a different sound each time the button is pressed, but I just can't get things to work out. Can someone show me the way? Any help would be huge. This is the only thing keeping me from finishing. Here's a little code:
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.finn_wordtoyourmother);
Button SoundButton = (Button)findViewById(R.id.Button1);
SoundButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
});
}
}
If you want to play a different sound with each click, then you'll have to keep a global counter variable or global string that you change to the next audio file name with each click of the button--sort of a circular buffer. The code you have there tries to play both sounds on the same click and likely mixes them together at the same time if "playSound()" is asynchronous, but I don't know what the SoundManager class is so I have no idea.
Gene Poole said:
If you want to play a different sound with each click, then you'll have to keep a global counter variable or global string that you change to the next audio file name with each click of the button--sort of a circular buffer. The code you have there tries to play both sounds on the same click and likely mixes them together at the same time if "playSound()" is asynchronous, but I don't know what the SoundManager class is so I have no idea.
Click to expand...
Click to collapse
Okay, so I understand what you mean, but I don't know how to say that in Java. Could you give a short example? As for SoundManager, it's really a useless class I have that I was using to play my media. You can take a look here:
Code:
package com.andrew.finnandjake;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
OK, I see it uses SoundPool. First, change your definition for addSound to:
Code:
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put([COLOR="Red"]Index[/COLOR], mSoundPool.load(mContext, SoundID, 1));
}
Here's code that should work to do a different tone each push and loop back to the beginning.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonSound extends Activity{
private SoundManager mSoundManager;
private int next=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager=new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0,R.raw.freeze);
mSoundManager.addSound(1,R.raw.ascend);
mSoundManager.addSound(2,R.raw.bubble);
mSoundManager.addSound(3,R.raw.chiff);
Button SoundButton=(Button)findViewById(R.id.button1);
SoundButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mSoundManager.playSound(next);
next++;
if(next>3)
next=0;
}
});
}
}
Gene Poole said:
OK, I see it uses SoundPool. First, change your definition for addSound to:
Code:
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put([COLOR="Red"]Index[/COLOR], mSoundPool.load(mContext, SoundID, 1));
}
Here's code that should work to do a different tone each push and loop back to the beginning.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonSound extends Activity{
private SoundManager mSoundManager;
private int next=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager=new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0,R.raw.freeze);
mSoundManager.addSound(1,R.raw.ascend);
mSoundManager.addSound(2,R.raw.bubble);
mSoundManager.addSound(3,R.raw.chiff);
Button SoundButton=(Button)findViewById(R.id.button1);
SoundButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mSoundManager.playSound(next);
next++;
if(next>3)
next=0;
}
});
}
}
Click to expand...
Click to collapse
Mkay, so I adjusted my code according to the example and everything came back error free; however, when I run the app and click the button, nothing happens. No sound, but no force close either.
I ran the Debugger and it's telling me that the SoundPool sample is not ready. "Sample 1 not READY", "Sample 2 not READY", etc.
I don't know. I assume you used different names for your sound files. I just grabbed those out of the "notifications" directory of my phone.
Gene Poole said:
I don't know. I assume you used different names for your sound files. I just grabbed those out of the "notifications" directory of my phone.
Click to expand...
Click to collapse
Alright, that's cool. Thanks a lot for all of the help. I owe you one.

How can I call a number from button press in Android?

hello I need help i need it to when the user clicks a button in my app is calls a specific phone number when I try doing it on my phone i click the button and it says app has crashed
Please help heres my class file:
Code:
package com.d4a.kingdomapp;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.net.Uri;
public class ComputerIssues extends Activity {
String number = "573-642-2800";
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.computerissues);
// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button callfulton =(Button)findViewById(R.id.fultonoffice);
callfulton.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {
// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {
// Now, which button did they press, and take me to that class/activity
case R.id.fultonoffice: //<<---- notice end line with colon, not a semicolon
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);
}
}
};
}
Thanks in advance
Regards
Rapsong11
Have you declared the permission in your menifest?
Add this permission
Yes I have
Thanks for the reply
Sent from my Nexus 4 using xda app-developers app

Webview Fragments and back button

I am having trouble using the back button to go back in webview. I have 5 different fragments in a tab + swipe layout. Each having a different webview. I have my MainActivity.Java as well as a java file for each fragment. Using the examples shown in stackoverflow regarding this (can't link. Sorry) I get a force close whenever I press back on the app. So I received some helped and did a non-conventional stab at it but I get a couple of errors so I'm hoping one of you guys can help me out. I am super new to app development and java in general so please bear with me.
Here is my MainActivity.java where the errors are:
Code:
package com.-------;
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.webkit.WebViewFragment;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
[user=439709]@override[/user]
public void onBackPressed(){
mSectionsPagerAdapter.getLastFragment().tryGoBack(); [B]<-Error #1[/B]
}
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private WebViewFragment lastFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public WebViewFragment getLastFragment() {
return lastFragment;
}
[user=439709]@override[/user]
public Fragment getItem(int position) {
WebViewFragment Fragment;
switch (position) {
case 0:
Fragment = new fragment1();
break;
case 1:
Fragment = new fragment2();
break;
case 2:
Fragment = new fragment3();
break;
case 3:
Fragment = new fragment4();
break;
case 4:
Fragment = new fragment5();
break;
default:
Fragment = null;
break;
}
lastFragment = Fragment;
return Fragment; [B]<-Error #2[/B]
}
[user=439709]@override[/user]
public int getCount() {
return 5;
}
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
case 2:
return getString(R.string.tab3).toUpperCase(l);
case 3:
return getString(R.string.tab4).toUpperCase(l);
case 4:
return getString(R.string.tab5).toUpperCase(l);
}
return null;
}
}
}
Error #1 is in line 26 and says:
Code:
The method tryGoBack() is undefined for the type WebViewFragment
Error #2 is in line 80 and says
Code:
Type mismatch: cannot convert from WebViewFragment to Fragment
Here is one of my fragment java files as well:
fragment1.java
Code:
package com.-----;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebViewFragment;
public class fragment1 extends WebViewFragment {
public abstract class WebViewFragment extends Fragment{
public abstract void tryGoBack();
}
WebView myWebView;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment1, container, false);
myWebView = (WebView) root.findViewById(R.id.webview1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("url");
return root;
}
}
Any help is greatly appreciated!
When dealing with two or different classes which all extend one of them, so in this case the WebViewFragment extending Fragment and your fragment1 inheriting from WebViewFragment, you can convert the object simply putting
Code:
(NewType)
in front of it. So for error 1, insert (Fragment1) and for error 2 insert (Fragment).
One advice I'd like to give you, Capitalize class names and don't capitalize object and variable names, so it should be Fragment1 (maybe a more clear name) and WebViewFragment fragment.

camera app help..

Hello,
I am newbie to android programming.I am trying to make a simple app.The app window has 1 button and 1 ImageView.The button will capture the image and will set ImageView to it.So i wrote a little bit of the code and when I try to run it on emulator,after clicking the capture button I get an error saying application stopped.Please help me!
package com.example.file;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
public class MainActivity extends Activity {
Bitmap mImageBitmap;
Button Capture;
ImageView mImageView;
int actioncode=0;
Intent i;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Capture=(Button)findViewById(R.id.c1);
Capture.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
dispatchTakePictureIntent(actioncode);
handleSmallCameraPhoto(i);
}
});
mImageView=(ImageView)findViewById(R.id.i);
}
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(mImageBitmap);
}
}
Please use code-tags!!
Plz read your **LogCat**.
Regards
bhushan.crysis said:
Hello,
I am newbie to android programming.I am trying to make a simple app.The app window has 1 button and 1 ImageView.The button will capture the image and will set ImageView to it.So i wrote a little bit of the code and when I try to run it on emulator,after clicking the capture button I get an error saying application stopped.Please help me!
Click to expand...
Click to collapse
Looking at your code, you will get a NullPointerException on calling intent.getExtras() in the last method since you're calling it in the Button click. You should instead override the onActivityResult check request and Result code and call that method there
But in general, make sure to read This awesome guide on debugging, that way you can solve your problems yourself!

Listview shuts down after discovering devices

Cheers fella's
For a bluetooth remote project I wanted to add some extra tools to increase the ease of use.
At this moment the application is able to turn on and off bluetooth, show a list of paired devices but.. I'd also like to inplement a listview with new unpaired devices.
The thing is, I've got it working.. it seems to logg the new devices but I think there is a catch with my Listview.
after pressing the button to scan for devices the application discovers my bluetooth device but here's what happens after.
A new device is discovered, the listview displays it's name twice underneath eachother, and within a second the activity just shuts off. After trying it twice of thrice the whole application shuts off.
sadly I can't check my logger because after the error there is so much happening that the logg is too short and the error gets deleted.
here's the code for the discovering new devices
```package com.example.bluetest;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Discoverdevice extends AppCompatActivity {
BluetoothAdapter myBluetoothAdapter;
ArrayAdapter<String> arrayAdapter2;
ArrayList arrayList2;
ListView lvnewdevice;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discoverdevice);
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
lvnewdevice = (ListView)findViewById(R.id.lvnewdevice);
arrayList2 = new ArrayList();
arrayAdapter2 = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList2);
lvnewdevice.setAdapter(arrayAdapter2);
}
public void discoverDevices (View view)
{myBluetoothAdapter.startDiscovery();}
BroadcastReceiver nreceiver = new BroadcastReceiver() {
@override
public void onReceive(Context context, Intent intent) {
String nDaction = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(nDaction)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
arrayList2.add(device.getName());
arrayAdapter2.notifyDataSetChanged();
}
}
};
@override
protected void onStart() {
super.onStart();
IntentFilter newdevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(nreceiver, newdevice);
}
}

Categories

Resources