Showing posts with label Beginning Android. Show all posts
Showing posts with label Beginning Android. Show all posts

Wednesday, February 20, 2019

Lesson 4– ListView

Right click to class name, New> Class, name it forth, finish.
Edit code to make it like this
public class forth extends Activity {
         
          @Override
          protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.ac4);

}
}
Ignore red mark at ac4, we create it now.
Right click to folder layout, New > Android XML File, name it ac4, finish.
Copy in to file ac4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"       
        android:divider="#ff8f40"
        android:dividerHeight="1px"
        android:lineSpacingExtra="-8dp"
        android:singleLine="true"
        android:textColor="#00F" >
    </ListView>
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="This is ListView"
        android:textColor="#800000"
        android:textSize="20sp" />
</LinearLayout>
Double click file AndroidManifest.xml to declare class forth, exactly it ‘s name.

Back to class forth.
First declare a ListView, array and adapter, find address.
String[] fru = { "Pear","Banana", "Cashew", "Orange", "Water melon", "Peach", "Grape", "Mango", "Plum" };
     private ArrayAdapter<String> adapter;
Screen now like this.

Add mode choice for List.
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Set adapter to show list.
          adapter = new ArrayAdapter<String>(forth.this,
                     android.R.layout.simple_list_item_checked, fru);
               lv.setOnItemClickListener(new OnItemClickListener() {

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

                     }
               });
               lv.setAdapter(adapter);
Press Ctrl+Shift+O to import library. Class forth now look like this.

Back to MainActivity change code in Next button to.
Intent in = new Intent(MainActivity.this,forth.class);                 
startActivity(in);
Run, press next, screen look like this.

Check to list to see green checkmark.
Add this in to setOnItemClickListener, we will show a toast when click list row.
int row = arg2;
Toast.makeText(forth.this, "You press the line "+row,
                                    Toast.LENGTH_SHORT).show();

Run and press first row, it show line 0, the first element of array, to see true index line, we add row=arg2+1.

In real app, we can do anything when click list row, for example, open a new screen.
To remove app name on top screen, add to above setContentView(R.layout.ac4);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Import library, run to see result.
Back to file ac4.xml, let look at android:textColor="#00F"

We color string to blue but why it still black? Because you can’t color string listview in file xml. We must use another way, see in topic about Listview.

Now you can go to make android app and code a real app.

Lesson 3– Spinner

Create a new class, name it third.
Right click to folder layout, New > Android XML File, name it ac3, finish.
Edit code to make it look like this.
public class third extends Activity {
         
          @Override
          protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.ac3);

}
}
Copy in to file ac3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Lunar year:"
        android:textColor="#800000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:textColor="#000080"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Choose year:"
        android:textColor="#800000"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/spin1"
        android:layout_width="72dp"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:background="#a1caf1"
        android:lineSpacingExtra="4dp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="41dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp" >

        <Button
            android:id="@+id/bu1"
            android:layout_width="65dp"
            android:layout_height="40dp"
            android:layout_marginLeft="4dp"
            android:text="Quit"
            android:textColor="#0000cd"
            android:textSize="14sp" />
    </LinearLayout>

</LinearLayout>
Double click to file AndroidManifest.xml to declare new class.

In class third, declare textView, Spinner, Button, find address.
Spinner show a list of data items, we can choose what we need, no need enter from keyboard.
Declare an array and adapter.
String[] data = { "2010", "2011", "2012", "2013", "2014",
               "2015", "2016", "2017", "2018", "2019", "2020"};  
private ArrayAdapter<String> adapter;
Import library.
Code now look like this.

Copy to below findViewById.
adapter = new ArrayAdapter<String>(third.this, android.R.layout.simple_spinner_item,data);
            sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

          @Override
     public void onItemSelected(AdapterView<?> parent, View view,
               int position, long id) {
          String year = sp.getSelectedItem().toString();
                    
                      
               }

               @Override
               public void onNothingSelected(AdapterView<?> arg0) {
                     // TODO Auto-generated method stub
               }
          });
     sp.setAdapter(adapter);
Here we set adapter for spinner to make it show list, class now look like this.
Back to MainActivity change code in Next button to.
Intent in = new Intent(MainActivity.this,third.class);                 
startActivity(in);
Run to see new class show spinner.
Copy function change year to above last close bracket.
public String lunaryear(int year){
          int remain=year%12;
          String lunar="";
          if(remain==1){
               lunar="Rooster";
          }
          else if(remain==2){
               lunar="Dog";
          }
          else if(remain==3){
               lunar="Pig";
          }
          else if(remain==4){
               lunar="Rat";
          }
          else if(remain==5){
               lunar="Cow";
          }
          else if(remain==6){
               lunar="Tiger";
          }
          else if(remain==7){
               lunar="Rabbit";
          }
          else if(remain==8){
               lunar="Dragon";
          }
          else if(remain==9){
               lunar ="Snake";
          }
          else if(remain==10){
               lunar="Horse";
          }
          else if(remain==11){
               lunar="Goat";
          }
          else {
               lunar="Monkey";
          }
          return lunar;
     }
In Spinner, we get string user choose by.
String year = sp.getSelectedItem().toString();
We cast it to number by this.
int year2 = Integer.parseInt(year);
No need try catch because our data not contain text inside.
After that, call function to change it to lunar year, set result to textView.
String lunar = lunaryear(year2);
tv.setText(lunar);

Run to see result.

Now we want spinner show year 2018 when open screen. Add this line to below setAdapter.
sp.setSelection(8);
Value 2018 is the eighth element, so we set i=7 because i=0 mean first element.
Code Quit button with command finish().

If our data array contain blank character in numbers, we can use trim() to remove them.
year=year.trim();
Change font size, text color in file ac3.xml  for more practical.

To change Hex code in line android:textColor="#0000cd" search Hex code color online.