Saturday, January 12, 2013

AsyncTask and reading internet data


 public class Nekaj extends Activity {

TextView Tkolo, Tbroj1;
String[] brojevi_varijabla;
String privremena_varijabla = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bez_provjere_739);
    Tkolo = (TextView) findViewById(R.id.Xkolo);
    Tbroj1 = (TextView) findViewById(R.id.Xbroj1);

    /*
     * try { privremena_varijabla = test.kolo_739();
     * Tkolo.setText(privremena_varijabla); } catch (Exception e) { // TODO
     * Auto-generated catch block e.printStackTrace(); }
     */

    new loadData().execute();
}
public class loadData extends AsyncTask<String, Integer, Boolean> {
 protected Long doInBackground(String... arg0) {
     Oto test = new Oto();
     Boolean result = false;

    try {
        brojevi_varijabla = test.brojevi_739();//Your
        result = true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
 }


 protected void onPostExecute(Boolean result) {
     if(result){
        if (Integer.valueOf(brojevi_varijabla[0]) > 10) {
            Tbroj1.setText("" + brojevi_varijabla[0]);
        } else {
            Tbroj1.setText(" " + brojevi_varijabla[0]);
        }
     }

 }
}
public class Oto {
    public String[] brojevi_739() throws Exception {

        int i = 0;
        int uvjet = 0;
        int varijabla = 0;
        char[] znak = { '>', '<' };
        BufferedReader in = null;
        String data[] = null;
        String provjera = "date-info";
        int[] polje = new int[2];

        try {
            HttpClient klijent = new DefaultHttpClient();
            URI webstranica = new URI(
                    "https://www.aaa.bb");
            HttpGet zahtjev = new HttpGet();
            zahtjev.setURI(webstranica);
            HttpResponse odgovor = klijent.execute(zahtjev);
            in = new BufferedReader(new InputStreamReader(odgovor
                    .getEntity().getContent()));
            StringBuffer brojevi = new StringBuffer("");
            String brojevi_string = null;
            String neki_string = null;
            String red = "";

            in.skip(21000);
            while ((red = in.readLine()) != null) {
                varijabla = red.indexOf(provjera);

                if (varijabla != -1) {

                    // 1. KOLO
                    if (uvjet == 0) { // onda sadrži taj
                                        // substring
                                        // !!!!
                        red = in.readLine(); // sada string red sadrži ono
                                                // što
                                                // želim, još moram samo to
                                                // izrezati!!
                        do {
                            if (i == 0) {
                                varijabla = red.indexOf(znak[i]);
                            }

                            else {
                                varijabla = red.indexOf(znak[i], polje[0]);
                            }

                            if (varijabla != -1) // ako taj znak postoji u
                                                    // stringu
                            {
                                if (i == 0) {
                                    polje[i] = varijabla + 1;
                                }

                                else {
                                    polje[i] = varijabla;
                                }
                                i++;
                            }

                        } while (i <= 1);

                        neki_string = red.substring(polje[0], polje[1]);
                        Tkolo.setText(neki_string);

                        provjera = "Dobitna kombinacija";
                        uvjet++;
                        continue;
                    }


                }
            }

            in.close();

            brojevi_string = brojevi.toString();

            data = brojevi_string.split("\n");

            return data;

        } finally {
            if (in != null) {
                try {
                    in.close();
                    return data;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}}

prevent the activity from loading twice


In the button's event listener, disable the button and show another activity.
    Button b = (Button) view;
    b.setEnabled(false);

    Intent i = new Intent(this, AnotherActitivty.class);
    startActivity(i);
Override onResume() to re-enable the button.
@Override
    protected void onResume() {
        super.onResume();

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setEnabled(true);
    }

Thursday, January 10, 2013

Android Colored Gradient Buttons Using XML



Now, this can be additional am fond of itwhen such a big amount of looking, I stumbled upon Folkert’s codewhereby he was ready to produce gradient buttons in robot victimisation simply XML. That’s right! XML! Ine'er liked having to form a coloured button in robot employing a nine patch image. it's a trouble.

ListView : Changing Selection Color

ListViews on the golemarea unit extensively used. we tend to see them in numerous shapes and sizes, withtotally different types of thingsvaried colours and fonts. I wouldn’t say these customizations area unittroublesome to code, however they are doing involve lots of bits and items. As you'll have complete, the technique is fairly distinctive and not therefore obvious. I’ll attempt to break it up into bits and items that area unit short and hopefully sweet.

This trick involves exploitation one thing called android:listSelector. primarily a listing selector is employed to specify the looks of the chosen list item (I’ve appear articles suggesting that they will even be wont tomodification the background color of individual items) which can be in one among the subsequent states:
  • android:state_focused
  • android:state_pressed
  • none of the above  

Friday, January 4, 2013

Android Adding Search Functionality to ListView

Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information he needs. In this tutorial i am discussing how to enable search filter to android ListView.


Thursday, January 3, 2013

Calculate average ARGB for Bitmap


int[] averageARGB(Bitmap pic) {
        int A, R, G, B;
        A = R = G = B = 0;
        int pixelColor;
        int width = pic.getWidth();
        int height = pic.getHeight();
        int size = width * height;
                
        for (int x = 0; x < width; ++x) {
                for (int y = 0; y < height; ++y) {
                        pixelColor = pic.getPixel(x, y);
                        A += Color.alpha(pixelColor);
                        R += Color.red(pixelColor);
                        G += Color.green(pixelColor);
                        B += Color.blue(pixelColor);
                }
        }
                
        A /= size;
        R /= size;
        G /= size;
        B /= size;
                
        int[] average = {A, R, G, B};
        return average;
                }

Get list of currently installed apps


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lView = (ListView) findViewById(R.id.list1);

    PackageManager pm = this.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);

    for (ResolveInfo rInfo : list) {
        results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
        Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
    }

    lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_ list_item_1, results));
}