Android GridView on Home Page with auto resized images.

When looking for a way to get a grid view with images like the above, my final result is something I want with icons and an image to depict a task.

I had to follow the below steps:

  1. Create a main fragment and add your grid view to it.
  2. Define a grid view item with an image and a text on its footer.
  3. Add an Adapter to the GridView for it to be compatible.
  4. Add the GridView adapter to the Main Fragment.

In you home fragment, for example ‘fragment_main.xml’ 

<FrameLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nomoreboundary.propertymanagerpro.MainActivity.MainFragment"
    android:id="@+id/frameLayout_main">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView_main"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:numColumns="2"
        android:smoothScrollbar="true" />

</FrameLayout>

We will create a new layout for an image that is to be shown on a grid.  Let say it includes and image and a text on its footer.

grid_image.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.nomoreboundary.propertymanagerpro.util.GridImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</FrameLayout>

 

Image View class with overridable sizing

GridImageView.java

public class GridImageView extends ImageView {

    public GridImageView(Context context) {
        super(context);
    }

    public GridImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GridImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); 
    }

}

 

An adapter for showing the grid view

MainGridAdapter.java

class MainGridAdapter extends BaseAdapter {

        private List<Item> items = new ArrayList<>();
        private LayoutInflater inflater;

        public MainGridAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("Image 1", R.drawable.bk_beach));
            items.add(new Item("Image 2", R.drawable.bk_bluecoffee));
            items.add(new Item("Image 3", R.drawable.bk_purple_yellow));
            items.add(new Item("Image 4", R.drawable.bk_railroad));
            items.add(new Item("Image 5", R.drawable.bk_lines));
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
                v = inflater.inflate(R.layout.grid_main, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
}

 

Add the grid adapter to the Main Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View rootView = inflater.inflate(R.layout.fragment_main, container, false);

	// Set up the grid view.
	GridView gridView = (GridView) rootView.findViewById(R.id.gridView_main);
	gridView.setAdapter(new MainGridAdapter(getActivity()));

	return rootView;
}
Advertisements

Android NDK Interview Questions

General


 

1. What is Android NDK?

Native development Kit as it it called gives you ace

2.  When can the callback implementations to NativeActivity cause an ANR?

Native activity contains all the callback information and data structures.  The main thread handles the callbacks.  So, your callback implementations should not be blocking.  If they block, you may receive an ANR because your main thread is unresponsive until the callback returns.

3.

Android Interview questions

Threads


 1.  During Android unit testing, what is the difficulty in using ‘static’ variables?

Static variables are cleared and set to ‘null’ each time a set of data is called.  This is because your app is reset/killed each time

Solution:  Call super.tearDown() before each test.

2.   What is dependency injection in Android?

Dependency injection is a style of object configuration in which an objects fields and collaborators are set by an external entity.

In Android, Dagger & Guice are examples of dependency injectors:

RoboGuice 2 example:

    @ContentView(R.layout.main)
    class RoboWay extends RoboActivity { 
        @InjectView(R.id.name)             TextView name; 
        @InjectView(R.id.thumbnail)        ImageView thumbnail; 
        @InjectResource(R.drawable.icon)   Drawable icon; 
        @InjectResource(R.string.app_name) String myName; 
        @Inject                            LocationManager loc; 

        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            name.setText( "Hello, " + myName ); 
        } 
    }

 

3.  How is Serializable different from Parcel?

Parcels are designed for high performance IPC transport, used heavily in Binders.  For normal serialization concepts, use Serializable.  Serializable is much heavier and takes more time.  Therefore Parcels are used in embedded devices.

4.   What is a Handler, Thread & Loader?

Handler:  A class that can synchronize a worker thread to the main thread.  In the example of ASyncTask, the onProgressUpdate() and the doPostExecute() method are already synchronized.  They are examples of handlers.  You can create your custom handler methods for your custom thread classes.

Thread:  A line of execution.

Loader:

  • It provides asynchronous loading of data.
  • They are available to every Activity and Fragment.
  •  They monitor the data source and deliver new results when contents change.
  • They automatically reload to their last loader cursor when being recreated after a configuration change.  They don’t need to re-query data

5.  What is a looper, message queue and handler?

Looper – part of any Thread to loop through message queue.  This will be used to check if any incoming message has arrived to this thread. Only one looper exists for a given thread. Only for handler threads this looper will be activated, for other normal threads this will be in passive or inactive mode.

Message Queue – part of any thread, will store incoming messages to this thread. For any thread only one message Q is available. Handler – communication channel between two threads.

Handler is associated with Looper. for a given looper we can n number of handlers to communicate with it from out side world.

Memory Leaks


 1.  What are some of the causes for memory leaks in Android?

Leaking a context causes references to an Activity’s view group and all its resources inside it.

2.  What is the most common memory leak in Android?

Screen orientation change.  When this happens,  Android destroys the current activity and creates a new one  preserving its state.   This can cause a leak in the entire Activity and all its references resources.

3.  What is a leak?

Keeping a reference to any object that the garbage collector is unable to collect.

4.  How to avoid Context related leaks?

  • Avoid using non-static inner classes in an activity if instances of the inner class could outlive the activity’s lifecycle. Instead, prefer static inner classes and hold a weak reference to the activity inside.

5. How can you leak a Context?

In Java, non-static inner and anonymous classes hold an implicit reference to their outer class. Static inner classes, on the other hand, do not.

Views


 1.  How do you create a View programmatically without using a View  Inflater service?

LinearLayout myLayout = findViewById(R.id.main);

Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                          LinearLayout.LayoutParams.FILL_PARENT,
                          LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

