This way we have defined few TextViews for displaying the text and an EditText to take input from the user. Now we can programmatically call the layout and use the components to ask user for an input and upon selecting the button he gets a response. The following code snippet for DemoAndroidApp shows how, using the setContentView() method, we call upon the layout defined in main.xml and also how components like EditBox are being called upon by referring their ids as defined in main.xml file.
public class DemoAndroidApp extends Activity { private EditText inputbox; private TextView ans; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); inputbox = (EditText) findViewById(R.id.editor); inputbox.setText(""); ans = (TextView) findViewById(R.id.ans);
final Button button = (Button) findViewById(R.id.back); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { ans.setText("Welcome!! "+inputbox.getText()); inputbox.setText(""); } }); } }
We associate a listener to the button so that when the user clicks on the button he gets a welcome message as a response. Now we can run and execute the application.
Executing on Android emulator Once you are done with coding, you can start running your application by selecting Run > open Debug Dialog. In the following Debug window, from the left pane, select the Android application and a new launcher entry appears with the name New_Configuration. Change the name of launcher to demoApp, and then under Android tab browse and select the current project. From the Activity option select the activity, which in our case is DemoAndroidApp. On pressing the Run button, the debugger starts the Android emulator, on which the application gets executed. This way we have run a simple app where we defined the UI. You can refer the Android's documentation for more sample applications and other APIs. The complete source code for this project can be found on PCQ Forums under Current Issue thread.
Integrate your work force and give a fight to your Competition.
Network with 2000 software professionals and create evangelists for your products.
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.