android onClick not firing? - java

EDIT: onClick is working properly now. The issue was that the button was trying to fire the onClick from the Parent class. now that is fixed. Of course that means a new issue is happening, that is the onActivityResult is never getting called.
So I am not really sure what the hell is going on, when I hit the button nothing happens, nothing in logcat, nothing, as if there is not code, but I am pretty sure this is written correctly, any thoughts?
public class myClass extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.store_selector);
Button getStore = (Button)findViewById(R.id.getStore);
getStore.setOnClickListener(buttonGetStoreOnClickListener);
}
Button.OnClickListener buttonGetStoreOnClickListener
= new Button.OnClickListener(){
public void onClick(View arg0) {
Intent intent = new Intent("com.blah.Blah.client.android.SCAN");
intent.setPackage("com.blah.Blah");
intent.putExtra("com.blah.Blah.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
};
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("debug tag", "contents: "+contents+" format: "+format);
Intent myIntent = new Intent(this, Ads.class);
myIntent.putExtra("key", contents);
startActivity(myIntent);
setContentView(R.layout.activity_ads);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
};

This is the class you need to import:
import android.view.View.OnClickListener;
And this is the method you should have:
OnClickListener onButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
};
Test it and let me know if it worked.
Regards

Are you sure you got the right button with Button getStore = (Button)findViewById(R.id.getStore); ?
If yes, then it can be something that sometimes happens to me sometimes.
When this happens, my logcat doesn't show anything. What I do to solve this, is to open the Devices view (Window, show view, other, Android, Devices) and select my device. Then when I look at the logcat again, everything's there.

Related

Selecting a Sound in an activity to be played in another activity

I have this idea that I couldn't code so I'm here Asking for help
I have two activities the first one :
Xml file : Button
Java File : a click listener for the Button to play a sound effect with the SoundPool class from res/raw
--all simple--
what want to do is to create a second activity where the user can choose an other sound effects like Sound1 or Sound2 ...etc from a radio button group, to be played instead.
this was my idea, so please help me coding this I'm stuck since 2 weeks and I have 0 clue whats the next step.
SOS =)
You could define a global variable for the sound effect to play:
int activeSoundEffectRawId = R.raw.defaultSound;
And play it with your SoundPool's load method.
To select the sound to play, you could add another button to your xml file that starts Activity2:
Button btnSelectSound = (Button) findViewById (R.id.button2);
btnSelectSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(new Intent(Activity1.this, Activity2.class), 1000);
}
});
It's important that you start the activity for result here with the request code 1000 (this number can be changed for sure).
In your Activity 2 you need the logic to select your sound and for example a "OK" button to save the selection. That ok button will hand over the selected sound to Activity1:
Button btnOk = (Button) findViewById (R.id.ok);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra("soundRawId", selectedSoundRawId /* <- replace this with the selected sound, like R.raw.yourSound */);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
After that, you can set the selected sound in Activity1:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == Activity.RESULT_OK) {
selectedSoundRawId = data.getIntExtra("soundRawId");
}
}

Serial scan using Zxing library - android

i'm developing a scan Barcode app on android , my application is simple and composed from an activity which contains a button and a textView which will receive the result of the scan.. The app works well but i want that i could realise serial scan in a raw. so after scanning a barcode i need that the capture Activity stay and the appli don't back to the button activity so i can scan the next Barcode. any solution please ?
this is my main java code :
public class MainActivity extends Activity {
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public boolean onTouchEvent(final MotionEvent event) {
IntentIntegrator integrator = new IntentIntegrator();
integrator.initiateScan(null);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(data.getStringExtra("SCAN_RESULT"));//this is the result
} else
if (resultCode == RESULT_CANCELED) {
// Handle cancel
} }
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
I had the same problem when I did my scanner activity, and the solution that I found was to make my mainActivity extends Zxing CaptureActivity, like this I overrided handleDecode and I avoided to switch between different activities (as you have to do to obtain your scanner result).
Anyhow, to restart the scanning process after a precedent scan I called the method
restartPreviewAfterDelay(0L)
(that is a method of CaptureActivity) in the onClick function of a button.
Take a look at that method, I think that it is what you need.
i finally found the solution i had just to add this code in the onActivityResult() declaration
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
So after the scan is finished the app is ready to scan again instead of going back to the home activity

Android back button freezes when using onkeydown method or overriding onbackbuttonpressed

