public void ShowMsg(){
Toast.makeText(getApplicationContext(),"No stock in this time.",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowMsg();
After click the button, show message is not shown before next executing.
You can try to add some delay if you calling method in onCreate
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override.
public void run() {.
// Do something after 1s = 1000ms.
ShowMsg();
}
}, 1000);
Related
I had write a code on trigger Barcode Scanner. I would like to trigger it after i clink on a button, but it doesn't work when i drop the code inside the onclicklistener. Need help on how to resolve this.
I had also include the button inside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_isbn = (TextView) findViewById(R.id.tv_display);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
camera();
}
});
}
```
I have a daily alarm and a settings page that allow users to change the timing that the daily alarm fires. I have a OnSharedPreferenceChangeListener in my SettingsFragment that extends PreferenceFragmentCompat and it is working here. However, when I do the same thing in my MainActivity, it does not seem to be working.
This is my code in Main Activity:
'''
public class MainActivity extends AppCompatActivity {
//some other variables
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//some other code
preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("daily alarm time")){
int alarmTime = sharedPreferences.getInt("daily alarm time", 36000000);
setdailyalarm(alarmTime, true, false);
System.out.println("onsharedpreferencechange activited");
}
if (key.equals("daily alarm toggle")){
Boolean dailyAlarmToggle = sharedPreferences.getBoolean(key, true);
System.out.println("111111111111111111111111");
if (dailyAlarmToggle){
int alarmTime = sharedPreferences.getInt(key, 36000000);
setdailyalarm(alarmTime, true, false);
}
else{
int alarmTime = sharedPreferences.getInt(key, 36000000);
setdailyalarm(alarmTime, true, true);
}
}
}
};
}
'''
This is my onpause and onresume:
'''
#Override
protected void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
}
#Override
protected void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(preferenceChangeListener);
}
'''
When I start my app and check my System.out, '''System.out.println("onsharedpreferencechange activited");''' does not seem to be firing which means my OnSharedPreferenceChangeListener is not working. I have seem the other discussions SharedPreferences.onSharedPreferenceChangeListener not being called consistently but it does not seem to be the solution to my problem.
When I start my app and check my System.out, '''System.out.println("onsharedpreferencechange activited");''' does not seem to be firing which means my OnSharedPreferenceChangeListener is not working.
It's because you're always unregistering the listener in your onPause(). So, whenever you leave the activity, the listener is removed and you can't listen for the SharedPreferences changes.
You could register the listener inside your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setup the listener
preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
...
// then register it
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(preferenceChangeListener);
}
Then unregister the listener when you close the activity by overriding the onDestroy() method:
#Override
public void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
super.onDestroy();
}
Or unregister the listener whenever you call finish() method. Either call the unregister part before finish() or overriding the finish() method.
I am doing an app that creates an ethereum wallet and send some ether when you touch the button register, it takes about 1 minute to do this, while it happens I want to show a message saying: Creating a wallet, wait please.
When I show the message it won't create the wallet or it will create the wallet but it won't show the message.
PS: If someone knows how to down the time to do this, it will help me a lot.
Thanks
thanks for the help... and the negative.
I have solved it anyway, I leave my solution below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ActivityMain);
errorMsg = new AlertDialog.Builder(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyAsyncTasks().execute();
}
});
}
class MyAsyncTasks extends AsyncTask<Void, Void, String> {
// ProgressDialog dialog;
#Override
//It will show a message during your background
protected void onPreExecute() {
dialog = new ProgressDialog(mainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Add title");
dialog.setMessage("Add message");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected void onPostExecute() {
dialog.dismiss(); // Close the Dialog
//Show a window with error or operation succesfully
if(hash.equals("Error")){
displayError("Error", "Error");
dialog.cancel();
}
else{
displayConfirmation("Operation Succesfully");
dialog.cancel();
}
}
#Override
protected String doInBackground(Void... voids) {
dialog.show();
// your code to do you background activity
}
}
I am developing a simple app. On one of my activity, there is button which calls the another activity. The another activity starts immediately, but I want to put some delay before opening the next activity after clicking the button.
Handler and Timer methods are not working. I have no idea, What to do???
public class thumb extends ActionBarActivity {
ImageButton show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thumb);
show=(ImageButton)findViewById(R.id.imageButton);
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),pop.class);
startActivityForResult(intent, 0);
}
});
}
}
Have you tried Thread.sleep(1000);? This will stop the execution by 1000 milliseconds.
I am developing a simple app in that my video is playing proper. but now I want to give "thank" message after finishing my video
How can I do this?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
final MediaPlayer player = MediaPlayer.create(MainActivity.this,R.raw.video);
Button button = (Button)findViewById(R.id.video_Play);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
player.start();
startActivity(new Intent("com.example.lenovo.play.app.VideoPlay"));
}
});
}
and Second VideoPlay.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
view = (VideoView)findViewById(R.id.videoView);
String url = "android.resource://"+getPackageName()+"/"+R.raw.video;
view.setVideoURI(Uri.parse(url));
view.start();
int duration = view.getDuration();
}
Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
#Override
public void onCompletion(MediaPlayer mp) {
//message
}