Wednesday, 23 April 2014

Android AlertDialog example: Custom MultiChoiceItem with "Select All" option

If you want to set the MultiChoiceItem in your AlertDialog and you want to add "Select All" option, this is an example code for you.

I was aiming for this:



So, here it is: activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity" >  
   <Button  
     android:onClick="onDialog"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="Show Dialog" />  
 </LinearLayout>  

And this is our array.xml file in resources that we will use for our list:

 <?xml version="1.0" encoding="utf-8"?>  
 <resources>  
   <string-array name="items">  
     <item >Select all</item>  
     <item >First</item>  
     <item >Second</item>  
     <item >Third</item>  
     <item >Fourth</item>  
   </string-array>  
 </resources>  

And finally, MainActivity.java in which the magic happens. Main things are AlertDialog.Builder.setMultiChoiceItems(R.array.items, null, null) and listView.setOnItemClickListener(...), because these two things combined give us a possibility to have a "Select All" option.

 public class MainActivity extends Activity {  
     AlertDialog mDialog = null;  
     /**  
      * This becomes false when "Select All" is selected while deselecting some other item on list.  
      */  
     boolean selectAll = true;   
     /**  
      * Number of items in array list and eventually in ListView  
      */  
     int length;  
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
         length = getResources().getStringArray(R.array.items).length;  
     }  
     public void onDialog (View v) {  
         mDialog = onCreateDialog(null);          
         mDialog.show();  
         // we get the ListView from already shown dialog  
         final ListView listView = mDialog.getListView();  
         // ListView Item Click Listener that enables "Select all" choice  
         listView.setOnItemClickListener(new OnItemClickListener() {  
             @Override  
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
                 boolean isChecked = listView.isItemChecked(position);  
                 if (position == 0) {  
                     if(selectAll) {  
                         for (int i = 1; i < length; i++) { // we start with first element after "Select all" choice  
                             if (isChecked && !listView.isItemChecked(i)   
                                     || !isChecked && listView.isItemChecked(i)) {  
                                 listView.performItemClick(listView, i, 0);      
                             }  
                         }          
                     }  
                 } else {      
                     if (!isChecked && listView.isItemChecked(0)) {  
                         // if other item is unselected while "Select all" is selected, unselect "Select all"   
                         // false, performItemClick, true is a must in order for this code to work  
                         selectAll = false;  
                         listView.performItemClick(listView, 0, 0);          
                         selectAll = true;  
                     }  
                 }  
             }  
         });  
     }  
     public AlertDialog onCreateDialog(Bundle savedInstanceState) {  
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
       // Set the dialog title  
         builder.setTitle(R.string.dialog)  
             // Specify the list array, the items to be selected by default (null for none),  
             // and the listener is null in order to "Select all" choice to work  
             .setMultiChoiceItems(R.array.items, null, null)  
             // Set the action buttons  
         .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {  
             @Override  
             public void onClick(DialogInterface dialog, int id) {  
               // User clicked OK, so save something here  
              }  
           })  
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {  
              @Override  
              public void onClick(DialogInterface dialog, int id) {}  
           });  
       return builder.create();  
     }  
 }  

Hope you've enjoyed in this code example!

Cheers!

Monday, 7 April 2014

Hustle, Outsource, Enjoy, Repeat!

Hi,

have you ever had those days where you were almost 100% productive and worked from 8 am till 11 pm with just launch breaks? This is what my past few days are pretty much alike. How will this create a freedom for me? 

Recently, I started applying to every job that was close to my knowledge (Android) and wishes to improve (seller). Android developer, Sales guy, Google AdWords guy, full-time, part-time, project oriented jobs, etc. I sent over 20 job applications in less than 20 days (that was my goal). Still, I apply on almost every invitation I get on oDesk daily (because, not every invitation means arranged job :) ). Click on a link if you wanna see my profile. 

I am currently developing a bluetooth app for a client on oDesk, selling IT services for my friend, successfully arranging 3 AdWords campaigns for clients and giving private lessons for high school students. Hope to get more work soon! Why? Let me answer the question finally! Because, when you fill up your calendar to the point where you can't breathe (and this is happening constantly, not temporarily), you can than think about eliminating your hustle with outsourcing. And this is why I want more work. But, don't outsource prematurely, because you need to know your vision and than actively pass it forward to your employees.

Hope you enjoyed reading this. Let's fill up our calendars! ;)

Hustle, outsource, enjoy and repeat!

Thursday, 20 March 2014

Early Bird

Hi,

how are you today?

After a very productive day I can just say: "Good. Let's do it better next time!" I had 63% productive time out of 16h logged time (stats at RescueTime - really nice app that measures everything you do on a computer or a smartphone and categorizes it as productive, neutral or distracting time).
Surprisingly, I woke up at 5:30 A.M. without alarm and started with 1h Android Development for references. And with all this said, you guess it right! I had a great productive day all because of going to sleep pretty early at 0:30 A.M. and waking up naturally at 5:30 A.M.. I am interested, when do you wake up and how long do you sleep? So, I am trying to stabilize my sleeping cycle for about a month and it is going somewhere slowly. I am not going out on weekends, going to sleep before 1 A.M. everyday and doing enough exercise daily.
These are some of the things I did today:

  • 2 hours of Android Development for new job referrals 
  • in 6 hours of calling and selling IT services at Evorion Mediji j.d.o.o. I did more than 40 dials, 15 successful calls and 1 close

I recommend you to test your productivity levels with RescueTime, great tool that helped me alongside with Google Calendar. Do you have some other suggestions?

Feel free to like, share and comment!
Enjoy!

Wednesday, 19 March 2014

Intro

Hi,

My name is Gabriel GirĨenko and at the point of writing this I am 22 years old, collage dropout and an adrenaline addict. Most of the things I do, I do it for the wealth freedom of my family and of coarse to feel adrenaline pumping my veins as much as possible.

On this blog you will find some of my views on building wealth categorized in sales, android development, entrepreneurship and hobby(my band). Hope, we will learn together and get out of our comfort zone.

Some of my role models are Buddha, Bill Gates, Grant Cardone, Jordan Belfort and others.

Enjoy!