- Android Application Development Cookbook(Second Edition)
- Rick Boyer Kyle Mew
- 466字
- 2021-07-09 19:36:22
Inserting a widget into a layout
As you may have seen from previous recipes, widgets are declared in a layout file, or created in code. For this recipe, we will go step-by-step to add a button with the Android Studio Designer. (For later recipes, we will just show the layout XML from the TextView.) After creating the button, we will create an onClickListener()
.
Getting ready
Start a new project in Android Studio and call it InsertWidget
. Use the default options for creating a Phone and Tablet project and select Empty Activity when prompted for the Activity Type. You can delete the default TextView (or leave it) as it will not be needed for this recipe.
How to do it...
To insert a widget into a layout, follow these steps:
- Open the activity_main.xml file in Android Studio and click on the Design tab.
- Find Button in the widget list and drag it to the center of the activity screen on the right. Android will automatically set the layout parameters based on where the button is dropped. If you center the button as shown in the screenshot, Android Studio will set those parameters in the XML.
- To view the
xml
created, click on the Text tab as shown in the following screenshot. See how the button is centered using theRelativeLayout
parameters. Also, take note of the default ID as we will need it for the next step. - Now, open the
MainActivity.java
file to edit the code. Add the following code to theonCreate()
method to set up theonClickListener()
:Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"Clicked",Toast.LENGTH_SHORT).show(); } });
- Run the application on a device or emulator.
How it works...
Creating the UI with the Android Studio is as simple as dragging and dropping Views. You can also edit the properties of the Views directly in the Design tab. Switching to the XML code is as simple as hitting the Text tab.
What we did here is very common in Android development—creating the UI in XML, then hooking up the UI components (Views) in the Java code. To reference a View from code, it must have a resource identifier associated with it. This is done using the id
parameter:
android:id="@+id/button"
Our onClickListener
function displays a pop-up message on the screen called Toast, when the button is pressed.
There's more...
Take a look again at the format of the identifier we created previously, @+id/button
. The @
specifies this is going to be a resource and the + sign indicates a new resource. (If we failed to include the plus sign, we would get a compile time error stating No resource matched the indicated name).
See also
- Butter Knife (Open Source Project)—Field and method binding for Android Views: http://jakewharton.github.io/butterknife/
- PostgreSQL for Data Architects
- Java Web基礎與實例教程(第2版·微課版)
- 用戶體驗增長:數字化·智能化·綠色化
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Serverless Web Applications with React and Firebase
- Instant jQuery Boilerplate for Plugins
- C語言程序設計實踐
- 基于GPU加速的計算機視覺編程:使用OpenCV和CUDA實時處理復雜圖像數據
- TypeScript圖形渲染實戰:2D架構設計與實現
- Python GUI Programming Cookbook(Second Edition)
- INSTANT LESS CSS Preprocessor How-to
- Oracle SOA Suite 12c Administrator's Guide
- Scala實用指南
- 三步學Python
- Getting Started with Hazelcast