Every Screen needs to have an Activity associated with it. the activity’s oncreate() method is called when the activity comes into existence. The UI for the screen should be defined in the layout xml and may be initialized in the oncreate() method. The following sections describe the Activity and some Custom Controls in detail.
1. Activity
The Screen content should be loaded from the layout xml inside the Activity’s oncreate() method.. Each Activity is a separate class and are listed in the application manifest file. The launcher activity is called when the application is launched
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".activity.MathUActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
2. Custom Controls
2.1. Custom keypad
To create a custom number keypad with 2 rows, follow the code below
public class MTKeyPad extends TableLayout { public static final int KEYS_PER_ROW = 5; private OnClickListener clickListener; private OnLongClickListener longClickListener; private OnNewListener newListener; public MTKeyPad(View p) { super(p.getContext()); parent = p; initialize(); } protected void initialize(){ TableRow row = null; int i, id; Button button = null; overWrite = false; createListener(); DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); //int width = (int)(displayMetrics.widthPixels / 7.5); int width = (int)(displayMetrics.widthPixels / 8.0); TableRow.LayoutParams layoutParams = new TableRow.LayoutParams( width, TableRow.LayoutParams.MATCH_PARENT); layoutParams.setMargins(0, 0, -4,-4); for(i=1; iif(i%KEYS_PER_ROW == 1){ row = new TableRow(getContext()); addView(row); } //inflate(getContext(), R.layout.key, row); //button = (Button)row.getChildAt(j); button = (Button)inflate(getContext(), R.layout.key, null); row.addView(button, layoutParams); id = i == 10 ? 0 : i; button.setText(Integer.toString(id)); button.setId(id); button.setOnClickListener(clickListener); } }
2.2. Toast
Toast are popups for displaying statuses or messages within the application for a momentary period.
ndroid.widget.Toast
The show method will display the toast
make sure to call the view and Duration
toast.setView(layout); toast.setDuration(Toast.LENGTH_SHORT); toast.show();
2.3. Focus Indicator
A border can be set when a Textview receives focus, please see the code below
public class MTFocusChangeListener implements OnFocusChangeListener { private int color; public MTFocusChangeListener(int c){ color = c; } public MTFocusChangeListener(){ this(Color.WHITE); } public void onFocusChange(View v, boolean hasFocus) { TextView tv = (TextView)v; if(hasFocus){ //tv.setBackgroundColor(Color.CYAN); tv.setBackgroundResource(R.drawable.border); } else{ //tv.setBackgroundResource(0); tv.setBackgroundColor(color); } }