Showing posts with label Android SDK. Show all posts
Showing posts with label Android SDK. Show all posts

Monday, July 26, 2010

Android: Intent Switching / Reusing

Recently found this interesting problem in a project where I was using the Intent flag FLAG_ACTIVITY_REORDER_TO_FRONT which is awesome to reuse the same activity kinda of like a cyclic activity switcher, the one issue with this is that if you press back on an activity you have to wait till the activities onDestroy is called. That can be annoying from a UI stand point.

The solution is a combination of flags. The following line fixes the problem.
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
The reason for this it checks the reorder first and reuse or starts a new activity if it needs too.

Thursday, August 13, 2009

Notes on Android SDK:PreferenceActivity, XML Preferences and using them

Okay so today I was trying to get preferences to work with my app. Now for those of us who have developed on iPhone we are aware that we can setup a plist for settings that will be show up in the settings application on the iphone. This is find and dandy but do we want our users to have to go to the settings application only to change a few settings in our app. No of course.

Well the android has a similiar functionality, that creates a setting page quickly. The beauty of it is that you can load it into your app quite easily. There is some documentation on this widely available, the following website provides a great tutorial on setting up the ui.

http://androidguys.com
the article in question is What's Your Preference in three parts.

The one thing that it doesn't cover is how to use the application settings throughout the app. Anyway asuming you are using the above tutorial to set up user preferences.

the folllowing line of code will recover the settings.


SharedPreferences mprefs = PreferenceManager.getDefaultSharedPreferences(this);

and then you can read them like so.

CharSequence[] names = getResources().getTextArray(R.array.questiontypes);
for(int i=0; i
Log.v(TAG, mprefs.getBoolean(names[i].toString(), true) + "");
}

if you want to alter them from somewhere other than the settings page. you need
a SharedPreferences.editor from the above. Then just do some thing like the following.

meditprefs.putInt(key,value);
meditprefs.commit()
//the above is very important it savs until this line is called and changes are ignored
its like
[[NSUserDefaults standardUserDefaults] synchronize];



Wednesday, August 12, 2009

Notes on Android SDK: TabHost, Tabs and Activities

Okay so I was doing some more playing around after reading some of the guidelines. Anyway I used two approaches to using to switching between different views in my application going to post above as its useful depending on needs.


the first one is on tabhost, which allows you to switch between different views easily with the tab being pressed, for iPhone Developers its the equiavelent of TabBar Controller, but requires more work to get going specifically if you set it up via the xml view.


import android.widget.TabHost.TabSpec;
import android.widget.TabHost;
import android.app.TabActivity;

//first note is if you want to use a TabHost extend TabActivity instead of Activity
public class Maestro extends TabActivity {

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addTabs();
}

private void addTabs(){
Resources resources = getResources();

//since your using the TabActivity you can simply call this method to
//set up the tabhost make sure to call setup()
TabHost tabs = getTabHost();
tabs.setup();

//tabspec defines the attributes of the tab
TabSpec tab;
//this creates a new tabspec with an id of quiz
tab = tabs.newTabSpec("Quiz");
//this sets the intent/ activity of the tab when pressed
tab.setContent(new Intent(this, Quiz.class));
//this sets the name displayed and the image if you add one
//tab.setIndicator("Quiz");
tab.setIndicator("Quiz",resources.getDrawable(R.drawable.quiz));
//then you add the tab to the tab view
tabs.addTab(tab);

tab = tabs.newTabSpec("Practice");
tab.setContent(new Intent(this, Practice.class));
tab.setIndicator("Practice",resources.getDrawable(R.drawable.practice));
tabs.addTab(tab);

tab = tabs.newTabSpec("Settings");
tab.setContent(new Intent(this, Settings.class));
tab.setIndicator("Settings",resources.getDrawable(R.drawable.settings));
tabs.addTab(tab);

//sets the current tab
tabs.setCurrentTab(0);
//add the tabhost to the main content view
setContentView(tabs);
}


now of course you need to make sure that the classes you have set above to be invoked by the tabhost are all activities of some sort depending on your application, and make sure you add them as activites in your Application Manifest.

this is simple just add the following xml to your manifest file. where Settings is the name of your classes.

\>activity label="Settings" name="Settings">
\>\activity>

