Wednesday, April 24, 2013

Android Splitting Touch


1.) Create a new project by File-> New -> Android Project name it SplittingTouchExample.
2.) Create and write following into src\Cheeses.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.example.SplittingTouchExample;
 
 
public class Cheeses {
 
    public static final String[] sCheeseStrings = {
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
            "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
            "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
            "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
            "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss",
            "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
            "
, "Waterloo", "Weichkaese", "Wellington",
            "Wensleydale", "White Stilton", "Whitestone Farmhouse", "Wigmore", "Woodside Cabecou",
            "Xanadu", "Xynotyro", "Yarg Cornish", "Yarra Valley Pyramid", "Yorkshire Blue",
            "Zamorano", "Zanetti Grana Padano", "Zanetti Parmigiano Reggiano"
    };

3.) Write following into main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This activity demonstrates splitting touch events across multiple views within a view group."/>
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:splitMotionEvents="true">
        <ListView android:id="@+id/list1"
                  android:layout_width="0dip"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
        <ListView android:id="@+id/list2"
                  android:layout_width="0dip"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>
4.) Create and write following into values\arrays.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cheese_responses">
        <item>I\'m afraid we\'re fresh out.</item>
        <item>I\'m afraid we never have that at the end of the week, sir.  We get it fresh on Monday.</item>
        <item>Ah. It\'s been on order, sir, for two weeks.  I was expecting it this morning.</item>
        <item>Normally, sir, yes.  Today the van broke down.</item>
        <item>No.</item>
        <item>No.</item>
        <item>No.</item>
        <item>Yes, sir.  It\'s, ah ..... it\'s a bit runny.</item>
        <item>Well, it\'s very runny, actually, sir.</item>
        <item>I think it\'s a bit runnier than you\'ll like it, sir.</item>
        <item>Oh... The cat\'s eaten it.</item>
        <item>No.</item>
        <item>No.</item>
        <item>No.</item>
        <item>Mmm... cheese.</item>
    </string-array>
     
     
</resources>
5.) Write following into strings.xml:
1
<string name="split_touch_view_cheese_toast">Do you have any %1$s?\n%2$s</string>
6.) Run for output.
Steps:
1.) Create a project named SplittingTouchExample and set the information as stated in the image.
Build Target: Android 4.0
Application Name: SplittingTouchExample
Package Name: com. example. SplittingTouchExample
Activity Name: SplittingTouchExampleActivity
Min SDK Version: 14
2.) Open SplittingTouchExampleActivity.java file and write following code there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.example.SplittingTouchExample;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
public class SplittingTouchExampleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
        ListView list1 = (ListView) findViewById(R.id.list1);
        ListView list2 = (ListView) findViewById(R.id.list2);
        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings);
        list1.setAdapter(adapter);
        list2.setAdapter(adapter);
 
        list1.setOnItemClickListener(itemClickListener);
        list2.setOnItemClickListener(itemClickListener);
    }
 
    private int responseIndex = 0;
 
    private final OnItemClickListener itemClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String[] responses = getResources().getStringArray(R.array.cheese_responses);
            String response = responses[responseIndex++ % responses.length];
 
            String message = getResources().getString(R.string.split_touch_view_cheese_toast,
                    Cheeses.sCheeseStrings[position], response);
 
            Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
            toast.show();
        }
    };
}
3.) Compile and build the project.
Output

No comments:

Post a Comment