Wife learns android creating views in code
Wife learns android creating views in code reviews the work June completed from assignment for creating an Android TextView & EditView and positioning in code.
Get Code
The code can be found here https://github.com/mobapptuts/androidwife-layouts.git
Or else you can run this command
git clone https://github.com/mobapptuts/androidwife-layouts.git
Code Sample
TextView juneView = new TextView(this); RelativeLayout.LayoutParams juneTextParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); juneTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL); juneTextParams.addRule(RelativeLayout.ABOVE, R.id.nigels_button); juneTextParams.setMargins(0, 0, 0, 108); juneView.setLayoutParams(juneTextParams); juneView.setText("Junes Views"); relativeLayout.addView(juneView); EditText juneEditText = new EditText(this); RelativeLayout.LayoutParams juneEditTextParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); juneEditTextParams.addRule(RelativeLayout.ABOVE, R.id.june_button); juneEditTextParams.addRule(RelativeLayout.ALIGN_START, R.id.june_button); juneEditText.setLayoutParams(juneEditTextParams); juneEditText.setHint("Testing Junes layout views"); relativeLayout.addView(juneEditText);
Code Description
First the TextView
Create a TextView & Layout Parameters
TextView juneView = new TextView(this); RelativeLayout.LayoutParams juneTextParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
Add positioning to the layout parameters
juneTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL); juneTextParams.addRule(RelativeLayout.ABOVE, R.id.nigels_button); juneTextParams.setMargins(0, 0, 0, 108);
Set the layout parameters & text to the view
juneView.setLayoutParams(juneTextParams); juneView.setText("Junes Views");
Add the view to the layout
relativeLayout.addView(juneView);
Now the EditText view
Create the EditText & LayoutParams objects
EditText juneEditText = new EditText(this); RelativeLayout.LayoutParams juneEditTextParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
Set the layout positioning
juneEditTextParams.addRule(RelativeLayout.ABOVE, R.id.june_button); juneEditTextParams.addRule(RelativeLayout.ALIGN_START, R.id.june_button);
Set the layout parameters & hint for the view
juneEditText.setLayoutParams(juneEditTextParams); juneEditText.setHint("Testing Junes layout views");
Add the EditText to the RelativeLayout
relativeLayout.addView(juneEditText);