Context


 1. What are contexts used for?

Various reasons but mostly to load and access resources.  This is why all widgets have a context parameter.

2.  What are some types of Contexts?

Application Context

An instance of Context that is tied to the lifecycle of the application.

Example:  Use when you need something that is tied to a global scope.  Such as a WakefulIntentService.  To get a static WakeLock you would need a context to PowerManager.  In this case it is safest to use getApplicationContext().

Activity context

An instance of Context that is tied to the lifecycle of an activity.

Usually passed to classes and methods that require a context.   Holds a reference to a Viewgroup and all the resources inside it.   If a context leaks, the entire activity can be leaked since a reference is always held causing a lot of memory to be held up.

Services


1.  What is a started Service?

When this service is started, it has a lifecycle that’s independent of the component that started it and the service can run in the background indefinitely, even if the component that started it is destroyed. As such, the service should stop itself when its job is done by calling stopSelf() or another component can stop it by calling stopService()

2.  What is a bound Service?

You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application’s functionality to other applications, through interprocess communication (IPC).

Application Questions


 1. Photo Sharing App / notification manager attach to a notification object, what would you use

Pending Intent

2. Data from one app to another and if you want some fields hidden

Content Provider

3. If your app has to have messaging as an option, what would you use to navigate to the messaging app.

 

4.  If you want an animation to appear .5 sec after a button click, what would you use?

 

5. Android Service – Does it run on UI thread?

Yes

6.  How do you schedule a recurring task at periodic intervals

Alarm Manager

My Rental Pro

Today’s Featured App

My Rental Pro

mrp_feature_graphic

Ever found the need to manage you rental property the way you want it to be.  An intuitive app that tracks and shows what you need and watch your cash flow grow.

You can use the app to manage all your rental properties, collect rents, track expenses.  If your property has a mortgage on it, the app provides parameters to see how your rents calculate against your existing loan.   The best part of the app is visualizing cash-flow against all values of your property and see how it is doing.

You can also hypothetically model a property and see if it will make money starting this year.

Get it on Google Play

Rails Formtastic – How to remove the unordered list from form inputs

When using ‘Formtastic’ the default behavior is to add an unordered list to the set of inputs.  This is specified in its CSS.   This makes the form look like it has a set of radio bullets.

How to remove this?

From an input:

<%=f.input :zip, :label => false, :placeholder => '5 digit zip',
           :input_html => { :class => 'form-control' },
           :wrapper_html => { :style => 'list-style-type:none;' } %>

From a button:

 <%= f.action :submit, :label => "Continue", 
              :button_html => { :class => 'btn btn-danger' },
              :wrapper_html => { :style => 'list-style-type:none;' }%>

 

How to create a guest user in Rails 4

If you have Devise authentication:

 

class ApplicationController < ActionController::Base

  protect_from_forgery

  # if user is logged in, return current_user, else return guest_user
  def current_or_guest_user
    if current_user
      if session[:guest_user_id] && session[:guest_user_id] != current_user.id
        logging_in
        guest_user(with_retry = false).try(:destroy)
        session[:guest_user_id] = nil
      end
      current_user
    else
      guest_user
    end
  end

  # find guest_user object associated with the current session,
  # creating one as needed
  def guest_user(with_retry = true)
    # Cache the value the first time it's gotten.
    @cached_guest_user ||= User.find(session[:guest_user_id] ||= create_guest_user.id)

  rescue ActiveRecord::RecordNotFound # if session[:guest_user_id] invalid
     session[:guest_user_id] = nil
     guest_user if with_retry
  end

  private

  # called (once) when the user logs in, insert any code your application needs
  # to hand off from guest_user to current_user.
  def logging_in
    # For example:
    # guest_comments = guest_user.comments.all
    # guest_comments.each do |comment|
      # comment.user_id = current_user.id
      # comment.save!
    # end
  end

  def create_guest_user
    u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(100)}@example.com")
    u.save!(:validate => false)
    session[:guest_user_id] = u.id
    u
  end

end

 

Redirect to previous page after sign in

When using ‘Devise’ for your authentication mechanism,  you may need to redirect to the previous webpage after signing in.

To do this:

class Users::SessionsController < Devise::SessionsController
  layout 'user'

  after_filter :store_location

  def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    return unless request.get?
    if request.path != "/users/sign_in" &&
        request.path != "/users/sign_up" &&
        request.path != "/users/password/new" &&
        request.path != "/users/password/edit" &&
        request.path != "/users/confirmation" &&
        request.path != "/users/sign_out" &&
        !request.xhr? # don't store ajax calls
      session[:previous_url] = request.fullpath
    end
  end

  #Take me to the referring page after sign in
  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

end

 

Writing an Executive Summary

Executive Summary

An executive summary should contain the following points:

One or two line summary

A concise one to two line depiction of what your company does with its product or service.

Services or Product provided

3. Market definition

4. Risk / Opportunity

5. Competition

6. Capital Requirements

7. Operations

8.  Financial Snapshot

9.  Previous Capital Raises

My Rental Pro

Today’s Featured App

My Rental Pro

mrp_feature_graphic

Ever found the need to manage you rental property the way you want it to be.  An intuitive app that tracks and shows what you need and watch your cash flow grow.

You can use the app to manage all your rental properties, collect rents, track expenses.  If your property has a mortgage on it, the app provides parameters to see how your rents calculate against your existing loan.   The best part of the app is visualizing cash-flow against all values of your property and see how it is doing.

You can also hypothetically model a property and see if it will make money starting this year.

Get it on Google Play