Wednesday, November 2, 2011

array list city android

private String[] arrListCity;
arrListCity = getResources().getStringArray(R.array.list_city_new);
public void prepareArrayLits()
    {
        itemList = new ArrayList<Object>();
        for(int i=0; i< arrListCity.length; i++)
          {
            Log.w(">>>>>>>>>>> test array", arrListCity[i]);
            AddObjectToList(R.drawable.arrow, arrListCity[i], "Kim Tuyến run rẩy khi chụp ảnh với nhện - VN's Next Top Model");
          }
       
    }

Wednesday, October 26, 2011

Shared Preferences button back splash with android exam

//Splash
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);
        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        check = app_preferences.getInt("check", 1);
    }
//
protected void onResume() {
        // TODO Auto-generated method stub
        if(check==1){
            newTimerCounter(3);
        }else {
            finish();
        }
        super.onResume();
    }
//Activity Main
public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
             SplashActivity.check = 0;
             Log.d(this.getClass().getName(), "back button pressed");
            }
            return super.onKeyDown(keyCode, event);

    }

send Bundle value intent with android exam

//Activity 1
public void onClick(View arg0) {
                long value;
                if (tinhthanh == 0) {
                    value = 1;
                }
                else {
                  value = 0;
                }
                Bundle sendBundle = new Bundle();
                sendBundle.putLong("value", value);
                Intent i = new Intent(VNTaxiCallActivity.this, ListTaxi.class);
                i.putExtras(sendBundle);
                startActivity(i);
                overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
            }
//Activity 2
Bundle receiveBundle = this.getIntent().getExtras();
            final long int_tinhthanh = receiveBundle.getLong("value");
            lblHeader =  (TextView) findViewById(R.id.lblHeaderCity);
            if(int_tinhthanh==1){
                Taxi_HaNoi();
            } else if (int_tinhthanh==2) {
                Taxi_DaNang();
            } else if (int_tinhthanh==3) {
                Taxi_HCM();
            }else {
                Taxi_CanTho();
            }

sort abc listview adapter android exam

java.util.Arrays.sort(arrListCity, java.text.Collator.getInstance(new Locale("vi")));

Friday, October 21, 2011

exit app android exam

 private void quitApplication() {
  try {
   ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
   List<ActivityManager.RunningAppProcessInfo> process = manager
     .getRunningAppProcesses();
   int pid = 0;
   for (RunningAppProcessInfo runningAppProcessInfo : process) {
    if (runningAppProcessInfo.processName
      .equals(this.getPackageName())) {
     pid = runningAppProcessInfo.pid;
     break;
    }
   }
   android.os.Process.killProcess(pid);
  } catch (Exception e) {
   Log.e(TAG, TAG + e);
  }
 }

Thursday, October 13, 2011

ListViewAdapter android exam

/*public void initComponent()
    {
        lblHeader = (TextView)findViewById(R.id.lblHeaderCity);
        lvCity = (ListView)findViewById(R.id.lvCity);
        arrListCity = getResources().getStringArray(R.array.list_city_central);
       
        lblHeader.setText(R.string.central);
        ListViewAdapter adapter = new ListViewAdapter(this, arrListCity);
        lvCity.setAdapter(adapter);
    }*/

Monday, October 3, 2011

Android: Quickly building a menu for an application

After creating an Android Project in Eclipse called “AndroidMenus” I then set about creating a new XML file in a folder (you need to create this folder) called “menu” under the “res” (parent) folder. I named the xml file “mymenu.xml” and added this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menuItem1"
          android:icon="@drawable/menu_item1"
          android:title="@string/menu_item1" />
    <item android:id="@+id/menuItem2"
          android:icon="@drawable/menu_item2"
          android:title="@string/menu_item2" />
</menu>
As you can see we need to make sure we have some icons for the menu as well as some menu item strings in the strings.xml file which is located in the “res/values” folder. Here is my modified strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, AndroidMenus!</string>
    <string name="app_name">Android Menus</string>
    <string name="menu_item1">Item 1</string>
    <string name="menu_item2">Item 2</string>
</resources>
The “main.xml” file can be left just as the project wizard created it however you need to modify the “AndroidMenus.java” file to override the onCreateOptionsMenu method.
package com.giantflyingsaucer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class AndroidMenus extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.mymenu, menu);
        return true;
    }
}
You also need to create some icons and place them into the appropriate folders as seen below:

Once you’ve got this done you can run the project. In the emulator press the “Menu” button to trigger the menu.
Results:

Yes its pretty ugly but like I said I’m not a graphics person. If you click on the menu items nothing happens, lets change this now by adding some Java code to the “AndroidMenus.java” file. For this we need to override the onOptionsItemSelected method.
package com.giantflyingsaucer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenus extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.mymenu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menuItem1:
            Toast.makeText(AndroidMenus.this, "Menu Item 1 was pressed", 3000).show();
            return true;
        case R.id.menuItem2:
            Toast.makeText(AndroidMenus.this, "Menu Item 2 was pressed", 3000).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}
You can see with the code depending what choice is made is reflected with a quick toast notification.
Results:










Full download