public boolean onKeyDown(int keyCode, KeyEvent event){
if (isSub2&&keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(ctxx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
isReturning = true;
return false;
}
else {
return super.onKeyDown(keyCode, event);
}
}
}
There are two Activities Main--Sub2.
When you push a button in Main you can go to Sub2.
This code is in Sub2. I want to use back button on the bottom to make the MainActivity put on the top of stack not killing Sub2.
When I run it on the phone it works all right at first,
but after few more times of going back in Sub2 and going to Sub2 again
the back button stops working.
I don't know what is making the back button freeze.. any ideas?
ps) i've tried using handlers inside the method and overriding onBackButtonPressed() instead of using onKeyDown..
but no difference at all..
Not sure why your button freezes. It would be helpful to see what you are doing in the main activity. Here's an example that works for me and does not freeze:
You can put this in the main activity:
#Override
protected void onStart() {
super.onStart();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
//****** Uncomment the following line if you want to re-use the subactivity instead of launching a new one
//intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
}
And this in the sub-activity:
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}

having issues with starting an activity using Intent

I'm frustrated as to how I can get the function to work properly.
I've been researching and looking around about Intents.
At first I thought I got it right but I was wrong, some overview on what I mean.
I have made an app with 6 buttons, all of which that open different applications.
Clock, 2. Calendar, 3. Browser, 4. Messaging, 5. Phone, and 6. Contacts.
This is my onClick method for launching the contacts application.
// Contacts Launch
Button contacts_launch = (Button) findViewById(R.id.contacts_launch);
contacts_launch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent_contacts = new Intent(Intent.ACTION_MAIN);
intent_contacts.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent_contacts);
}
});
The onClick Intent method is the same for all my buttons, just the intent name has been changed according to the applications name, like messaging is intent_message.
When launching the application, and when I tapped the button. It prompted me with a window where I could select the application. And it ran the app every time I selected the button.
But when I select another application, it launches the contacts app? And doesn't let me choose it like before. How can I fix this? I'm pretty sure I'm using the intent function wrong.
Thanks for your time.
Please check code again, that was my modified one that didn't work which was the one with only one intent method. I added the code that I used at first where it let me choose. That's the one with the intent and category. (The one you can see now)
if you dont want to select the Activity over and over again (like when using createChooser) try this:
public class Chooser extends Activity implements OnClickListener {
private static final int NUM = 6;
private static final CharSequence DEFAULT = "click for select the app, long click to clear it";
private Intent[] mIntents = new Intent[NUM];
private LinearLayout mLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < NUM; i++) {
Button b = new Button(this);
b.setTag(i);
b.setText(DEFAULT);
b.setOnClickListener(this);
registerForContextMenu(b);
mLayout.addView(b);
}
setContentView(mLayout);
}
private CharSequence getName(Intent intent) {
PackageManager mgr = getPackageManager();
ResolveInfo info = mgr.resolveActivity(intent, 0);
if (info != null) {
return info.loadLabel(mgr);
}
return intent.getComponent().getShortClassName();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
int itemId = (Integer) v.getTag();
if (mIntents[itemId] != null) {
menu.add(Menu.NONE, itemId, Menu.NONE, "Clear");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
int i = item.getItemId();
Button b = (Button) mLayout.getChildAt(i);
b.setText(DEFAULT);
mIntents[i] = null;
return super.onContextItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Button b = (Button) mLayout.getChildAt(requestCode);
b.setText(getName(data));
mIntents[requestCode] = data;
startActivity(data);
}
}
#Override
public void onClick(View v) {
int i = (Integer) v.getTag();
if (mIntents[i] == null) {
Intent intent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent filter = new Intent(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra(Intent.EXTRA_INTENT, filter);
startActivityForResult(intent, i);
} else {
startActivity(mIntents[i]);
}
}
}
Hi Use the below Code to open contacts:
#SuppressWarnings("deprecation")
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivity(intent);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", urlToShare);
startActivity(sendIntent);
This is a sample code to open message application or hangout. You can do like this for others also.

Issue's containing, "onClick" and a getIntExtra

I am having issues getting this to work, here is what I am trying to get to: an onClickListener that will get the state of the radio and return a toast. But I am messing up somewhere. I am having an issue with the intent, anyone able to shed some light on this?the intent is not able to be resolved. (Yes, I obviously have imported it.) Here is what I have:
public class MainActivity extends Activity {
protected Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button check = (Button)findViewById(R.id.Button01);
check.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){
getIntent();
int state= intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);
String msg=null;
switch (state) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
You don't store the value returned by getIntent(). This is better :
public void onClick(View v){
intent = getIntent();
int state= intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);
Don't forget to call show()
manual
example
Toast toast = Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT);
toast.show();
Another thing is indeed in this line
int state=intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);
What is "intent" here? Is it declared somewhere in the class?
You say in the comment that you're getting a forceclose in this line. Is it a nullpointer? Do you need to call getIntent() somewhere maybe? Is that 'intent' initialized?

Categories

Resources