The Android findViewById Function PRO

Most Android developers are fairly well familiar with the findViewById function our activity inherits from the Activity class. This function receives the ID number of a specific View resource the activity user interface includes.

If you ever try to call that function before calling the setContentView function (our activity inherits from the Activity class) then you must already know that an exception will be thrown. That exception is thrown because as long as we don’t call the setContentView function our activity knows nothing about the very specific view we refer its ID.

When creating a custom dialog based on xml layout document that describes its user interface we must call the findViewById function that was defined in View. We must invoke it on the very specific View object our code inflates from the xml layout document. Calling the findViewById function that was defined in Activity won’t get us the very specific view we want. The activity is not connected with that view.

The following code sample shows a small demo for creating a popup dialog that has its own customized user interface.

package com.abelski.samples;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DialogActivitySample extends Activity {
	private EditText tf1 = null; 
	private EditText tf2 = null;
	private EditText tf3 = null;
	private Button bt = null;
	private EditText tfDialog;
	public void setNum(double number)
	{
		tf2.setText(String.valueOf(number));
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);   
		setContentView(R.layout.main);
		tf1 = (EditText) findViewById(R.id.editText1);
		tf2 = (EditText) findViewById(R.id.editText2);
		bt = (Button) findViewById(R.id.button1);
		tf3 = (EditText) findViewById(R.id.editText3);
		LayoutInflater li = LayoutInflater.from(this);
		View view = li.inflate(R.layout.promptdialoglayout, null);
		tfDialog = (EditText)view.findViewById(R.id.editTextDialog); 
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("d i a l o g");
		builder.setView(view);
		android.content.DialogInterface.OnClickListener listener = new android.content.DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				double num = Double.parseDouble(tfDialog.getText().toString());
				setNum(num);
			}

		};
		builder.setPositiveButton("Set", listener);
		bt.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {  
				// TODO Auto-generated method stub
				double num1 = Double.parseDouble(tf1.getText().toString());
				double num2 = Double.parseDouble(tf2.getText().toString());
				if (num2 == 0) {
					AlertDialog dialog = builder.create();
					dialog.show();
				} else {
					tf3.setText(String.valueOf((num1 / num2)));
				}
			}

		});
	}
}

 

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences