Problems&Solutions on Android Development for Beginner Version 2





Clicking app icon doesn't trigger onOptionsItemSelected()


For packages targetting API level 14 onwards, you need to enable the home button by calling setHomeButtonEnabled()

In your onCreate, add the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    getActionBar().setHomeButtonEnabled(true);
}

Android Tutorial: Using the ViewPager


The project was not built since its build path is incomplete

Right click on project -> Properties -> Java Build Path (From Left List) -> Libraries (Tab) -> Add Library (Button on right) -> JRE System Library -> Next -> Workspace default JRE -> Finish


public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, OverviewActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
}
return super.onOptionsItemSelected(item);
}

getResources().getString(R.string.describe_btn1);

setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments 

You are passing your Activity class as the OnClickListener in this line:
exitBtn.setOnClickListener(this);
However, your class needs to explicitly declare that it is implementing the View.OnCLickListener interface. Change your class declaration line to this:
public class welcome extends Activity implements OnClickListener


How to build .apk file?

When you run the project on the emulator, the .apk is generated on the bin directory. Keep in mind that just building the project (and not running it) will not output the apk file into the bin directory.


Unable to execute dex: GC overhead limit exceeded

It can be fixed by changing the VM values in Eclipse.ini. Set the values to 512 and 1024 as below:

openFile
--launcher.XXMaxPermSize
512M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx1024m

Best practices for exposing multiple tables using content providers in Android

I'm building an app where I have a table for events and a table for venues. I want to be able to grant other applications access to this data. I have a few questions related to best practices for this kind of problem.

First you need to create multiple CONTENT_URIs
public static final Uri CONTENT_URI1 = 
    Uri.parse("content://"+ PROVIDER_NAME + "/sampleuri1");
public static final Uri CONTENT_URI2 = 
    Uri.parse("content://"+ PROVIDER_NAME + "/sampleuri2");
Then you expand your URI Matcher
private static final UriMatcher uriMatcher;
static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri1", SAMPLE1);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri1/#", SAMPLE1_ID);      
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri2", SAMPLE2);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri2/#", SAMPLE2_ID);      
}
Then create your tables
private static final String DATABASE_NAME = "sample.db";
private static final String DATABASE_TABLE1 = "sample1";
private static final String DATABASE_TABLE2 = "sample2";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE1 =
    "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE1 + 
    " (" + _ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "data text, stuff text);";
private static final String DATABASE_CREATE2 =
    "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE2 + 
    " (" + _ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "data text, stuff text);";
Don't forget to add the second DATABASE_CREATE to onCreate()
You are going to use a switch-case block to determine what table is used. This is my insert code
@Override
public Uri insert(Uri uri, ContentValues values) {
    Uri _uri = null;
    switch (uriMatcher.match(uri)){
    case SAMPLE1:
        long _ID1 = db.insert(DATABASE_TABLE1, "", values);
        //---if added successfully---
        if (_ID1 > 0) {
            _uri = ContentUris.withAppendedId(CONTENT_URI1, _ID1);
            getContext().getContentResolver().notifyChange(_uri, null);    
        }
        break;
    case SAMPLE2:
        long _ID2 = db.insert(DATABASE_TABLE2, "", values);
        //---if added successfully---
        if (_ID2 > 0) {
            _uri = ContentUris.withAppendedId(CONTENT_URI2, _ID2);
            getContext().getContentResolver().notifyChange(_uri, null);    
        }
        break;
    default: throw new SQLException("Failed to insert row into " + uri);
    }
    return _uri;                
}
You will need to devide up the deleteupdategetType, etc. Wherever your provider calls for DATABASE_TABLE or CONTENT_URI you will add a case and have DATABASE_TABLE1 or CONTENT_URI1 in one and #2 in the next and so on for as many as you want.
SQLiteManager plugin for eclipse

You can see the sqlite database in eclipse by opening File Explorer .Then

/data/data/package_name/databases

But here we cannot see the tables and table data.
For viewing the table details Eclipse has a plugin. You can download the jar from here.


gen already exists but is not a source folder


myproject/gen already exists but is not a source folder. Convert to a source folder or rename it.

Two actions, first:
1.Right click on the project and go to "Properties"
2.Select "Java Build Path" on the left
3.Open "Source" tab
4.Click "Add Folder..." and check "gen" and "src"
second: (because the previous action asked me to remove something... I do not remember what it was...)

1. Right click on the project and go to "Properties"
2. Select "Java Build Path" on the left
3. Open Libraries "tab"
4. Add an external JAR. Add the Google API that is in your android directory (android-sdk\platforms\android-yourversion
And now it works for me!

How to create array of buttons programmatically without an error

Button btn[] = new Button[oNumber];
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);

for (int i=0;i<oNumber;i++){
    btn[i] = new Button(this); // initialize it
    btn[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    btn[i].setText(oName[i]);

btn[i].setOnClickListener(this);

    System.out.println("making b's");
    layout.addView(btn[i]);
}

*also need to add implements  OnClickListener

How to avoid automatically appear android keyboard when activity start

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


How to programmatically hide a button in android

playButton.setVisibility(View.GONE);
stopButton.setVisibility(View.VISIBLE);

How to make a smaller RatingBar?

style="?android:attr/ratingBarStyleSmall"
note: the reference already in the title of each topic.

Popular posts from this blog

เทคนิค ชนะ เกมเศรษฐี 14 ตารวด!!! ( Let's get Rich )

Using Google Chrome Inspect Element to Analyze the website

Easy way to download whole youtube play list and convert them to mp3