I am using Android Studio, and I want to open a class page with an intent.
My code currently looks like this
public class StartUpScreen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_up_page);
final ImageView MyImageView =(ImageView) findViewById(R.id.imageView);
final Animation Animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.startup);
final Animation Animation2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
MyImageView.startAnimation(Animation);
Animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
MyImageView.startAnimation(Animation2);
finish();
Intent a = new Intent(this,MainPage.class);
startActivity(a);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
All looks good, but my Intent has a red line from (this,MainPage.class); I have looked and tried changing it with no luck.
Can anyone please help?
Related
The app I'm trying to develop has a ViewFlipper and some buttons bellow corresponding to views, you click on the third button and the ViewFlipper changes views to the 3rd image, the problem is that once I click that button the timer doesn't reset to 0, so for example if I was on the second image for 4 secs and then if I change to the 3rd image, after 1 sec the ViewFlipper will change to the 4th image, I wanted the timer to reset back to 0 so that way it would stay on the view that I changed to for 5 secs.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
add this to your image2 click listener :
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
viewFlipper.setFlipInterval(flippertime);
viewFlipper.startFlipping();
Ok i found the answer, what i needed to do was use a Handler, the problem at first is that the Handler would overwrite other previous handlers and when i changed to one view and then changed to another both handlers would still be waiting, so all i needed to add was this line handler.removeCallbacks(runnable);
Here's my full current code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
handler.removeCallbacks(runnable);
} catch(Exception e){}
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
viewFlipper.showNext();
viewFlipper.startFlipping();
}
}; handler.postDelayed(runnable, flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
While debugging on USB I just wanted to expand WebView to fullscreen. Unfortunately, white screen appeared and nothing after it. I really do not know what to do. I made CustomView and everything seemed fine. Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filmkey = "<iframe width=\"96%\" height=\"96%\" src=\"https://www.youtube.com/embed/EtMOgsWEAmQ\" frameborder=\"0\" allowfullscreen></iframe>";
screen=(WebView)findViewById(R.id.webView);
screen.getSettings().setJavaScriptEnabled(true);
String myvideokey = filmkey;
screen.loadData(myvideokey, "text/html", "utf-8");
screen.setWebChromeClient(new WebChromeClient(){
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
}
});
}
try the following Code. In this on full screen i created a View which will become full Screen.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
screen.restoreState(savedInstanceState);
}
setContentView(R.layout.activity_support);
filmkey = "<iframe width=\"96%\" height=\"96%\" src=\"https://www.youtube.com/embed/EtMOgsWEAmQ\" frameborder=\"0\" allowfullscreen></iframe>";
screen=(WebView)findViewById(R.id.webview);
screen.getSettings().setJavaScriptEnabled(true);
String myvideokey = filmkey;
screen.loadData(myvideokey, "text/html", "utf-8");
CustomWebChromeClient custom = new CustomWebChromeClient();
screen.setWebChromeClient(custom);
}
and the custom WebChromeClient is
class `CustomWebChromeClient` extends WebChromeClient{
#Override
public void onShowCustomView(View view, final CustomViewCallback callback) {
super.onShowCustomView(view, callback);
Dialog dialog = new Dialog(SupportActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
view.setBackgroundColor(Color.TRANSPARENT);
dialog.setContentView(view);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
callback.onCustomViewHidden();
}
});
dialog.show();
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
}
}
and also make sure you use all these methods in the activity to handle with the data in webView.
#Override
protected void onResume() {
super.onResume();
screen.onResume();
}
#Override
protected void onPause() {
super.onPause();
screen.onPause();
}
#Override
protected void onStop() {
super.onStop();
screen.stopLoading();
}
#Override
protected void onDestroy() {
super.onDestroy();
screen.destroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
screen.saveState(outState);
}
I hope it helps.
I made a button for Android that rotates on click, but when I set a button and new activity, when I click it's just set me to new activity.
I need just this: when I click on that button, first to do animation e.g. rotate, then to execute a new activity. Here is my code:
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
startActivity(new Intent("com.example.threepandas.MENU"));
}
});
You can set animation listener, when finish animation start your activity.
Please refer this link
Example code (must update based on your purpose):
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
}
});
pandarotate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent("com.example.threepandas.MENU"));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
insert a delay for the length of the animation after the animation starts and before the activity starts
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Here is the solution I use for this problem (got it from the Google I/O App Source Code on Github)
private static final int DELAY = 250;
private Handler mHandler;
#Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.button:
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, DELAY);
break;
}
}
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();
}
I have seen a lot of answers but doesn't seems to find one. I am using new FAB in one of my fragments and want to remove it when that particular fragment goes to backstack, but I am not sure which method in fragment gets called when it is added to back stack and replaced by other fragment.
Following methods were called whenever the Fragment is replaced or added to backstack
1) onPause()
2) onStop()
3) onDestroyView()
call your FAB removing method in any one of the above methods in your Fragment.
http://developer.android.com/guide/components/fragments.html#Creating
Here is my suggestion -
First, add the animation code and backstack listener in your Activity:
public class MainActivity extends AppCompatActivity
implements FragmentManager.OnBackStackChangedListener {
private FloatingActionButton mFab;
private Animation mShowFab;
private Animation mHideFab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().addOnBackStackChangedListener(this);
mShowFab = AnimationUtils.makeInAnimation(this, false);
mShowFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
mFab.setVisibility(View.VISIBLE);
}
});
mHideFab = AnimationUtils.makeOutAnimation(this, true);
mHideFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mFab.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
}
public void showFab(boolean show) {
boolean visible = mFab.isShown();
if (show) {
if (!visible)
mFab.startAnimation(mShowFab);
} else {
if (visible)
mFab.startAnimation(mHideFab);
}
}
And then - depending on backstack depth - show or hide the FAB:
#Override
public void onBackStackChanged() {
showFab(getSupportFragmentManager().getBackStackEntryCount() > 0);
}