Professional Documents
Culture Documents
Beginner Android Part 2
Beginner Android Part 2
A Text field allows the user to type text into your app.
It can be either single line or multi-line. Touching a text filed places the cursor and automatically displays the keyboard.
In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data
look-up via auto-completion.
You can specify the type of keyboard you want for your EditText object with the andoid:inputType attribute. For Example,
if you want the user to input a Phone number, you should use the phone input type.
There are several different input types available for different situations. Here are some of the
common values for android:inputType:
Wrap content
The component just want to display
big enough to enclose its content only.
Both layout width and height can set
Fill parent
Width will be adjusted to height of activity
https://www.youtube.com/
watch?v=zhszwkcay2A
A device-independent pixel (also: density-independent pixel, dip, dp) is a physical unit of measurement based on a
coordinate system held by a computer and represents an abstraction of a pixel for use by an application that an underlying
system then converts to physical pixels.
A typical use is to allow mobile device software to scale the display of information and user interaction to different screen
sizes.
The abstraction allows an application to work in pixels as a measurement, while the underlying graphics system converts the
abstract pixel measurements of the application into real pixel measurements appropriate to the particular device.
As dp is a physical unit it has an absolute value which can be measured in traditional units, e.g. for Android devices 1 dp equals
1/160 of inch or 0.15875 mm.
The Android system scales these for the current screen density and the user’s system-wide
Scaling factor for text selected in the settings app.
Dimensions in Android
Formatting Text
TEXT VIEW : Text and ID for further Reference is important
* Call this method Buttonclick In the onClick event in property window of button
On click of a button display the content typed in the text field as message using toast
Toast.makeText(MainActivity.this,t1.getText(),Toast.LENGTH_LONG).show();
}
Events are a useful way to collect data about a user's interaction with
interactive components of Applications.
Like button presses or screen touch etc. The Android framework maintains an
event queue as first-in, first-out (FIFO) basis. You can capture these events in
your program and take appropriate action as per requirements.
There are following three concepts related to Android Event Management −
Event Listeners − An event listener is an interface in the View class that contains
a single callback method. These methods will be called by the Android
framework when the View to which the listener has been registered is triggered
by user interaction with the item in the UI.
Though there are several tricky ways to register your event listener
for any event, only top 3 ways are listed here, out of which you can
use any of them based on the situation.
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView =(TextView)findViewById(R.id.textView);
txtView.setTextSize(25);
}
}
);
}
}
Method 2 – Using Layout file activity_main.xml
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ButtonClick(View v) {
// declaration for the edit text element
EditText t1 = (EditText)findViewById(R.id.editText);
t1.setTextSize(25);
}
Checkbox Class
The android.widget.CheckBox class provides the facility of creating
the CheckBoxes.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layiout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
// cast all checkbox and button
check1=(CheckBox)findViewById(R.id.checkBox_ic);
check2=(CheckBox)findViewById(R.id.checkBox_ch);
check3=(CheckBox)findViewById(R.id.checkBox_fr);
button_sel=(Button)findViewById(R.id.button);
Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH
_LONG).show();
}
}
);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onclickListenerButton(); // calling the function
}
public void onclickListenerButton(){
//casting the radiobutton and button
radio_g=(RadioGroup)findViewById(R.id.RG);
button_sub=(Button)findViewById(R.id.b1);
button_sub.setOnClickListener(
new View.OnClickListener() {
@Override
<ImageView
android:src="@drawable/cat"
android:layout_width=“wrap_content"
android:layout_height=“wrap_content"
android:id="@+id/imageView"
android:scaleType="centerCrop"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="75dp" />
Android provides a wide variety of controls you can use in your UI,
such as buttons, text fields, seek bars, checkboxes, zoon buttons,
toggle buttons and many more.
<TextView android:id="@+id/text"
android:layout_width="wrap_content“
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button" />
</LinearLayout>
Buttons
Android buttons are that GUI components which are sensible to taps (click) by the User.
Buttons:
A standard Android button with text is represented by android Class
android.widget.Button
Button instance can be inserted into GUI either through .XML file or programmatically in
.java file.
android:text="I am a Button“ – Defines the text which the UI element should display
Adding a button programmatically:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
1 Create the instance of the Button class by passing the current context as this.
2 Set the text which the button should display.
3 Create the object of the ViewGroup (Layout) in which the button needs to be placed.
4 Find the layout by using findViewByid() method.
5 Add the button to the layout by addView function
* You can also add an image with button by using android:drawableLeft property.
Try Yourself
For example, an activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can send them to the Android system
defining the components you are targeting.
For example, via the startActivity() method you can define that the intent should be used to start an
activity.
An intent can contain data via a Bundle. This data can be used by the receiving component.
In Android the reuse of other application components is a concept known as task. An application can access other
Android components to achieve a task.
For example, from a component of your application you can trigger another component in the Android system, which
manages photos, even if this component is not part of your application. In this component you select a photo and
return to your application to use the selected photo.
The following code demonstrates how you can start another activity via an intent.
Activities which are started by other Android activities are called sub-activities. This wording makes it easier
to describe which activity is meant.
To start a services via intents, use the startService(Intent) method call.
There are two types of intents in android: implicit and explicit.
Implicit Intent
Implicit Intent doesn't specify the component. In such case, intent provides information of available
components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.vinay.in"));
startActivity(intent);
Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
Sending out explicit or implicit intents
Android supports explicit and implicit intents.
An application can define the target component directly in the intent (explicit intent) or ask the Android
system to evaluate registered components based on the intent data(implicit intents).
Explicit intents explicitly define the component which should be called by the Android system, by using the
Java class as identifier.
Explicit intents are typically used within an application as the classes in an application are controlled by the
application developer. The following shows how to create an explicit intent and send it to the Android
system to start an activity.
If only one component is found, Android starts this component directly. If several components are identified by the
Android system, the user will get a selection dialog and can decide which component should be used for the intent.
For example, the following tells the Android system to view a webpage.
All installed web browsers should be registered to the corresponding intent data via an intent filter.
Your application can register to system events, e.g., a new email has arrived, system boot is complete
or a phone call is received and react accordingly.
Try it yourself with sample code