Splash screen depends onClick to move on - java

Currently, my splash screen directly link to my mainActivity. The splash screen ends when the rotate duration finish. android:duration="8000"
Then I have alert dialog at the end there.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.animator.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
new AlertDialog.Builder(this).setTitle("Promo Code").setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U").setNeutralButton("Close", null).show();
}
`
So, is there any way to like end the splash screen after I click on close ?
This may really sounds stupid cause I'm really new in android

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(yoursplashscreen.this);
dialogBuilder.setTitle("Promo Code");
dialogBuilder.setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U");
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// finish your splash screen here and set intent to your new activity
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

Related

How to use mediaplayer and update seekbar in a function that contain custom dialog in android studio?

I want to use mediaplayer in a function that provide dialog with positive and negative answer. when positive answer selected one audio will played, and when negative selected another activity will appear.
I do these and my audio play successfully, but my seekbar couldn't update.
my function name is autoPlayDialog that code of call is this:
else {
if(MainActivity.getBadgeCount() == 0 ){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
}, 3000);
} else {
autoPlayDialog();
//Intent i = new Intent(Splash.this, MainActivity.class);
//startActivity(i);
}
}
and then my function code is:
private void autoPlayDialog()
{
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song2);
final SeekBar seekBar;
final Runnable runnable;
final Handler handler;
final ImageView playIcon;
seekBar = findViewById(R.id.autoseekbar);
playIcon = findViewById(R.id.autopauseplay);
handler = new Handler();
View view = LayoutInflater.from(Splash.this).inflate(R.layout.activity_auto_play,null);
final AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setTitle("new message")
.setMessage("Do you want to play?")
.setPositiveButton("Yes", null) //Set to null. We override the onclick
.setNegativeButton("No", null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.start();
playIcon.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
changeSeekbar();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playIcon.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
dialog.dismiss();
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
});
}
});
Button button2 = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
});
}
});
dialog.show();
}
My problem is that this line of code seekBar.setMax(mediaPlayer.getDuration()) make error and my app closed. autoPlayDialog function is developed in Splash activity and seekbar assigned to another xml file seekbar with this line of code seekBar = findViewById(R.id.autoseekbar) that this xml file assigned to view of my dialog: View view = LayoutInflater.from(Splash.this).inflate(R.layout.activity_auto_play,null);
Also about playIcon image i have this problem too.
Thank you for your attention :)

How to make a button listen again after click?

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dpText = (TextView) findViewById(R.id.dpText);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
updateQuestion(random.nextInt(mdata.questions.length));
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(button1.getText().equals(correctAnswer)){
updateQuestion(random.nextInt(mdata.questions.length));
} else{
gameOver();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(button2.getText().equals(correctAnswer)){
updateQuestion(random.nextInt(mdata.questions.length));
} else{
gameOver();
}
}
});
I was making a quiz app. I created a method, updateQuestion(), which takes a random integer to show questions from array. I am facing problem that when a user clicks the button, it works fine for first time. When Second question pops up, button doesn't listen for answer. On repeated clicks, button work sometimes. Please help, sorry for poor english
update question code -
public void updateQuestion(int num){
dpText.setText(mdata.getQues(num));
button1.setText(mdata.getAlia(num));
button2.setText(mdata.getPari(num));
correctAnswer = mdata.getAnswer(num);
}
game Over code -
private void gameOver(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder
.setMessage("Game Over")
.setCancelable(false)
.setPositiveButton("New Game",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
})
.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}

AnimationListener inside method

I'm working on an older project, and now I'm stuck a little bit.
I have 9 buttons on one layout, and they all are connected to their methods who call their Intents.
Like this.. button 1
public void button1_click(View view){
Intent intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
}
Now I after a couple of years, I want to implement an simple animation where the button will do something (whats the animation it's irrelevant).
Now in my Animation method - startAnimation
private void startAnimation(final View view){
final Animation wigle = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
view.startAnimation(wigle);
wigle.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
}
I want to wait for the animation to finish and then to start my intent.
The button 1 with this method looks like
public void button1_click(View view){
startAnimation(view);
Intent intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
}
But my application starts the animation and then starts the Intent.
What should I implement to not to implement setAnimationListener to all buttons.
try this method
private void startAnimation(final View view){
final Animation wigle = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
view.startAnimation(wigle);
wigle.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent;
switch (view.getId()){
case R.id.btn1_id:
intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
break;
case R.id.btn2_id:
intent = new Intent(getApplicationContext(), Btn2.class);
startActivity(intent);
break;
.......
............
......
}
}
});
}
use method as
public void button1_click(View view){
startAnimation(view);
disableAllButtons();
}

How to work with onBackPressed() in Android?

I have Activity 'A', Activity 'B', Activity 'C', Activity 'D', Activity 'E', Activity 'E' . I want to exit the Activity click on onBackPressed(). When i run the app it goes to Activity 'A' it does not exit the app. How can i implement with this.
Here is my Code
public class MainActivity extends Activity {
TableLayout table_layout;
EditText firstname_et, lastname_et;
Button addmem_btn;
DatabaseHelper databaseHelper = new DatabaseHelper(this);
protected SQLiteDatabase db;
ProgressDialog PD;
ImageView imgButtonBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.performance_report);
table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);
//ImageView imgButtonBack;
imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
imgButtonBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//finish();
}
});
}
}
#Override
public void onBackPressed()
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
alertbox.setTitle("Do you wish to exit ?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// finish used for destroyed activity
finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Nothing will be happened when clicked on no button
// of Dialog
}
});
alertbox.show();
}
}
Thanks to Appreciate.
You can try this in onBackPressed:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Which exits the application. And works fine for me. Hope it helps.
call this.finish() while you open your new activity(i.e going from activty A to some other activity). In your case write this
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
MainActivity.this.finish();
}
});
You can do something like this -
private exit = false;
public void onBackPressed() {
if (exit)
Home.this.finish();
else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}

Press a button that holds geo-location details from different activity and display it on MapView?

I have a mapView class and a StoreFinder class. I want to be able to press a button that is in my AlertDialog, which holds geo-point values longitude and latitude. I want it to display the position of the longitude and latitude values in my MapView.
Simple:
Press button with longitude and latitude IN ALERTDIALOG -> MapView display position.
How would I go about doing this?
Thank you for your ample time.
CODE:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.storefinder_bars);
sirius();
}
public void sirius() {
ImageButton hoursButton = (ImageButton) findViewById(R.id.hours);
ImageButton addressButton = (ImageButton) findViewById(R.id.address);
//ImageButton phoneButton = (ImageButton) findViewById(R.id.phone);
final AlertDialog.Builder siriushours = new AlertDialog.Builder(this);
hoursButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
siriushours.setTitle("Opening Hours");
siriushours.setMessage(
"\n---Monday---\n" + "CLOSED"
+ "\n\n---Tuesday---\n" + "CLOSED"
+ "\n\n---Wednesday---\n" + "22:00 - 3:00"
+ "\n\n---Thursday---\n" + "CLOSED"
+ "\n\n---Friday---\n" + "22:00 - 3:00"
+ "\n\n---Saturday---\n" + "22:00 - 3:00"
+ "\n\n---Sunday---\n" + "CLOSED");
siriushours.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
siriushours.show();
}
});
//ADDRESS
final AlertDialog.Builder siriusadd = new AlertDialog.Builder(this);
addressButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
siriusadd.setTitle("Address");
siriusadd.setMessage(
"33 Belvoir Street, Leicester , LE1 6SL");
siriusadd.setPositiveButton("Show On Map",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
}
});
siriusadd.setNegativeButton("Route",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?daddr="+52.63274+","+-1.13145));
startActivity(intent);
}
});
siriusadd.show();
}
});
}
I want to make the POSITIVE BUTTON LOCATED HERE: "siriusadd.setPositiveButton("Show On Map", " to allow me to jump from the AlertDialog to my MapView geo-cordinate. If not, I have balloon itemized layout, I have markers in place. I would prefer if they would jump to that instead. However, geo-points would do.
Cheers for all the assistance.
Use MapController to animate to desired geopoint.
Instantiate MapController for your MapView:
private MapController mc;
mc = mapView.getController();
After initialization of dialog set onClickListener for button (here is example for positive button:
dialog.setPositiveButton(mContext.getString(R.string.photo), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mc.animateTo(geoPoint);
}
});
Just use putExtra in calling an intent:
siriusadd.setPositiveButton("Show On Map",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
Intent myIntent = new Intent(getApplicationContext(),
MyMapActivity.class);
myIntent.putExtra("id", anId);
// add any additional informations with other key values
// (latitude or longitude)
startActivityForResult(myIntent, 0);
}
});
Get these information in your map activity's onResume method into your map activity and set up your map view appropriately:
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent.getStringExtra("id") == null) {
// your map logic goes here
}
}

Categories

Resources