Hi ^^
I have a problem developing my app.
I have a button on activity B which starts activity C. I have a textView on activity A. I would like to increase the activity A's textView by 1 with a counter every time the user touches my button in Activity B.
Can you help me?
P.s. Tell me if you need my codes
I would implement that by using a broadcast. Activity b sends a broadcast every time the button is pressed. Activity a must have a BroadcastReceiver object inside it. I think it should be like
BroadcastReceiver mReceive = new BroadcastReceiver(...) { @override
public void onReceive(...) {
//put your code to change the textview text here
}
};
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
---------- Post added at 06:50 PM ---------- Previous post was at 06:48 PM ----------
Ah or just let activity a implement broadcastreceiver
... extends Activity implements BroadcastReceiver
And then the onReceive void
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
Thank you so much.
I added those code lines (after a lot of researches because i'm a beginner):
ACTIVITY B (send broadcast):
Code:
public static String BROADCAST_ACTION = "com.example.appquiz.firstimagelogo.COUNTER";
sendBroadcast(new Intent(BROADCAST_ACTION), null); //inside the onCreate method under onClick
ACTIVITY A (receiver)
Code:
int counter;
BroadcastReceiver mReceive;
private BroadcastReceiver receiver;
TextView txView = (TextView)findViewById(R.id.counter1);
//inside onCreate:
counter=0;
mReceive = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
counter++;
txView.setText(counter + "/20");
}
};
//outside
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(FirstImageLogo.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
But i'm getting an error when trying to open Activity A
Ya android wants you to put the receiver somewhere else than in onCreate
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
Nothing to do.. it keeps crashing
I set it into the manifest and i put it out of onCreate method, but no way.
falloutjump said:
Nothing to do.. it keeps crashing
I set it into the manifest and i put it out of onCreate method, but no way.
Click to expand...
Click to collapse
Why don't you use the SharedPreferences to save that counter and whenever that button is touched, get it, increase it and save it again. Then in activity A just get the counter from the preferences again and set the text. I don't see why you'd want to use a BroadcastReceiver here. It's not that you need activity A to be notified of the changes, just when it is visible you set the correct value.
SimplicityApks said:
Why don't you use the SharedPreferences to save that counter and whenever that button is touched, get it, increase it and save it again. Then in activity A just get the counter from the preferences again and set the text. I don't see why you'd want to use a BroadcastReceiver here. It's not that you need activity A to be notified of the changes, just when it is visible you set the correct value.
Click to expand...
Click to collapse
THANK YOU ALL. I solved thanks to SharedPreferences. The app was crashing because of a mistake when importing the textview.
falloutjump said:
THANK YOU ALL. I solved thanks to SharedPreferences. The app was crashing because of a mistake when importing the textview.
Click to expand...
Click to collapse
Great, have fun!
Related
How do you update an activity that implements on click listener WITHOUT needing to click something? (e.g. with a timer)
What is it you are trying to do? Why the new thread?
I need to update button, background images and text based on a timer instead of only updating when something is clicked
I don't understand what you mean by "only updating when something is clicked."
the images used (e.g. back ground, or buttons) will not change (update) unless a button is clicked. So its basically waiting for something to be pressed (any button) before it updates any imgaes
So let me get this straight; you're changing the images associated with layout controls in a timer of some kind? What kind of timer implementation? Is the timer in its own thread?
yeah. I tried making another class for the time but it doesn't work for updating the view of the main activity that I want. So I am trying to use:
Timer timer2 = new Timer();
timer2.scheduleAtFixedRate(Title(), 1, 1000);
Title() is defined as: public TimerTask Title()
NEVERMIND!! I GOT IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to do
}
});
}};
t.schedule(scanTask, 1, 1000);
I had a class which i wanted to split into two for better readability and whenever i want to start a method from the second class the code gives NullPointerException.
So i ended up making another dummyclass and realized the problem is that either i dont know to to call from another class in android or am facing another issue.
In java id just make an object and call it but here NullPointerException and app crashes
Code:
public class active extends Activity {
public Button but;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void click()
{
but = (Button)findViewById(R.id.wifiSwitch);
but.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Rebooting...", Toast.LENGTH_LONG).show();
}
});
}
}
In the first class i do the following
Code:
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
rootReqSetting();
mActive = new active();
mActive.click();
return true;
}
the mActive = new active(); and mActive.click(); causes the nullexception.
Some help please ?
Well, first I request that you post logcats in the future.
Second: You call that:
Code:
but = (Button)findViewById(R.id.wifiSwitch);
but.setOnClickListener(new View.OnClickListener() {
What is the system doing? There is a new object whose onCreate method has never been called (just the constructor). For that reason the field but is null (equals "no value"). That's why it crashes.
Third: You do not do it that way:
mActive = new active();
Starting other Activities is done using Intents. Read this: http://www.vogella.com/articles/AndroidIntent/article.html
Where in the code did you inflate the wifi button ????
That is the cause of NullPointerException
Secondly,
You cant instantiate activities like this you must use
startActivity(Intent);
Sent from my GT-S5302 using Tapatalk 2
Thank you for you answer.
The Third part i understood that i should make an intent for the second class.
About the second part you said i didnt understand correctly , can you explain a little more ?
@sak-venom1997 what do you mean by inflate the button, didnt the whole menu get inflated when i called it in the first ?
Code:
getMenuInflater().inflate(R.menu.main, menu);
or there is something i should add?
And a new window will show when i start the intent without calling anything in it. i just want the button to do what it is supposed to do.
Tell me how the hell did you manage to put a Button in a MenuItem that code would just infate the menu Button would be infalted only if the parent layout is inflated
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
Tell me how the hell did you manage to put a Button in a MenuItem that code would just infate the menu Button would be infalted only if the parent layout is inflated
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
can you tell me then what am supposed to do, Am new to android and trying to learn.
You must call
setContentView(R.layout.layout_name);
Where is that button ure reffering
Sent from my GT-S5302 using Tapatalk 2
I just discovered how to use the action bar to have a OptionsMenu. I just solved a problem with it and I wanted to know if I did it the right way.
What I did
So, I added the menu to a fragment started by an activity, adding to it the setHasOptionsMenu(true) in the onCreate method and setting up the two convenience methods public void onCreateOptionsMenu() and public boolean onOptionsItemSelected() of course setting up the xml first.
The problem
Now, if I rotate my device in landscape mode the button in the action bar duplicates. If I put it in portrait mode again, the number of items stays the same (I'm sure it doesn't actually increase, checked the size in different ways) though.
What I thought and how I solved it
So, I was thinking about some fragment overlaps, but if they actually overlap then I shouldn't notice anything, right?
Is the menu being recreated, while not destroyed when the fragment is destroyed?
I solved the problem removing the item in the menu, in the Fragment.onDestroy() method manually.
Is the menu supposed to be cleared manually, or there is something wrong with my code (eg. fragments overlapping, etc..) ?
domenicop said:
Is the menu supposed to be cleared manually, or there is something wrong with my code (eg. fragments overlapping, etc..) ?
Click to expand...
Click to collapse
Could you please post your code? That would make helping much easier.
nikwen said:
Could you please post your code? That would make helping much easier.
Click to expand...
Click to collapse
Of course. Thanks for the interest.
This is the onCreate method of MainActivity.java, from where I handle the arrangement of fragments on the screen.
Also, place check my comment under the line "Log.d(TAG, "Orientation Portrait detected.");", where I try to explain why I handle fragments in that specific way. It's the way I understood it, but I'm not sure if that's correct.
Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize the mDatabase
mContactsDatabase = ContactsDatabase.getInstance(getApplicationContext());
// Determine device orientation
mDualPane = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
// Set the fragment that will be changed
mFragmentToReplace = mDualPane ? R.id.contactsPagerFragmentContainer : R.id.portraitFragmentContainer;
// Set up the GUI
FragmentManager fm = getSupportFragmentManager();
if (!mDualPane) {
Log.d(TAG, "Orientation Portrait detected.");
// First, look for previously saved fragments
// findByFragmentId(int id) look for the fragment that was previously associated
// to the resource that has for id the argument passed in. Then, we try to cast it
// to the type of fragment we want, and if that goes all right, we have our fragment.
// Else, null will be returned.
mContactListFragment = (ContactListFragment) fm.findFragmentById(R.id.portraitFragmentContainer);
if (mContactListFragment == null) {
mContactListFragment = ContactListFragment.newInstance(mContactsDatabase);
fm.beginTransaction()
.replace(R.id.portraitFragmentContainer, mContactListFragment)
.commit();
}
}
else {
Log.d(TAG, "Orientation Landscape detected.");
// First, look for previously saved fragments
mContactListFragment = (ContactListFragment) fm.findFragmentById(R.id.landscapeFragmentContainer);
mContactsPagerFragment = (ContactsPagerFragment) fm.findFragmentById(R.id.contactsPagerFragmentContainer);
if (mContactListFragment == null) {
mContactListFragment = ContactListFragment.newInstance(mContactsDatabase);
fm.beginTransaction()
.replace(R.id.contactListFragmentContainer, mContactListFragment)
.commit();
}
if (mContactsPagerFragment == null) {
final int FIRST_CONTACT_POSITION = 0;
mContactListFragment = ContactListFragment.newInstance(mContactsDatabase);
mContactsPagerFragment =
ContactsPagerFragment.newInstance(FIRST_CONTACT_POSITION, mContactsDatabase);
fm.beginTransaction()
.replace(R.id.contactsPagerFragmentContainer, mContactsPagerFragment)
.commit();
}
}
}
This are the relevant pieces of the ContactListFragment.java, that have something to do with the menu
Note that I changed the way I checked for double menu entries.
Now I check if (menu.size() != 1), because I have just one item in there.
If I remove that clause, there will be two item in the menu after rotating to landscape mode, and if I take the device back to portrait mode, the two items will remain two. That's so even if I change from landscape to portrait and vice versa a hundred times from now, the menu items will always be two.
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Bundle args = getArguments();
mDatabase = (ContactsDatabase) args.getSerializable(DATABASE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (menu.size() != 1) {
inflater.inflate(R.menu.contact_list_fragment_menu, menu);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addContactMenu:
mCallback.onAddContactMenuOptionSelected();
default:
return super.onOptionsItemSelected(item);
}
}
domenicop said:
I just discovered how to use the action bar to have a OptionsMenu. I just solved a problem with it and I wanted to know if I did it the right way.
What I did
So, I added the menu to a fragment started by an activity, adding to it the setHasOptionsMenu(true) in the onCreate method and setting up the two convenience methods public void onCreateOptionsMenu() and public boolean onOptionsItemSelected() of course setting up the xml first.
The problem
Now, if I rotate my device in landscape mode the button in the action bar duplicates. If I put it in portrait mode again, the number of items stays the same (I'm sure it doesn't actually increase, checked the size in different ways) though.
What I thought and how I solved it
So, I was thinking about some fragment overlaps, but if they actually overlap then I shouldn't notice anything, right?
Is the menu being recreated, while not destroyed when the fragment is destroyed?
I solved the problem removing the item in the menu, in the Fragment.onDestroy() method manually.
Is the menu supposed to be cleared manually, or there is something wrong with my code (eg. fragments overlapping, etc..) ?
Click to expand...
Click to collapse
I agree with nikwen but broadly the idea is...
- the menu belongs to the Activity not the Fragment.
- for an Activity for which you'd like an options menu you override onCreateOptionsMenu and do 2 things...
1/ call menu.add(...) for each menu item
2/ return 'true' at the end, so that your menu can be displayed
Also, see the Activity docs for onCreateOptionsMenu()
"This is only called once, the first time the options menu is displayed.
To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)."
If you want to add menu options to the Activity menu from within a Fragment you can also..
- override setHasOptionsMenu() in Fragment to return true
- override onCreateOptionsMenu() to add any items you like (just like you did at the Activity level)
- override onDestroyOptionsMenu()
The key thing here is that onCreateOptionsMenu() in Fragment is adding to the Activity menu.
It's hard to see without debugging exactly what's going on here but watch for calls to all of the above methods in Activity and Fragment and that will probably tell you what's going on.
If it is possible to control all menu options at the Activity level instead of the Fragment level, I would do that.
If not, and you need to use the Fragment menu methods, beware of Android's Fragment management (saving / restoring state)
Also, make sure your Fragments have an ID and/or tag.
(from the docs..)
"The fragment being instantiated must have some kind of unique identifier so that it can be re-associated with a previous instance if the parent activity needs to be destroyed and recreated. This can be provided these ways:
If nothing is explicitly supplied, the view ID of the container will be used.
android:tag can be used in <fragment> to provide a specific tag name for the fragment.
android:id can be used in <fragment> to provide a specific identifier for the fragment."
Finally, if all else fails, using Menu's findItem() before calling add() would be a safeguard, I suppose.
---------- Post added at 10:14 PM ---------- Previous post was at 09:52 PM ----------
domenicop said:
Of course. Thanks for the interest.
Click to expand...
Click to collapse
Sorry caught in the crossfire - have only just seen your code.
I think much of the above applies.
Set the menu up in the Activity instead of the Fragment, if you can.
I would also consider using layout-port and layout-land to control your two different layouts.
PicomatStudios said:
I agree with nikwen but broadly the idea is...
- the menu belongs to the Activity not the Fragment.
- for an Activity for which you'd like an options menu you override onCreateOptionsMenu and do 2 things...
1/ call menu.add(...) for each menu item
2/ return 'true' at the end, so that your menu can be displayed
Also, see the Activity docs for onCreateOptionsMenu()
"This is only called once, the first time the options menu is displayed.
To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)."
If you want to add menu options to the Activity menu from within a Fragment you can also..
- override setHasOptionsMenu() in Fragment to return true
- override onCreateOptionsMenu() to add any items you like (just like you did at the Activity level)
- override onDestroyOptionsMenu()
The key thing here is that onCreateOptionsMenu() in Fragment is adding to the Activity menu.
It's hard to see without debugging exactly what's going on here but watch for calls to all of the above methods in Activity and Fragment and that will probably tell you what's going on.
If it is possible to control all menu options at the Activity level instead of the Fragment level, I would do that.
If not, and you need to use the Fragment menu methods, beware of Android's Fragment management (saving / restoring state)
Also, make sure your Fragments have an ID and/or tag.
(from the docs..)
"The fragment being instantiated must have some kind of unique identifier so that it can be re-associated with a previous instance if the parent activity needs to be destroyed and recreated. This can be provided these ways:
If nothing is explicitly supplied, the view ID of the container will be used.
android:tag can be used in <fragment> to provide a specific tag name for the fragment.
android:id can be used in <fragment> to provide a specific identifier for the fragment."
Finally, if all else fails, using Menu's findItem() before calling add() would be a safeguard, I suppose.
---------- Post added at 10:14 PM ---------- Previous post was at 09:52 PM ----------
Sorry caught in the crossfire - have only just seen your code.
I think much of the above applies.
Set the menu up in the Activity instead of the Fragment, if you can.
I would also consider using layout-port and layout-land to control your two different layouts.
Click to expand...
Click to collapse
Wow. thanks for that detailed answer. :good: (Why have I used all my thanks?)
I've always put it into the Activity as well but it might make sense to add it to the Fragment sometimes. For example if you need the same Fragment in multiple Activities...
In that case you'd need to add a listener to the item in the Fragment method though.
@PicomatStudio Thank you for the detailed answer, I really appreciate that =)
Now I'm creating the menu in the activity and I don't have to worry about the fragments life cycle anymore.
The only problem is: I didn't get the menuInflater for free here, I guess it's because of the fact that in the Fragment scenario, I had to use the Activity inflater, whereas here in the activity, I can get my own, with getMenuInflater().
Here's the way I modified it, now is in MainActivity.java.
Please report back if you find something strange
Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (getSupportFragmentManager().findFragmentByTag(CONTACT_LIST_FRAGMENT) != null)
getMenuInflater().inflate(R.menu.contact_list_fragment_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addContactMenu:
onAddContactMenuOptionSelected();
default:
return super.onOptionsItemSelected(item);
}
}
domenicop said:
@PicomatStudio Thank you for the detailed answer, I really appreciate that =)
Now I'm creating the menu in the activity and I don't have to worry about the fragments life cycle anymore.
The only problem is: I didn't get the menuInflater for free here, I guess it's because of the fact that in the Fragment scenario, I had to use the Activity inflater, whereas here in the activity, I can get my own, with getMenuInflater().
Here's the way I modified it, now is in MainActivity.java.
Please report back if you find something strange
Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (getSupportFragmentManager().findFragmentByTag(CONTACT_LIST_FRAGMENT) != null)
getMenuInflater().inflate(R.menu.contact_list_fragment_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addContactMenu:
onAddContactMenuOptionSelected();
default:
return super.onOptionsItemSelected(item);
}
}
Click to expand...
Click to collapse
I don't tend to use MenuInflater (just menu.add()..)
But looking at the docs it takes a Context so could you do..
Code:
new MenuInflater(this);
.. from your Activity ?
On a general design point the less your Activity depends on its Fragments the better, I reckon.
domenicop said:
The only problem is: I didn't get the menuInflater for free here, I guess it's because of the fact that in the Fragment scenario, I had to use the Activity inflater, whereas here in the activity, I can get my own, with getMenuInflater().
Click to expand...
Click to collapse
Well, you can get one:
Code:
getActivity().getMenuInflater();
Hello,
Can anyone help me with code to make widget make an http request and display response in a text view on the widget.
Thanks
Nobody will code YOUR app for free here.
If you have googled and a specific question you can ask here.
EmptinessFiller said:
Nobody will code YOUR app for free here.
If you have googled and a specific question you can ask here.
Click to expand...
Click to collapse
Thank you. I have googled and and just been able to create my first widget. I however need to update the widget with data from a webservice via http request. I have got code to do this in a real app but can't seem to figure it out in a widget. My programming skills is very very low especially for java.
Any help will be appreciated. If I have to pay, how do Ii go about that please.
Thanks
EmptinessFiller said:
Nobody will code YOUR app for free here.
If you have googled and a specific question you can ask here.
Click to expand...
Click to collapse
This is what I came up with but the code won't run unless I take out the whole try/catch block. No errors are shown though.
Code:
public class LovelyBatteryWidget extends AppWidgetProvider {
//remote views object to access visible interface elements
private RemoteViews widgetViews = new RemoteViews("com.yourdomain.battery", R.layout.battery_widget);
/*
* Determine what happens when the widget updates
* - this method is called repeatedly
* - frequency determined by updatePeriodMillis in res/xml widget info file
*/
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
//register for the receiver when the battery changes
Intent received = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
//find out what action has been received
String receivedAction = received.getAction();
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
//String msisdn=manager.getSimSerialNumber();
//only carry out amendments if the action is a change in the battery level
if (receivedAction.equals(Intent.ACTION_BATTERY_CHANGED))
{
//get the level amount, pass default value
int level = received.getIntExtra("level", 0);
//indicate the level amount within the text view
this.widgetViews.setTextViewText(R.id.text_level, level+"%"+carrierName);
this.widgetViews.setTextViewText(R.id.text_balance, "4.44");
//this.widgetViews.setTextViewText(R.id.text_data, "1433");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("myURLGoesHere"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
int text=responseString.length();
this.widgetViews.setTextViewText(R.id.text_data, String.valueOf(text));
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (Exception e) {
return;
}
//get component to update
ComponentName appComponent = new ComponentName(context, LovelyBatteryWidget.class);
//update the widget
AppWidgetManager.getInstance(context).updateAppWidget(appComponent, this.widgetViews);
}
}
}
Battery level updates normally and so does balance when i comment out the whole http request part. However, all code including battery level and balance parts won't run when the http request part is included.
You should not try performing any IO operations on main (UI) thread.
I would build service with its own Looper object or using AsyncTask to perform HTTP request and sending back received data.
There is a guide on the internet, i think on android dev docs to make a word of the day widget that gets the word from wiktionary
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
---------- Post added at 09:41 AM ---------- Previous post was at 09:40 AM ----------
About the try catch block: with http response you need to cover IOException and TimeoutException and also a few more
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
Hello everyone, I am new to android app making.
I would like to make an app that works like a spining bottle, Onclick start spinning and the the arrow stops in a radom place after random time.
So my picture spins but on click it stops at the start position.
Please help thank you !
I would like to show you my code but i can't sorry
ed37 said:
Hello everyone, I am new to android app making.
I would like to make an app that works like a spining bottle, Onclick start spinning and the the arrow stops in a radom place after random time.
So my picture spins but on click it stops at the start position.
Please help thank you !
I would like to show you my code but i can't sorry
Click to expand...
Click to collapse
Not sure anyone can help you if you dont really ask a question.... what you have currently reads like
I want to do "abcdefghijklmnopqrstuvwxyz" can someone tell me how ?
You should really be asking how do I do "a" but having some idea and learned the basics yourself ...then maybe someone could be constructive in helping
Ok, well how to I get an image view to stop rotating after a random time like 2 to 15 secs?
ed37 said:
Ok, well how to I get an image view to stop rotating after a random time like 2 to 15 secs?
Click to expand...
Click to collapse
using a timer/thread , never actually done something like that but I suspect you can get the frame that is currently being played and work out the rotation, stop the animation and set that rotation.
Or maybe even rotate the thing yourself and do all of it in onDraw of a view or by manually drawing it on a canvas/surface
Use a canvas object, like that you get a drawn object you can completely personalize. Also the rotation angle
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
Here is what I have so far
Code:
public class SpinFragment extends Fragment {
public SpinFragment(){}
private Button btnRotate, btnstop;
private ImageView imgview;
AnimationListener listener;
Random rand = new Random();
int milis = (rand.nextInt(10) + 2)*1000;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_spin, container, false);
TextView txt = (TextView) rootView.findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/harabara.ttf");
txt.setTypeface(font);
final ImageView iv = (ImageView) rootView.findViewById(R.id.image_arrow);
btnRotate = (Button) rootView.findViewById(R.id.start);
btnstop = (Button) rootView.findViewById(R.id.stop);
final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.button_rotate);
rotation.setRepeatCount(Animation.INFINITE);
rotation.setAnimationListener(listener);
btnRotate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iv.startAnimation(rotation);
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
iv.clearAnimation();
}
}, milis);
}
});
return rootView;
}
}