Wednesday, 28 August 2013

Using SharedPreferences


Watch this on YouTube

activity_main.xml
1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:paddingBottom="@dimen/activity_vertical_margin"
6: android:paddingLeft="@dimen/activity_horizontal_margin"
7: android:paddingRight="@dimen/activity_horizontal_margin"
8: android:paddingTop="@dimen/activity_vertical_margin"
9: tools:context=".MainActivity" >
10: <EditText
11: android:id="@+id/name"
12: android:layout_width="wrap_content"
13: android:layout_height="wrap_content"
14: android:layout_alignParentTop="true"
15: android:layout_centerHorizontal="true"
16: android:layout_marginTop="29dp"
17: android:ems="10"
18: android:hint="Enter Name"
19: android:gravity="center"
20: >
21: <requestFocus />
22: </EditText>
23: <EditText
24: android:id="@+id/mob"
25: android:layout_width="wrap_content"
26: android:layout_height="wrap_content"
27: android:layout_alignLeft="@+id/name"
28: android:layout_below="@+id/name"
29: android:layout_marginTop="28dp"
30: android:ems="10"
31: android:gravity="center"
32: android:hint="Enter Mob" />
33: <Button
34: android:id="@+id/save"
35: android:layout_width="wrap_content"
36: android:layout_height="wrap_content"
37: android:layout_below="@+id/mob"
38: android:layout_centerHorizontal="true"
39: android:layout_marginTop="24dp"
40: android:text="SAVE DATA" />
41: <Button
42: android:id="@+id/load"
43: android:layout_width="wrap_content"
44: android:layout_height="wrap_content"
45: android:layout_alignLeft="@+id/save"
46: android:layout_below="@+id/save"
47: android:layout_marginTop="24dp"
48: android:text="LOAD DATA" />
49: </RelativeLayout>
MainActivity.java
1:  package com.sharedpreference;  
2: import android.app.Activity;
3: import android.content.SharedPreferences;
4: import android.os.Bundle;
5: import android.view.Menu;
6: import android.view.View;
7: import android.view.View.OnClickListener;
8: import android.widget.Button;
9: import android.widget.EditText;
10: import android.widget.Toast;
11: public class MainActivity extends Activity {
12: Button SAVE,LOAD;
13: EditText NAME,MOB;
14: SharedPreferences prf;
15: String Name,Mob;
16: @Override
17: protected void onCreate(Bundle savedInstanceState) {
18: super.onCreate(savedInstanceState);
19: setContentView(R.layout.activity_main);
20: SAVE = (Button) findViewById(R.id.save);
21: LOAD = (Button) findViewById(R.id.load);
22: NAME = (EditText) findViewById(R.id.name);
23: MOB = (EditText) findViewById(R.id.mob);
24: SAVE.setOnClickListener(new OnClickListener() {
25: @Override
26: public void onClick(View v) {
27: // TODO Auto-generated method stub
28: Name = NAME.getText().toString();
29: Mob = MOB.getText().toString();
30: prf = getSharedPreferences("my_details", MODE_PRIVATE);
31: SharedPreferences.Editor edit = prf.edit();
32: edit.putString("key_name", Name);
33: edit.putString("key_mob", Mob);
34: edit.commit();
35: Toast.makeText(getBaseContext(), "Data Succeessfully saved", Toast.LENGTH_LONG).show();
36: }
37: });
38: LOAD.setOnClickListener(new OnClickListener() {
39: @Override
40: public void onClick(View v) {
41: // TODO Auto-generated method stub
42: prf = getSharedPreferences("my_details", MODE_PRIVATE);
43: String getName = prf.getString("key_name", "");
44: String getMob = prf.getString("key_mob", "");
45: Toast.makeText(getBaseContext(), "Name "+getName + " Mob : "+getMob, Toast.LENGTH_LONG).show();
46: }
47: });
48: }
49: @Override
50: public boolean onCreateOptionsMenu(Menu menu) {
51: // Inflate the menu; this adds items to the action bar if it is present.
52: getMenuInflater().inflate(R.menu.main, menu);
53: return true;
54: }
55: }

SpinnerView


