Shalvin Interests

Sunday, May 22, 2016

Filling Android ListView and Spinner with Array data



<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="Expense">
        <item>Chilly Chicken</item>
        <item>Tea</item>
        <item>Yipee Noodles</item>
        <item>Tea</item>
    </string-array>
</resources>


Here I am creating an xml file called Expense.xml inside values folder.

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expense_details);

        String[] expense = getResources().getStringArray(R.array.Expense);

        ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, expense);

        Spinner spExpense = (Spinner)findViewById(R.id.spExpense);
        spExpense.setAdapter(Adapter);
    }
}

An array called expense is created which contains data from Expense.xml.
That data is bound to Spinner control.



String[] expense = getResources().getStringArray(R.array.Expense);
        ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, expense );

        ListView lv = (ListView)findViewById(R.id.lvwExpense);
        lv.setAdapter(Adapter);

Here I am using ListView.


Wednesday, May 4, 2016

Android Development with Android Studio Part 1 : Views

Android Studio is the preferred IDE for writing Android Apps. It was announced in Google IO 2013. There are other options like Eclipse, IntelliJ Idea, etc. Android Studio is based on IntelliJ Idea Community Edition.

 In this  blog In will be concentrating on Android  Studio.



Next screen will ask you to select Android SDK.

In  the next screen you can select an Activity. Here I am starting with Basic Activity. Later on we will see other Activities like Google Maps Activity.


In the next screen you can specify a name for your activity.



Then the IDE will appear.

On clicking the Preview option you can see the preview.



Views and ViewGroups

View is a rectangular area in Angular. Examples of Views are TextView, Button, ImageView, etc. Views are defined in XML.

ViewGroup is a grouping of Views.  Popular ViewGroups are LinearLayout, etc.

Here I have changed the ViewGroup to LinearLayout and its orientation to vertical.





wrap_content


<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shalvin P D Blog : Shalvinpd.blogspot.com Interests : Microsoft .Net, SharePoint, AngularJS, Android"
        android:background="@android:color/holo_blue_bright" />


layout_width


<TextView
        android:text="Shalvin P D Blog :shalvinpd.blogspot.com"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="140dp"
        android:layout_height="50dp"/>

layout_width is defined in dp which stands for Density Independent Pixel. So that the unit is the same irrespectity of the pixel density of the Android device.





textSize



<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shalvin P D Blog : Shalvinpd.blogspot.com Interests : Microsoft .Net, SharePoint, AngularJS, Android"
        android:background="@android:color/holo_blue_bright"
        android:textSize="32sp"/>

The textSize is specified using sp. sp stands for Scale Independent Pixel and  is used only for Fonts. The concept is similar to dp.


Event Handling


<?xml version="1.0" encoding="utf-8"?>
<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"
   android:orientation="vertical"
    tools:context="com.blogspot.shalvinpd.helloevents.MainActivity">

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="40sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:id="@+id/btnHello"
        android:onClick="btnHello_Click"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/textView" />
</LinearLayout>

 public void btnHello_Click(View view)
    {
        EditText et = (EditText)(findViewById(R.id.etName));
        Toast.makeText(this, "Hello " + et.getText().toString(), Toast.LENGTH_SHORT).show();
    }