Lab 2 – Activity & Layout
KUAN-TING LAI 2020/9/28
Lab 2 Activity & Layout KUAN-TING LAI 2020/9/28 Concept of - - PowerPoint PPT Presentation
Lab 2 Activity & Layout KUAN-TING LAI 2020/9/28 Concept of Activities Activity shows the UI components One activity, one window (screen) Enables one app to invoke another app Use Intent to communicate An activity can
KUAN-TING LAI 2020/9/28
https://developer.android.com/guide/components/activities/intro-activities#java
<manifest ... > <application ... > <activity android:name=".MainActivity" /> ... </application ... > ... </manifest >
Ref: https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65
Top TextView FrameLayout + Bottom ListView
Select Text
<Lin Linea earLayout xm xmlns:android id=http://schemas.android id.com/apk/res/android android id:layout_width="match_parent" android id:layout_heigh ght="match_parent" android id:orientation="vertic ical" l" > </Lin Linea earLayout>
<Fr Fram ameLayou
an android id:la :layou
idth="match_parent" an android id:la :layou
an android id:la :layou
ight="1"> <Scr Scrol
lVie iew androi
_width="match_parent" androi
layout_h _heig ight="match_parent"> <TextVie iew androi
id="@+i +id/top
iew" androi
_parent" androi
layout_heig ight="wrap_content" androi
le/TextAppearance.AppCompat.Headli line" " /> </Scr Scrol
lVie iew> </ Fr FrameLa Layou
<Fr Fram ameLayou
an android id:la :layou
idth="match_parent" an android id:la :layou
an android id:la :layou
ight="1"> <Scr Scrol
lVie iew androi
_width="match_parent" androi
layout_h _heig ight="match_parent"> <TextVie iew androi
id="@+i +id/top
iew" androi
_parent" androi
layout_heig ight="wrap_content" androi
le/TextAppearance.AppCompat.Headli line" " /> </Scr Scrol
lVie iew> </ Fr FrameLa Layou
Layout_weight: Expand and fill all the free space 0px to let layout_weight decide height
<LinearLayout ... > ... <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:hint="@string/to" /> <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:hint="@string/message" /> ... </LinearLayout>
…. <Fr FrameLayout> …. </ </FrameLayout> > <Li ListVie iew an android id:id id="@+id/bottomListV tView" an android id:la layout_height="wrap_content" an android id:la layout_wid idth="match_parent"> </Li ListView> ….
<Lin LinearLayout xm xmlns: s:android="http://sc schemas. s.android.com/apk/res/ s/android" an android:layout_width="match_parent" an android:layout_height="match_parent" an android:orientation="vertical" > <Fr Fram ameLayout an android:layout_width="match_parent" an android:layout_height="0px" an android:layout_weight="1"> <Scr ScrollView an android:l :layout_w _width="match_parent" an android:l :layout_height="match_parent"> <Text xtView an android:id="@+i +id/topText xtView" an android:layout_width="match_parent" an android:layout_height="wrap_c _content" an android:textAppearance="@style/TextAppearance.A .AppCompat.Headline" " /> </Scr ScrollView> </Fr Fram ameLayout> <Lis ListView an android:id="@+id/bottomListView" an android:layout_height="wrap_content" an android:layout_width="match_parent"> </Lis ListView> </Line LinearLayout>
import … public class MainActivity extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.bottomListView); String[] values = new String[] { "Android List View", "Adapter implementation", "Simple List View In Android", "Create List View Android", "Android Example", "List View Source Code", "List View Array Adapter" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); listView.setAdapter(adapter); } }
… listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int itemPosition = position; String itemValue = (String) listView.getItemAtPosition(position); String posInfo = "Position :"+itemPosition+" ListItem : " +itemValue; // Show Alert Toast.makeText(getApplicationContext(), posInfo , Toast.LENGTH_LONG).show(); TextView topView = findViewById(R.id.topTextView); topView.setText(posInfo); } }); …