Watch this on YouTube
activity_main.xml
1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:paddingBottom="@dimen/activity_vertical_margin"
6: android:paddingLeft="@dimen/activity_horizontal_margin"
7: android:paddingRight="@dimen/activity_horizontal_margin"
8: android:paddingTop="@dimen/activity_vertical_margin"
9: tools:context=".MainActivity" >
10: <TextView
11: android:id="@+id/textView1"
12: android:layout_width="wrap_content"
13: android:layout_height="wrap_content"
14: android:text="Select a district" />
15: <Spinner
16: android:id="@+id/sp"
17: android:layout_width="wrap_content"
18: android:layout_height="wrap_content"
19: android:layout_alignLeft="@+id/textView1"
20: android:layout_below="@+id/textView1"
21: android:layout_marginTop="22dp" />
22: </RelativeLayout>
Strings.xml
1:  <?xml version="1.0" encoding="utf-8"?>  
2: <resources>
3: <string name="app_name">SpinnerDemo</string>
4: <string name="action_settings">Settings</string>
5: <string name="hello_world">Hello world!</string>
6: <string-array name="districts_name">
7: <item>Alappy</item>
8: <item>Kollam</item>
9: <item>Kottayam</item>
10: <item>Cochin</item>
11: </string-array>
12: </resources>
MainActivity.java
1:  package com.spinnerdemo;  
2: import android.os.Bundle;
3: import android.app.Activity;
4: import android.view.Menu;
5: import android.view.View;
6: import android.widget.AdapterView;
7: import android.widget.AdapterView.OnItemSelectedListener;
8: import android.widget.ArrayAdapter;
9: import android.widget.Spinner;
10: import android.widget.Toast;
11: public class MainActivity extends Activity {
12: String[] districts;
13: Spinner sp;
14: @Override
15: protected void onCreate(Bundle savedInstanceState) {
16: super.onCreate(savedInstanceState);
17: setContentView(R.layout.activity_main);
18: sp = (Spinner) findViewById(R.id.sp);
19: districts = getResources().getStringArray(R.array.districts_name);
20: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,districts);
21: sp.setAdapter(adapter);
22: sp.setOnItemSelectedListener(new OnItemSelectedListener() {
23: @Override
24: public void onItemSelected(AdapterView<?> arg0, View arg1,
25: int arg2, long arg3) {
26: // TODO Auto-generated method stub
27: int index = arg0.getSelectedItemPosition();
28: Toast.makeText(getBaseContext(), "You select "+districts[index], Toast.LENGTH_LONG).show();
29: }
30: @Override
31: public void onNothingSelected(AdapterView<?> arg0) {
32: // TODO Auto-generated method stub
33: }
34: });
35: }
36: @Override
37: public boolean onCreateOptionsMenu(Menu menu) {
38: // Inflate the menu; this adds items to the action bar if it is present.
39: getMenuInflater().inflate(R.menu.main, menu);
40: return true;
41: }
42: }


How to show pictures from Google Plus in any website

This article shows how you can make a slideshow of all the photos from an album in your own Google Plus Photo collection, which can be shown on a website or blog.


Sharing a photo album from Google+ Photos

Google's help-pages note that you can share a Google Photos album using a link - and is a good option for showing your photos to people who are outside of Google+.

But what are the options if you want to show a Google Photos album, not just an individual picture, in your blog or website?
  • Put the link in your website. But that just gives bland, boring text, like click here to see my photos.
  • Put one picture in your website, labelled "click this photo to see the rest", and link it to your Google Photos album. But that just shows one photo - and it takes people away from your website when they go to view your photos.
  • Load each photo from the album individually to your website. That's fine for 2-3 or even 10 photos. But what if you've got dozens or even hundreds - it could take hours!

A better option is to use an embedded slideshow.

To do this, you need to get a small piece of HTML code from Google, and put it into your site. Then when someone looks at your site, the code runs and they the see a slideshow made of the photos your album at the current time.   This means that the pictures on the other website are automatically updated when you change the album in Google Photos (eg when you add, change or remove pictures).

Unfortunately there is no tool to make this type of slideshow code in Google Plus Photos at the moment.

But there is was a very simple work-around which gives you get the code that you need, using existing Google tools.

Using Picasa-web-albums to get the slideshow code

If you have loaded pictures into Google Photos, then you can manage them using either Picasa-web-albums (ref: What is Picasa vs PWA?) or Google Photos.

So, to get the embeddable slideshow display code for a photo album:
  • Navigate to the album that you want to make a slideshow for.
    NB  you need to be viewing the album, not the page of all albums, to get the correct code.
  • Make sure that the security for this album is set to at least "anyone with the link".
  • Choose Link to this Slideshow and then Embed album from the right side bar
  • Copy the HTML code that is provided

What your readers see

Readers using a regular web-browser with Flash enabled should see a slideshow of your pictures.

Readers whose device (eg cellphone, tablet) isn't able to show Flash graphics will most likely just see a black square instead of the album - possibly with a message telling them why this is happening.



Related Articles

What are Picsasa and Picasa-web-albums

How to put 3rd party HTML or Javascript into your blog

Understanding Google accounts

Putting a Picasa-web-albums slideshow into a blog post or website

WebGL dungeon crawler Moonshades now FOSS

More browser-based RPG goods for you today: the developer behind the game Moonshades recently indicated on the Opengameart.org forums that this neat old-school (ok not as old-school as Heroine Dusk) dungeon crawler is now fully open-source.

Have a look at the alpha game-play:


It seems the entire game (including the source-code) is released under the rather art focused Creative Commons Attribution (cc-by 3.0) license, but since that is pretty compatible even to the GPL, this shouldn't really matter at all.

Have fun playing!

Friday, 23 August 2013

Browser based MMO: Ironbane

I wanted to write about this browser-based MMO game called Ironbane for a while, but never actually got around trying it (it's easy though, no need to register for the alpha currently, just hit play; but for me under Linux with Firefox 23 it just kept loading and loading... could have been my very slow connection though). Luckily the creator got into contact with us to remind me about it.

Here is an slightly older video of the tutorial level:


The code (GPLv3) can be accessed on Github, and there is a nice contributors guide. The author also confirmed that there are plans to release all the artworks under CC-by-SA soon, so it can be called a proper FOSS game.

But regardless of that, I feel they need to work on the huge pixel (ok actually texel) density spread, e.g. the strongly different size of individual pixels on the screen ;)

We also asked the creator about any longer plans to commercialize it and this is what he got to offer in that regard:
When we reach beta we would like to offer optional stuff like houses, special clothes and other things for donations (nothing that can give an unfair advantage). So in a way this can be classified as F2P, yes.
Which I guess sounds like a good idea to fund further development and hosting costs.

Anyways... unless you live in the same internet darkage like me, there is no reason not to give it a try!