Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Saturday, April 5, 2014

Simple check box list in Android


From this post I am going to describe a simple way to design a custom check box list for an Android application. I have presented

  • AllergyFood.java- Which is the activity that included the check box list
  • allergy_food.xml- Which is the layout file for check box list
  • strings.xml- Which is the resource file that used for extract the values for the check box list.

What I am doing here is cerating a custom check box list with the values from the string.xml. Then let the user to put checks for their prefered values. Finally all the selected values will be stored in a string array by a button click event.

  • User interface is looks like below image.

  • strings.xml file is included below string array.
<string-array name="allergy_foods">
      
        <item>Peanut</item>
        <item>Tree Nut</item>
        <item >Milk</item>
        <item>Egg</item>
        <item>Soy</item>
        <item >Wheat</item>
        <item>Fish</item>
        <item>Shell Fish</item>
        <item >Tomato</item>
        <item >Strawberry</item>
       
 </string-array>




  • allergy_food.xml Layout file for check box list  
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="332dp"
        android:layout_weight="3.38" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next Step" />




  •  AllergyFood.java Activity file that is included the check box list layout

import android.app.ListActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import android.content.Intent; 

public class AllergyFood extends ListActivity{

String checkedValues="";

  @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.allergy_food);
      
      setListAdapter(new ArrayAdapter<String>(this,          android.R.layout.simple_list_item_multiple_choice,getResources().getStringArray(R.array.allergy_foods)));
        
        Button nextStep=(Button) findViewById(R.id.button1);

        final ListView listView1=(ListView) findViewById(android.R.id.list);
        listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 


    listView1.setOnItemClickListener(new OnItemClickListener() {

      @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)

{

                int counter=0;
                checkedValues="";
       while(listView1.getCheckedItemPositions().size()>counter){                      if(listView1.getCheckedItemPositions().valueAt(counter)==true)

          {                                                                       checkedValues=checkedValues+"|"+listView1.getItemAtPosition(counter); 
                    }
                    counter++;
                }            
            }
           
        });

}

Monday, November 11, 2013

Debug your Android app without connecting the USB cable

It is annoying to debug an Android application with your device by connecting an USB cable. We are going to know a handy method to debug your app with your device via Wi-Fi without the USB. Let's know it step-by-step. I have done this successfully with Windows and Android 4.0.0.

No need to be ROOTED


1) You have to connect your device with the PC via Wi-Fi
If you have no wireless router and you need to connect your device with the PC via a Wi-Fi adhoc, then you have to convert your PC to a Wi-Fi hot-spot. There is a free and easy application for windows.(For Linux this will be not necessary)
http://www.mhotspot.com/downloadm.html

2) Follow the below Blog by LeoLink®
http://tech.leolink.net/2012/11/use-adb-wifi-without-rooting-for.html
(You have to change the directory of your CMD to "platform-tools" at "sdk" folder)

**Sometimes connection can be lost. Again check the IP address of the device and connect with
adb connect IP ADDRESS

A Simple method to connect your Android phone with PC localhost via Wi-Fi

Especially when we are developing Android applications, we may need to connect our Android application with our personal computer's local host for the testing purposes. The most convenient method to connect both devices is using Wi-Fi. But it is some what difficult to connect Android phones with a Wi-Fi enabled PC via a Ad-Hoc connection without using a separate software.
There is a more simple way to connect your Android phone and PC via Wi-Fi. To use that method your phone need to have the option called "Portable Hotspot" which is used to share your phone's internet connection with other devices via Wi-Fi.
Turn on the "Portable Hotspot" and connect your PC via Wi-Fi. Then open the Command Prompt of your PC and check the "Wireless LAN adapter wireless network connection" information using "ipconfig" command. Then identify the IPv4 address of it.
By using that IP address you can connect with PC's local-host through your Android phone. To check whether phone can access or not, just type the PC's IP address at your phone's web browser's address bar. If the connection is established successfully, you will be able to see welcome screen of your local-host.
Now, it is obvious that you can use that IP with your android applications to connect them with the PC's localhost.