Android: Reading From a Properties File

September 21, 2012

Copy the file that you want to read from in the assets folder of your project

and use the following code to read

AssetManager am = c.getAssets();
inpStream = am.open(“x.properties”);
props.load(inpStream);

NOTE: I had a properties file and read it into the Java Properties object


Android: Getting Text from Selected List Item

September 19, 2012

I spent a lot of time trying to figure out how to do a very simple thing – you have a list view and you select a particular item – i wanted to get the text associated with the selected list item – it ended up being more complex than i thought ūüôā

Here is the code you will need in the AdapterView.OnItemClickListener to retrieve the textview and this code worked for me

private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position,
long id) {
Intent i = new Intent(MainActivity.this, CartoonListActivity.class);
TextView txt = (TextView) parent.getChildAt(position).findViewById(
R.id.title);
i.putExtra(“CartoonName”, txt.getText().toString());
startActivity(i);
}
};


Android UI – A Better Looking List View

September 18, 2012

I used the following article –¬†http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/, to help me build a more professional looking list view.

Currently my code uses static images, as a next step i plan to use Parse to store image information and make the UI dynamic

I like the look and feel i have got now ūüôā , if you need access to the project with all the code and the graphics – let me know

 


Android UI – Grid Layout with Image and Text

September 17, 2012

It is a typical application interface on a mobile application Рa grid layout with images being used in grid to lead the navigation flow in the application

A combination of the GridView and an Image Adapter Class make this happen

Here is a sample of such an application running on Nexus7

Application with Image Based Grid Layout

MainActivity 

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

activity_main.xml

<GridView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/gridview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:columnWidth=”150dp”
android:numColumns=”auto_fit”
android:verticalSpacing=”10dp”
android:horizontalSpacing=”10dp”
android:stretchMode=”columnWidth”
android:gravity=”center”
/>

 

grid_item.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/GridItem”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:gravity=”center_horizontal”>

<ImageView
android:id=”@+id/icon_image”
android:layout_width=”200dp”
android:layout_height=”150dp” >
</ImageView>

<TextView
android:id=”@+id/icon_text”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:text=”TextView”
android:textColorHighlight=”#656565″ >
</TextView>

</LinearLayout>

ImageAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;

if (convertView == null) { // if it’s not recycled, initialize some attributes
LayoutInflater li = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(mTextsIds[position]);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
//iv.setLayoutParams(new GridView.LayoutParams(85, 85));
//iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
iv.setImageResource(mThumbIds[position]);
} else {
v = (View) convertView;
}
return v;
}

// references to our images
private Integer[] mThumbIds = {
R.drawable.batman_96, R.drawable.caillou_96,
R.drawable.dennis_96, R.drawable.disney_96,
R.drawable.heman_96, R.drawable.looneytunes_96,
R.drawable.popeye_96, R.drawable.ppg_96, R.drawable.sd_96,
R.drawable.spm_96, R.drawable.superman_96,
R.drawable.tintin_96, R.drawable.tj_96, R.drawable.wp_96,
R.drawable.ww_96

};

// references to our texts
private String[] mTextsIds = {
“Batman”, “Caillou”,
“Dennis The Menace”, “Disney”,
“He-Man”, “Looney Tunes”,
“Popeye”, “Power Puff Girls”, “Scooby Doo”,
“Spiderman”, “Superman”,
“Tintin”, “Tom and Jerry”, “Winnie the Poo”, “Woody Woodpecker”
};
}


First Parse Application

September 16, 2012

Now you know what is Parse, how to get it and you have also initialized data in the Parse cloud that is going to be used by your Android application

You have also initialized Parse in your Android application using the initialize Parse code snippet

Now building the application is simple

  • Parse requires a call to the data on the cloud and therefore as we learnt earlier, we have to make this call¬†asynchronously – you need a AsyncTask. Look at the private class¬†RemoteDataTask ¬†that extends AsyncTask
  • Learn to use the query language – it is simple :¬†https://parse.com/docs/android_guide#queries
  • the rest of the steps are the same as you would do in a typical application – you have access to the data¬†dynamically, build the adapter and associate the adapter to the view, do all this in in onPostExecute method
  • Enable internet access on your AndroidManifest file –¬†<uses-permission android:name=”android.permission.INTERNET” />

public class MainActivity extends Activity {

private ListView categoryListView;
private ArrayAdapter<String> categoryListAdapter;
private List<ParseObject> categories;
private ArrayList<String> categoryList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeParse();
new RemoteDataTask().execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private void initializeParse() {
Parse.initialize(this, “NDUawcm7DwXAkkSnuLMMiG6waq7l2RgOOu6YOpZB”,
“z3OdV85A5Dvr3tj49M06nZ1GdvQFa4nJLzxxW6Ct”);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
ParseACL.setDefaultACL(defaultACL, true);
}

private class RemoteDataTask extends
android.os.AsyncTask<Void, Void, Void> {
// Override this method to do custom remote calls
protected Void doInBackground(Void… params) {
// Gets the current list of Categories in sorted order
ParseQuery query = new ParseQuery(“Categories”);
query.orderByDescending(“_created_at”);

try {
categories = query.find();
Log.i(“MainActivity:RemoteDataTask:doInBackground:Categories:”,categories.toString());
} catch (ParseException e) {
Log.e(“MainActivity:RemoteDataTask:doInBackground:Exception Retrieving Categories:”,
e.getMessage());

}
return null;
}

protected void onPostExecute(Void result) {
// Put the list of categories into the list view
for (ParseObject category : categories) {
categoryList.add((String) category.get(“Name”));
}
categoryListView = (ListView) findViewById(R.id.categoryListView);
categoryListAdapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.activity_main, R.id.categoryTextView, categoryList);
categoryListView.setAdapter(categoryListAdapter);
}

}

}


Getting Started with Parse

September 16, 2012
  • Register and get started with the basic plan
  • Create an application – just requires the name
  • Go to the Data Browser, choose the application and create a Class – this is equivalent to the Table in the DB
  • There are standard fields that come with every class and add additional columns you need – i just created a Name column
  • Start creating rows and add data to them – all the default columns will be auto filled, add values to the custom¬†columns

Creating Table and Data in Parse

  • Copy the Application Id and Client Key from the Overview Page – you will need these to initialize in your android application java code

Keys required for Parse Application

  • In your main activity for the application that uses parse call the following initializeParse method from your onCreate method

              onCreate()

                    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeParse();
}

initializeParse()

private void initializeParse() {
Parse.initialize(this, “NDUawcm7DwXAkkSnuLMMiG6waq7l2RgOOu6YOpZB”,
“z3OdV85A5Dvr3tj49M06nZ1GdvQFa4nJLzxxW6Ct”);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
ParseACL.setDefaultACL(defaultACL, true);
}


Android and Parse

September 16, 2012

Parse: https://parse.com/ is a mobile app platform that allows you to store your applications data in the cloud. It has a full sdk that handles storage of basic data types, locations, photos, and query across them in just a few lines of code. It also comes with a data browser that lets you manage all your objects in an intuitive spreadsheet-like interface.

I plan to use parse in Android and will start with a basic plan [https://parse.com/plans]. In the next set of posts, i will provide examples of using Parse with Android