Now the last thing to note is that these are the basic of using the tabhost, using the above methods you can easily setup a tab bar and get your other views up and running, now if you want to use xml to set up tabhost there are strict requirements in setting it up.

here is what you need for the tabhost via xml

you need a tabhost
with a tabwidget
with a framelayout

according to sources there seems to be a naming convention as well, but in my opinion its too much of a hassle especially if your app only needs the tabhost as the main controller.


Next instead of using the tabhost lets use the menu list that you get when you press menu. The benefits of this is you get more room in your app. this is quite simple with a few options.

well first to get an options menu use the following.

public boolean onCreateOptionsMenu(Menu menu){
//add(groupid, itemid, order, itemname)
menu.add(0, 1, 0, "Quiz").setIcon(R.drawable.quiz);
menu.add(0, 2, 0, "Settings").setIcon(R.drawable.settings);
menu.add(0, 3, 0, "Practice").setIcon(R.drawable.practice);
return true;
}

public boolean onOptionsItemSelected(MenuItem item){
switch(item.getid()){
case 1:{
startactivity(new Intent(this,Quiz.class));
return true;
}
case 2:{
startactivity(new Intent(this,Settings.class));
return true;
}
case 3:{
startactivity(new Intent(this,Practice.class));
return true;
}
}
return false;
}


now that is the simple way that simply starts a new activity when you click on one of the menu items. Now to get back you click on the back button on the phone. As you can imagine that is annoying so you have to thinking about the design of app before you consider something like this. For instance if you want to bring up a settings page the above would be nice as you can get rid of a button from the main ui. then simply press back after changing your settings.

But if your going to try and use this as a navigation controller like a tab bar then make sure you add the following after each start activity.
finsh();

that closes the current activity before going to the next one. this gets rid of the tabhost and give you the same benefits, also by doing this the menu is dynamic unlike the tabhost. I mean why would you allow a user to choose the quiz when you are in the quiz already.

Notes on Android SDK: Dynamic TableLayouts, adding Views and TableRows

So I currently playing around with making dynamic tables on the Android platform. I want to port my iPhone applications to the other mobile platforms. Am going to be looking into Palm Pre as well soon enough. Anyway while doing this I was trying to create a Dynamic Table, instead of relying on the XML view.

now the code to adding something to a TableLayout is quite simple.


//set the content view from an XML file
setContentView(R.layout.main);

//first get the TableLayOut from the xml file that you previous loaded
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

//set the columns to resize so that the row gets the full length of the screen
table.setColumnStretchable(0, true);
table.setColumnCollapsed(1, true);

//useful for getting a resource by name
int resID = getResources().getIdentifier("filename", "drawable", "com.yourcompany.yourApp");

//now create a view you want to add to the tableLayout
//in this case a ImageView
ImageView img = new ImageView(activity);
img.setImageResource(ResID);

//set the height to 100 and width to the width of the parent
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 100));

//finally add to the tableLayout view
table.addView(img);



its quite simple, but now replace adding the ImageView to the Table with adding a TableRow to the Table which may have any number of views within. A TableRow is simply a LinearLayout.


//set the content view from an XML file
setContentView(R.layout.main);

//first get the TableLayOut from the xml file that you previous loaded
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

//set the columns to resize so that the row gets the full length of the screen
table.setColumnStretchable(0, true);
table.setColumnCollapsed(1, true);

//useful for getting a resource by name
int resID = getResources().getIdentifier("filename", "drawable", "com.yourcompany.yourApp");

TableRow row = new TableRow(this);

//the tablerow is suppose to use this layout params always according to sdk guidelines
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

//now create a view you want to add to the tablerow
//in this case a ImageView
ImageView img = new ImageView(this);
img.setImageResource(ResID);

//set the height to 100 and width to the width of the parent
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 100));

//add the ImageView to the row
row.addView(img);

//finally add to the tableLayout view
table.addView(row);


now if you have tried the above could nothing should come up, that's because certain views have their own LayoutParams class.

so what you need to do to make the above code work is change the following line.

row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

with

row.setLayoutParams( new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

the basic thing is make sure to check your using the right LayoutParams with a given class, because if your not the platform won't know what to do with that layout.

Anyway though it was worthing mentioning cause it took me a while to figure it out.