50 likes | 186 Views
GUI Programming Fundamentals. < 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" > < TextView
E N D
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"<LinearLayoutxmlns: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"> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/up_button" > </Button> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/down_button" > </Button> <Button android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/left_button" > </Button> <Button android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/right_button" > </Button> </LinearLayout> At Build Time, the SDK creates a class that assigns integers to each element. packagecom.seetsoft.test; publicfinalclass R { publicstaticfinalclassattr { } publicstaticfinalclassdimen { /** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. */ publicstaticfinalintactivity_horizontal_margin=0x7f040000; publicstaticfinalintactivity_vertical_margin=0x7f040001; } publicstaticfinalclassdrawable { publicstaticfinalintic_launcher=0x7f020000; } publicstaticfinalclass id { publicstaticfinalintaction_settings=0x7f080005; publicstaticfinalintdown=0x7f080002; publicstaticfinalintleft=0x7f080003; publicstaticfinalintmsg=0x7f080000; publicstaticfinalintright=0x7f080004; publicstaticfinalintup=0x7f080001; } publicstaticfinalclass layout { publicstaticfinalintactivity_main=0x7f030000; } publicstaticfinalclass menu { publicstaticfinalintmain=0x7f070000; }
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"<LinearLayoutxmlns: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"> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/up_button" > </Button> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/down_button" > </Button> <Button android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/left_button" > </Button> <Button android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/right_button" > </Button> </LinearLayout> protectedvoidonCreate(Bundle savedInstanceState) { // etc setContentView(R.layout.activity_main); // etc } • At Run Time, • Activity: • setContentView(…) • Fragment: • View v = inflater.inflate(R.layout.pix_frag_layout, container, false); • causes • the XML layout file to be parsed • GUI objects to be created (inflated)
At Run Time, we access the inflated objects via the Resource class R. publicclassMainActivityextends Activity implementsOnClickListener { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button downButton = (Button)findViewById(R.id.down); downButton.setOnClickListener(newOnClickListener(){ publicvoidonClick(View view){ } }); } @Override publicbooleanonCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); returntrue; } publicvoidonClick(View view){ } }
Programming Considerations: • We cannot access GUI elements programmatically before they have been rendered. An attempt to do so will typically result in a null pointer exception. • We can, however, always access the Resource class R. The id values are created independently of the state of their existence.