How to get a circular reveal animation on start of application
CircularRevealTransition();
public void CircularRevealTransition() {
int cy = view.getHeight()/2;
int cx = view.getWidth()/2;
float finalRadius = (float) Math.hypot(cx,cy);
Animator animator = ViewAnimationUtils.createCircularReveal(findViewById(R.id.cicular_reveal_id),cx,cy,0,finalRadius);
view.setVisibility(View.VISIBLE);
animator.start();
}
i have written this code inside onCreate method, its only working when applied with button click , but not with start of the application
OnCreate method is too early place to start animation, because real drawing starts much later. There is OnPreDrawListener it invokes right before view is drawn.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
CircularRevealTransition();
return true;
}
});
}
Related
I am learning custom views in android.
I made one custom view, with a rectangle and a text. The code of the custom view is:
public class TileGenerator extends View {
// rectangle parameters
private Rect rect = new Rect();
private Paint rectPaint = new Paint();
private Integer rectEndX;
private Integer rectEndY;
private Integer rectStartX;
private Integer rectStartY;
private Integer rectFillColor;
private Float rectTextStartX;
private Float rectTextStartY;
//rectangle text
private String rectText;
private Paint rectTextPaint = new Paint();
public TileGenerator(Context context) {
super(context);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setTileTitleText(String rectText) {
this.rectText = rectText;
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
rectEndX = getrectEndX();
rectEndY = getrectEndY();
rectStartX = getRectStartX();
rectStartY = getRectStartY();
rectTextStartX = rectEndX/4f + rectStartX;
rectTextStartY = 3.5f * rectEndY/4f + rectStartY;
rectTextPaint.setTextSize(rectEndY/8);
rectTextPaint.setColor(Color.BLACK);
rect.set(rectStartX,rectStartY,rectEndX,rectEndY);
rectPaint.setColor(getRectFillColor());
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(rect, rectPaint);
canvas.drawText(rectText,rectTextStartX,rectTextStartY,rectTextPaint );
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
public Integer getrectEndX() {
return rectEndX;
}
public void setrectEndX(Integer rectEndX) {
this.rectEndX = rectEndX;
}
public Integer getrectEndY() {
return rectEndY;
}
public void setrectEndY(Integer rectEndY) {
this.rectEndY = rectEndY;
}
public Integer getRectStartX() {
return rectStartX;
}
public void setRectStartX(Integer rectStartX) {
this.rectStartX = rectStartX;
}
public Integer getRectStartY() {
return rectStartY;
}
public void setRectStartY(Integer rectStartY) {
this.rectStartY = rectStartY;
}
public Integer getRectFillColor() {
return rectFillColor;
}
public void setRectFillColor(Integer rectFillColor) {
this.rectFillColor = rectFillColor;
}
public String getRectText() {
return rectText;
}
}
After that I created an blank activity. I am doing all with JAVA code. No XML. Then I try to add above custom view to a gridview layout. I want to add two custom views with different text in a horizontal gridview. So far my code is as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridLayout gridLayout = new GridLayout(this);
// first custom view
CustomRectWithText customRectWithText = new CustomRectWithText(this);
customRectWithText.setRectEndX(200);
customRectWithText.setRectEndY(200);
customRectWithText.setRectStartX(2);
customRectWithText.setRectStartY(2);
customRectWithText.setImage(image);
customRectWithText.setRectText("Text");
customRectWithText.setRectFillColor(Color.BLUE);
gridLayout.addView(customRectWithText);
// second custom view
CustomRectWithText customRectWithText1 = new CustomRectWithText(this);
customRectWithText1.setRectEndX(400);
customRectWithText1.setRectEndY(200);
customRectWithText1.setRectStartX(200 + 5);
customRectWithText1.setRectStartY(2);
customRectWithText1.setTileTitleText("Text 1");
customRectWithText1.setRectFillColor(Color.GREEN);
gridLayout.addView(customRectWithText1);
setContentView(gridLayout);
}
But still I am not getting both of the rectangles in a grid view. Only one rectangle is displayed at a time. In above case only first custom view is displayed.
Where am I doing wrong.
All I want is to make a repetitive rectangle of varying labels of any size inside a grid view.
Is this the way to do it. I mean is there any other way around.
I dont want to use ListItems.
Sorry but i do not have enough repo to comment.
But why dont you make an adapter?
Gridview behaves same as listView.
Use adapter to fill your grid.
This is the proper way to populate listView and gridView also.
new SO user here and I'm new to android as well. I need help doing a simple calculation between two edittext boxes - without a button, using a textwatcher, so that after the user is done using the edittext boxes it can add the two together and output the answer in a textview. I'd post my code here, but it just isn't any good. Can anyone provide an example of how to take two edittext boxes, assign a textwatcher to both, then put the output into a textview. Also, please keep in mind that this is eventually going to use multiple edittext boxes while autoupdating each other. This would be similar to creating forumulas in Microsoft excel and having them update immediately. One of the main problems I seem to have is catching the numberformatexception when just one is empty. Thank you for your time.
I implemented a simple app - I hope it helps you.
public class MainActivity extends Activity implements TextWatcher {
TextView tvResult;
EditText tvNumberOne, tvNumberTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.tvResult);
tvNumberOne = (EditText)findViewById(R.id.tvNumberOne);
tvNumberTwo = (EditText)findViewById(R.id.tvNumberTwo);
tvNumberOne.addTextChangedListener(this);
tvNumberTwo.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
int numOne = 0, numTwo = 0;
try{
numOne = Integer.valueOf(String.valueOf(tvNumberOne.getText()));
numTwo = Integer.valueOf(String.valueOf(tvNumberTwo.getText()));
}catch(Exception ex){
Toast.makeText(getApplicationContext(), "Parsing error!",Toast.LENGTH_SHORT).show();
}
tvResult.setText(String.valueOf(numOne + numTwo));
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
}
I'm developing an Android game with LibGDX. And I need to find the height of the soft-keyboard the user is using, as I want to display some objects right above the keyboard.
I have read this thread:
how can i get soft keyboard height on android?
But the comment suggests that it doesn't work, and it also seems to be for using with Android SDK. I'm not sure. Does anyone know a way that will definitely work?
If your problem is that your textfields are obscured then I suggest using
void Gdx.input.getTextInput(Input.TextInputListener listener,
java.lang.String title,
java.lang.String text)
instead because that will generate a native modal text input dialog that moves up and down with the keyboard. I have tried to get the height of the keyboard as well but so far I haven't managed.
See answers for this thread as well:
How do libgdx detect keyboard presence
Hopefully someone will find this answer helpful:
There is a workout to detect the exact height of the soft-keyboard which involve the Launcher Activity to send screen dimension to the game when a screen resize event occurs.
First, set a layout listener on the ViewTreeObserver of the rootView of your LauncherActivity:
public class AndroidLauncher extends AndroidApplication {
//...
public void setListenerToRootView() {
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
}
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect visibleDisplayFrame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleDisplayFrame);
game.screenResize(visibleDisplayFrame.width(), visibleDisplayFrame.height());
}
};
//...
}
If you try to get the height of the root view, it will not work as most of the games are fullscreen.
Don't forget to add and remove the listener on appropriate occurrences:
#Override
protected void onCreate (Bundle savedInstanceState) {
//...
setListenerToRootView();
}
#Override
protected void onDestroy () {
super.onDestroy();
removeListenerToRootView();
}
public void removeListenerToRootView() {
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);
}
Next, declare the screenResize method inside the Game Class which will receive the dimensions and send it to the current screen:
public class YourGame extends Game {
//...
public ScreenBase currentScreen;
//...
public void screenResize(float width, float height) {
if(currentScreen != null)
currentScreen.onScreenResize(width, height);
}
//...
}
Every screen that involves a change must implement the onScreenResize method. Introduce an Abstract Base Class of screen that has an abstract method onScreenResize. The currentScreen variable must be set in the constructor:
public abstract class ScreenBase implements Screen {
//...
public ScreenBase(YourGame game) {
//...
this.game = game;
this.game.currentScreen = this;
//....
}
public abstract void onScreenResize(float width, float height);
Implement these in whichever screen you want:
public class LoginScreen extends ScreenBase {
//...
#Override
public void onScreenResize(final float width, final float height) {
if(Gdx.graphics.getHeight() > height) {
Gdx.app.log("LoginScreen", "Height of keyboard: " + (Gdx.graphics.getHeight() - height));
}
}
}
I have two Viewpagers one above the other in my layout file.
Bottom viewpager has 3 imageviews and the top view pager is empty with transparent background.
I want to pass the scroll event from top pager to bottom pager to see the paging.
Below is the code, and is not working properly.
getScrollX always returns 0, please let me know the solution.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_screen);
ViewPager pager = (ViewPager) findViewById(R.id.bottomPager);
ImageAdapter adapter = new ImageAdapter(this);
pager.setAdapter(adapter);
pager.setPageMargin(5);
//pager.setPageMarginDrawable(R.color.white);
//pager.setClipChildren(false);
bottomPager = (ViewPager) findViewById(R.id.bottomPager);
topPager = (ViewPager) findViewById(R.id.topPager);
topPager.setOnTouchListener(new OnTouchListener() {
#SuppressLint("NewApi")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
bottomPager.scrollTo(v.getScrollX(), v.getScrollY());
if(BuildConfig.DEBUG){
Log.i("LaunchScreen.onCreate()", "inside topPager touch ###########");
Log.i("LaunchScreen.onCreate()", "X : "+v.getScrollX()+" Y : "+v.getScrollY());
}
return false;
}
});
}
I am trying to build a button with a progress bar which is displayed in the center of the button and instead of text, as soon as the button is tapped, and while some background processing takes place.
What I am doing is to have a custom button and set the animatable as a drawable left to the button, and in onDraw to move the animatable in the center of the button. However, for a short period of time after the button is tapped, I do not know why there are 2 progress bars on the button.
public class ButtonWithProgressBar extends Button {
protected int progressId = R.drawable.progress_bar;
protected Drawable progress;
protected CharSequence buttonText;
#Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (Strings.notEmpty(text)) {
buttonText = text;
}
}
public void startSpinning()
{
if(isSpinning()){
return;
}
progress = getContext().getResources().getDrawable(progressId);
final Drawable[] old = getCompoundDrawables();
setCompoundDrawablesWithIntrinsicBounds(old[0], old[1], progress, old[3]);
setEnabled(false);
if(progress instanceof Animatable){
((Animatable)progress).start();
}
setText("");
}
public void stopSpinning()
{
if(!isSpinning()){
return;
}
if(progress instanceof Animatable){
((Animatable)progress).stop();
}
setEnabled(true);
setText(buttonText);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (progress != null) {
final int drawableHeight = progress.getIntrinsicHeight();
final int drawableWidth = progress.getIntrinsicWidth();
final int drawableTop = (getHeight() - drawableHeight) / 2;
final int drawableLeft = (getWidth() - drawableWidth) / 2;
progress.setBounds(drawableLeft, drawableTop, drawableLeft + drawableWidth, drawableTop + drawableHeight);
progress.draw(canvas);
}
}
}
And this is displayed for a short period after tap, before everything looking as I want:
Any ideas why this is happening?
Found the problem.Updated code to a working version.