SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
blink(v);
//I am calling the animation only when the image is clicked.
//I want to perform animation as the page loads.
//Not on clicking the image.
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginOptionsActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
public void blink(View view){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
image.startAnimation(animation1);
}
}
blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.codeinks.luxehues.SplashActivity"
android:background="#drawable/splash_shirt">
<ImageView
android:layout_width="300dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#drawable/fullsize"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please suggest a way to perform the animation
as the page loads and not while clicking the image.
Try this in onCreate method and comment that onClick method and give a try to below code,
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation myAnim = AnimationUtils.loadAnimation(context,R.anim.blink);
imageView.startAnimation(myAnim);
Try below code:
Thread splashTread;
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
StartAnimations();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l = (LinearLayout) findViewById(R.id.layoutAnimation);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.blink);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageSplash);
iv.clearAnimation();
iv.startAnimation(anim);
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3000) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(SplashActivity.this, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
} catch (InterruptedException e) {
// do nothing
} finally {
SplashActivity.this.finish();
}
}
};
splashTread.start();
}
and corresponding xml layout is:
<!-- activity_splash.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutAnimation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.SplashActivity">
<ImageView
android:id="#+id/imageSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/splash"
android:scaleType="fitXY"
android:src="#drawable/splash" />
</LinearLayout>
If got any issue, plz bother me. Thinks will help You.
Related
I don't know why but animations for splash screen isn't working at all.
I guess my codes are correct!..
All I want is to perform the animation on the splash screen content.
public class Welcome extends AppCompatActivity {
Animation topAnim, bottomAnim;
ImageView imageView;
TextView textView;
private static int SPLASH_TIME_OUT=4000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setContentView(R.layout.activity_welcome);
topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);
bottomAnim = AnimationUtils.loadAnimation(this,R.anim.bottom_animation);
imageView = findViewById(R.id.imageView3);
textView = findViewById(R.id.textView);
imageView.startAnimation(topAnim);
textView.startAnimation(bottomAnim);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent homeIntent =new Intent(Welcome.this,MainActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
}
These are the bottom and top animations .xml files respectively
<translate
android:fromXDelta="0%"
android:fromYDelta="100%"
android:duration="1500" />
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500" />
<translate
android:fromXDelta="0%"
android:fromYDelta="-50%"
android:duration="2000" />
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500" />
Replace this line
getWindow().setContentView(R.layout.activity_welcome);
With
setContentView(R.layout.activity_welcome);
And remove setContentView(R.layout.activity_welcome); from above
new Handler().postDelayed(new Runnable() {
Just remove the followimg line:
setContentView(R.layout.activity_welcome);
above this line: new Handler().postDelayed(new Runnable() {
And it would work fine.
sorry for annoying you but i am in my first step in android programming and need your support
the question is,
i want to change between photos using next and previous Button
i have 3 photos in an array
and 2 button (next and previous )
put when i started my app. i should press next button twice to change the photo
also in previous i should press it twice to change back
hope someone help me to improve the code down
public class SabahList extends AppCompatActivity {
ImageView img;
int[] mario = new int[]{R.drawable.image_a,R.drawable.image_b,R.drawable.image_c};
int n =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
img= (ImageView)findViewById(R.id.imageView2);
}
public void btu_next(View view) {
img.setImageResource(mario[n]);
if(n<2)
n++;
}
public void btu_prev(View view) {
img.setImageResource(mario[n]);
if(n>0)
n--;
}
}
Here is your logic problem. Your button's function command them set resource first, then increase/decease value, therefore when you click the second time, it will be set as previous increasingly/decreasingly.
public void btu_next(View view) {
if(n < 2)
n++;
img.setImageResource(mario[n]);
}
This answer may helpful to you if you are using normal array added images from drawable
//Your xml like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.devobal.quitchet.SabahList"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="previous"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="next"/>
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//And your Activity like this
public class SabahList extends AppCompatActivity {
ImageView imageView2;
Button previous,next;
int i=0;
private int[] textureArrayWin = {R.drawable.star1,R.drawable.star2, R.drawable.star3,};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
imageView2 = (ImageView)findViewById(R.id.imageView2);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
if(i==0){
previous.setVisibility(View.GONE);
}
if (i==2){
next.setVisibility(View.GONE);
}
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView2.setImageResource(textureArrayWin[i]);
i--;
if(i==0){
previous.setVisibility(View.GONE);} else{ previous.setVisibility(View.VISIBLE);}
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//if you are loading from bitmap
imageView2.setImageResource(textureArrayWin[i]);
i++;
if(i==0){
previous.setVisibility(View.GONE); } else {previous.setVisibility(View.VISIBLE); }
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
}
}
This is my first question. I want to add a textview to an existing layout every second using the addView method. And I want to print the text down while they remain. But nothing appear in the layout. Hope somebody can help me.
This is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = this.findViewById(R.id.btn);
textView = this.findViewById(R.id.tv1);
linearLayout = this.findViewById(R.id.line1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
runThread();
}
});
}
private void runThread(){
new Thread() {
#Override
public void run() {
while(i++ < 10) {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView createdTextView = new TextView(MainActivity.this);
createdTextView.setText(new Date(System.currentTimeMillis()).toString() + i);
linearLayout.addView(createdTextView);
}
});
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
This is avtivity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/line1"
tools:context="com.example.runonuithread.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this:
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView createdTextView = new TextView(MainActivity.this);
createdTextView.setText(new Date(System.currentTimeMillis()).toString() + i);
createdTextView.setLayoutParams(lparams);
linearLayout.addView(createdTextView);
I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.
This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?
XML main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.how_to_use.MainActivity"
android:id="#+id/main_view">
<TextView
android:id="#+id/long_press_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long Press To Open \n'Drag n Drop'"/>
</LinearLayout>
XML 2nd page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/swirl_img"
android:src="#drawable/Swirl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private LinearLayout mainView;
private TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.long_press_textView);
addListenerLongPressBtn();
addListenerOnClickBtn();
}
public void addListenerOnClickBtn() {
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(this, DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
}
I found a solution to my own problem.
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(view.getContext(), DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
I'm making a text-based game with Android Studio.
This is the Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView)findViewById(R.id.textView5);
final TextView txt3 = (TextView)findViewById(R.id.textView6);
final TextView txt2 = (TextView)findViewById(R.id.textView7);
final TextView txt4 = (TextView)findViewById(R.id.textView8);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
new CountDownTimer(2000, 1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt1.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt2.startAnimation(fadeIn2);
txt1.setText("Text");
txt1.setVisibility(View.VISIBLE);
txt2.setText("Text");
txt2.setVisibility(View.VISIBLE);
}
}.start();
new CountDownTimer(5000, 1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
Animation fadeIn_img = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
img1.startAnimation(fadeIn_img);
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt3.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt4.startAnimation(fadeIn2);
img1.setImageResource(R.drawable.img);
txt3.setText("Text");
txt3.setVisibility(View.VISIBLE);
txt4.setText("Text");
txt4.setVisibility(View.VISIBLE);
}
}.start();
I want the animation and the countdowntimer to work only one time for each textview. When I restart the game the textviews have to display without anything.
It is like a list of textviews that display in succession every 2 seconds. When the user close and then restart the application, he has to find the state where he had finished.
How can I do that?
Thanks.
So it looks like a fainting textviews right? Heres an example
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
LinearLayout Container;
ArrayList<View> textList = new ArrayList<>();
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Container = (LinearLayout) findViewById(R.id.Container);
for (int i = 0; i < Container.getChildCount(); i++) {
textList.add(Container.getChildAt(i));
}
fadeView(textList.get(0));
}
private void fadeView(final View view) {
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadein);
count++;
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
if (count == textList.size()) return;
fadeView(textList.get(count));
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
view.startAnimation(anim);
}
}
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1.0"
android:repeatCount="0"
android:toAlpha="0.0" />
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is second"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is third"
android:textSize="30dp" />
</LinearLayout>
Just make sure to put all your views (in this case textviews) inside an array to make it clean and proper.
Hope it helps. Cheers Happy codings.