I hope this post is ok at this place.
I have an activity which implements both View.OnTouchListener and TextWatcher.
Adding the TextWatcher to a widget like an EditText via addTextChangedListener() works fine. eg. afterTextChanged() fires.
But wenn i´m adding a touch listener via setOnTouchListener() to the same widget only onTouch() fires.
I debugged it, but the App does not stop at any breakpoints of TextWatcher event handler.
It appears like the touch listener kicks the textChanged listener. Or what could be the issus?
sbaumg2s said:
I hope this post is ok at this place.
I have an activity which implements both View.OnTouchListener and TextWatcher.
Adding the TextWatcher to a widget like an EditText via addTextChangedListener() works fine. eg. afterTextChanged() fires.
But wenn i´m adding a touch listener via setOnTouchListener() to the same widget only onTouch() fires.
I debugged it, but the App does not stop at any breakpoints of TextWatcher event handler.
It appears like the touch listener kicks the textChanged listener. Or what could be the issus?
Click to expand...
Click to collapse
You have to make sure that you aren't "consuming" the touch event in your onTouchListener. That means you should return false from the onTouch method. See here for reference.
Related
I'm developing a simple android application using the TabActivity.
This Activity adds 3 tabs, each one with its own activity.
The problem is: when I tap each tab, the system calls onDestroy of the current activity. So, when I tap the first activity again, all the state of private fields are gone, its like it has created a new instance every time I change tabs.
I was searching around about this, and looking on the samples, but I guess that the default behavior is to keep the activity instances paused when the tab is not current.
Anyone have an idea of what I'm missing?
Thanks.
That is by design. You need to override onSaveInstanceState() and onRestoreInstanceState() in your activity class. You can store/retrieve state data there, or retrieve it int the Bundle passed into OnCreate(). See:
http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)
My limited programming experience does not reach into the realm of android, as evidenced by the title of this thread. So I figured I'd ask for help.
Here is what I'm trying to do: Tie and activity to the Simi Clock launcher.
Unfortunately, it only lists apps in the launcher options, so I was wondering if there is an easy way to make an app out of an activity.
Any help is greatly appreciated.
Do you mean how do you make an app with an activity at all, or how to use it with the clock you mentioned?
A new app can be easily created using the ADT Plugin for Eclipse, a great thing to have. From there all you have to do is make a new class that extends Activity class.
You can use intents to run portions of other apps, but I am not sure if I quite understand what you mean.
roflha said:
Do you mean how do you make an app with an activity at all, or how to use it with the clock you mentioned?
A new app can be easily created using the ADT Plugin for Eclipse, a great thing to have. From there all you have to do is make a new class that extends Activity class.
You can use intents to run portions of other apps, but I am not sure if I quite understand what you mean.
Click to expand...
Click to collapse
Hehe, and I'm not sure I understand what you mean, but that's because I know diddly-squat about this stuff.
Let me be more descriptive:
- Let's say I have a widget, for example Beautiful clock, which allows me to to launch an app upon clicking on the clock numbers.
- I also have a launcher which allows me to make shortcuts directly to activities, for example wifi toggle. That acts like an app or widget if placed on a home screen.
- Now, I would like to be able to invoke that "wifi toggle" or other activity by clicking on the clock widget numbers. However, my clock widget only lists apps in "launch-able" options. Hence the dilemma.
Did that make any sense?
Yeah, I think I see what you're saying.
Well one thing worth noting is that the clock widget is most likely not pulling its list of possible applications based off of the ones you have links to or the ones in your drawer. What I am guessing is it using something called an Intent that allows it to filter for all of the apps that have a launch-able intent Your shortcut most likely doesn't have that.
What you could try doing is, since you were asking about Activities, is create a simple app that, when run, redirects you to the WiFi settings. All the code would be for such an Activity is
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(intent);
}
Like the attached apk... lol. Install it and try to map weather to it.
roflha said:
Yeah, I think I see what you're saying.
Well one thing worth noting is that the clock widget is most likely not pulling its list of possible applications based off of the ones you have links to or the ones in your drawer. What I am guessing is it using something called an Intent that allows it to filter for all of the apps that have a launch-able intent Your shortcut most likely doesn't have that.
What you could try doing is, since you were asking about Activities, is create a simple app that, when run, redirects you to the WiFi settings. All the code would be for such an Activity is
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(intent);
}
Like the attached apk... lol. Install it and try to map weather to it.
Click to expand...
Click to collapse
Thanks for the help. I love this place!
I saw a lot of ppl having this problem and it's something SO common to do, and the solutions I saw were ugly to the point that I bet that Google's solution for it isn't any of those.
Simple scenario, I have a few dialogues in my application being setup inside:
Code:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_LOAD: return new ProgressDialog... etc.. etc..
}
}
and called with
Code:
showDialog(DIALOG_LOAD);
and dismissed with
Code:
dismissDialog(DIALOG_LOAD);
all inside the activity class.
This approach to dialogues I believe is the 'cleanest' way of doing it and just let the Activity manages the dialogues.
it works fines IF I don't rotate the device, I though because the activity was managing it, it would be all inclusive, but no, life is never that simple. If I rotate the phone when the dialogue is up, the rotation occurs, the dialogue is still visible but the moment the code try to dismiss it it gives a nice:
05-26 20:07:02.951: ERROR/AndroidRuntime(10041): java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
so...
do I have to care about dismissing and re-creating the dialogue onPause and onResume or is there anything I can do to make the activity take care of it by itself?
I have multiple widgets provided by one app, and thus multiple classes that extend AppWidgetProvider. The idea is that every widget contains a Button, which starts a Service when pressed. This Service runs a bit of code and then stops. Now for the problem: for example, I have a Widget1a and a Widget1u. These widgets look like each other, but each widget does a slightly different thing. If I place a Widget1a on the homescreen and press it, a Service is started and the code that this Service should run is run, as it should be. Now I place a Widget1u on the homescreen, and suddenly Widget1a stops working and doesn't do anything when I press it. If I place a second Widget1a on the homescreen both Widget1as work again, but now Widget1u is disabled and doesn't respond when I press it. In short: it seems that at any particular time only one type's code works when it is pressed (and if there are multiple instances of the same type on the homescreen all of these instances will work, but no instances of any other widget of my app will do anything when pressed).
The code in the AppWidgetProvider of each widget is pretty basic. It merely contains an overrided onUpdate-method. It creates an Intent to start a Service that I previously created, this is then used to create a PendingIntent which is used by setOnClickPendingIntent to make sure it can be activated by a Button in the widget. Each widget makes, when its Button is pressed, a slightly different Intent so that a certain Service is executed slightly differently. So this only works for the instances of 1 widgettype at a time, namely those of which an instance was added to the homescreen most recently.
Why do all my app's other widgets stop working when I add a widget of a different type to my homescreen? Does anyone know more about widgets or how I can make sure that each widget keeps doing its job when I add a new one from the same app to the homescreen?
Here is the onUpdate method of one of my AppWidgetProviders for one of my widgets. All widgets operate similarly and I know I used a lot of copypasting which is a bad programming habit but it didn't seem to really want to work otherwise.
Code:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Create an intent to launch the service
Intent serviceIntent = new Intent(context, SendService.class);
//I just put some extra data here to be used by the Service which wil eventually get this Intent and thus the data inside
serviceIntent.setData(Uri.parse("uri::somethingrandomandunique"));
serviceIntent.putExtra("Lamp", "1a");
// PendingIntent is required for the onClickPendingIntent that actually
// starts the service from a button click
PendingIntent pendingServiceIntent =
PendingIntent.getService(context, 0, serviceIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
// Get the layout for the App Widget and attach a click listener to the
// button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1a);
views.setOnClickPendingIntent(R.id.button1a, pendingServiceIntent);
// super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName componentName = new ComponentName(context.getPackageName(), Widget1a.class.getName());
appWidgetManager.updateAppWidget(componentName, views);
}
So what am I doing wrong?
Could someone help me add a m player and a setonclicklistener in a fragment. I have a couple of sound bite apps and I am trying to put them together in a Viewpager. I have the layout all set up, but im not sure on how to have the m player to the fragment. Thanks.
Sent from my SCH-I535 using Tapatalk 2
Set an onClickListener by using the setOnClickListener() method in the onCreateView() method.
If you set the onClick attribute in the xml, it will look for the method in your Activity.
The MediaPlayer requires a Context, right? Pass your Activity which you can get by calling getActivity().
u can use fragmentpagerapteradapter
to assign fragment as viewpager child...