Tip:
Highlight text to annotate it
X
Greetings to all, it's a lesson 17, in which we will be adding
components on the screen directly from the application.
At the last lesson, we will create components in the method Activity.onCreate,
ie when creating the application.
In this tutorial, we will create is already in the running application.
Will create buttons - Buttons, because They clearly
all displayed. We will specify the text
that will be displayed on the button, and alignment:
left, center, or right. Will also provide an opportunity
delete the created elements.
Create the project.
Project name: P0171_DynamicLayout2
Let's create a screen that will help us to create View-components.
Open main.xml and write there the following:
A detailed discussion of the screen.
rgGravity - it RadioGroup, with three RadioButton (rbLeft, rbCenter, rbRight). This
component we use to select the alignment
component to create etName - text field here
will specify the text to be displayed
to create a component btnCreate - button that launches
the creation process. btnClear - button, erasing
Anything that creates llMain - vertical LinearLayout,
in which you will create the components
The screen is ready, let's write implementation. Open MainActivity.java.
To begin with, we describe and find all the necessary
We components. By the way, we have a couple of buttons,
we will be using, so they need a handler.
As a handler, assign Activity (ie the need to
finish: implements OnClickListener) and create an empty until
processing method onClick:
I also created a variable wrapContent and will be stored in
its value LinearLayout.LayoutParams.WRAP_CONTENT. I do it only to reduce
cumbersome code.
We now describe the process of creating a Button-component filling
method onClick:
Let us examine the writing. For a start, we check that
pressed btnCreate - ie button creation. Then
create LayoutParams with the height and width of the content.
Here I used a variable, about which he wrote up
- WrapContent. Otherwise, we would have quite cumbersome.
Next, create a variable btnGravity, by default,
write down the value of alignment LEFT. To determine which
RadioButton is selected at the moment, we use the method
getCheckedRadioButtonId - he returns to RadioGroup ID marked
RadioButton-a. We compare it with our three ID and skid
the corresponding value in the variable btnGravity. Throw off
this value to gravity at LayoutParams.
Next, create a button and assign it the text of
etName. Note that not enough to write
getText, because it will not give the text. It is also necessary to call
method toString. Well, in the end add the generated Button
our LinearLayout.
Keep all running the application. Add some
buttons.
The buttons appear with the specified alignment
and text.
When entering text, a keyboard appears below
and closes the review. To make it disappear, you should click
Button Back (Back) on the emulator or the ESC key on a conventional keyboard.
If the keyboard appears with Japanese characters,
Call the context menu for the input (long press
Left-click), click Input method and select from the list
Android Keyboard.
Remaining unsold button Clear, which is designed to
remove all created. To do this, we need to
add method onClick, add a switch (v.getId ()) is another case:
RemoveAllViews method removes all the child components of the View-
with our LinearLayout. With Toast displays a message
about success. Save then run and check it out. Add some
buttons, press the Clear button and see the result:
As a result, we have got a very dynamic
an application that is able to change itself.
That's all, and in the next lesson we will change
layout-parameters for the existing components of the screen. Everyone
bye!