Showing posts with label view. Show all posts
Showing posts with label view. Show all posts

Monday, November 14, 2016

Activity's and Fragments

Last summer during my internship I used Fragments quite often and as such for a while I understood at least the basics about Fragments, their use cases, and what strengths and weaknesses come with Fragments. Unfortunately, quite recently I had my knowledge tested on this and I learned quite quickly that I don't understand Fragments as good as I once did. So after going back and using Fragments over the weekend and reading the docs and as many tutorials as possible, I finally at least got a slight refresher on Fragments.


When is it better to use a Fragment over an Activity?


Well, actually they really just work hand in hand. An Activity will hold the context and everything you need for the Fragment, the Fragment however will store information and thus you can pass information easily through from Fragment to Fragment.

When is this useful?


Let's say for a minute that you are a general contractor and have 7 houses you're currently building. For each house you have information you need to know, but shouldn't be included in the list.

Now you could do a list like below where each item in the list is associated with an ID which you send through an intent to another activity. This might look great on phone with a relatively small screen, but on a larger device like a tablet, as you can see below, it could definitely look better.



This is where fragments come to the rescue. Let's go back to that list. Now lets say that each item in that list actually has a reference to a JobSite object that has all of the information about the job site. The fragment can pass objects through the activity to another fragment within the same Activity. Doing that you can fairly easily do something like this.


On a mobile phone you can easily use an animation to slide, expand, fade or whatever animation works best for you. On a tablet however this makes your app more user friendly across a wider range of devices.

How do I pass information to the Activity?


Passing data from the Fragment to the parent Activity is fairly simple. Within the fragment make a call to getActivity() and you can use all of the accessor methods available in the parent Activity.

Wednesday, December 16, 2015

libGDX Android Text Input

libGDX has its ups and its downs. It isn't perfect by any means, but I still believe it has a ton of potential especially with RoboVM at its side which makes it work wonderfully with iOS and still keeps it working great for Android and Desktop. I love the make it once, run it everywhere aspect that it provides. Also the same reason I love Java.

However the problem I was facing today was dealing with Android text input. I am amid creating my own library add-on for libGDX that provides a lot of the same GUI menu like items provided with Android, however they are directly for libGDX and use within games and game menus. I have been working on Buttons, Layouts, Text Inputs, Labels, Custom Widgets, Loading Bars, you name it. However when it came to text input from the soft-keyboard provided in Android, there were bugs that were all dependent on device, or if they weren't using Google keyboard. For a game that is supposed to run flawlessly on any device on which it can be installed, this wouldn't cut it. So I did a bit of popping around on the internet and came up with this way of creating a popup for Android that would take input.


Input.TextInputListener inputListener = new Input.TextInputListener() {
    @Override
    public void input(String input) {
        text = input;
    }

    @Override
    public void canceled() {

    }
};
             
Gdx.input.getTextInput(inputListener,  hint, text, "");

This worked excellently. It gave me a little popup that allowed for me to input text, then it would make the text in the widget, which I call EditText after Android EditText widget, change to be whatever I put in the popup. Everything was great. Just one problem. The popup looked almost 'prehistoric' or at least it was Android Froyo styling. Not exactly my taste. I like Lollipop much better, but I also supported back to Android Jelly Bean. So I did the next best thing.

In my Android part of the project I went into styles.xml and changed one part so that the android Dialog would look how I wanted it to.


Before:
<resources>
    <style name="GdxTheme" parent="android:Theme">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


After:
<resources>
    <style name="GdxTheme" parent="android:Theme.DeviceDefault">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


Changing the parent of GdxTheme from android:Theme to android:Theme.DeviceDefault will make it so that the popup matches whatever OS you're running, be it KitKat, JellyBean, or Marshmallow.

That's all for today. Hope this helps anyone else struggling with this problem.

Friday, October 24, 2014

Android Grow View Dynamically

My problem was I wanted to add animation to my app in order to make it more appealing, and to more along the lines of material design.
I did a couple quick Google searches, but didn't find much. Instead I came up with this quick trick that made it possible.
public class ResizeAnimation extends Animation {
    private int startHeight;
    private int deltaHeight; // distance between start and end height
    private View view;
    /**
    * constructor, do not forget to use the setParams(int, int) method before
    * starting the animation
    * @param v
    */
    public ResizeAnimation (View v) {
        this.view = v;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
     }
    /**
    * set the starting and ending height for the resize animation
    * starting height is usually the views current height, the end height is the height
    * we want to reach after the animation is completed
    * @param start height in pixels
    * @param end height in pixels
    */
    public void setHeightDifference(int start, int end) {
        this.startHeight = start;
        deltaHeight = end - startHeight;
    }
    /**
    * set the starting and ending width for the resize animation
    * starting width is usually the views current width, the end width is the width
    * we want to reach after the animation is completed
    * @param start height in pixels
    * @param end height in pixels
    */
    public void setHeightDifference(int start, int end) {
        this.startWidth = start;
        deltaWidth = end - startWidth;
    }
    /**
    * set the duration for the hideshowanimation
    */
    @Override
    public void setDuration(long durationMillis) {
        super.setDuration(durationMillis);
    }
    @Override
    public boolean willChangeBounds() {
        return true;
    }
}
LinearLayout clickableLayout = (LinearLayout) findViewById(R.id.click able_layout)
clickableLayout.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){
        RelativeLayout v = (RelativeLayout) V;
        // getting the layoutparams might differ in your application, it depends on the parent layout
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();
        ResizeAnimation a = new ResizeAnimation(relativeLayout);
        a.setDuration(500);
        // set the starting height (the current height) and the new height that the view should have after the animation
        a.setParams(lp.height, newHeight); relativeLayout.startAnimation(a);
        v.startAnimation(a)
    }
}
       
The difference between this and a scale animation is you can actually have one line of text on a linear layout, grow to three lines, but not in a scale like way of squished text. With the XML scale setting you would actually end up having the space all pop up there, but the animation would slowly fill it, with this, it will create the space every time it grows making it more smooth and clean.