This question already has answers here:
How does Java Object casting work behind the scene? [duplicate]
(5 answers)
Closed 7 years ago.
(I am new to android programming) I've seen this piece of code which assigns a predefined button to variable b:
Button b = (Button) findViewById(R.id.button1);
It's all clear for me (to me?), but I just don't get one thing: what is that (Button)?!
(Button) is a typecast. Every widget that comes back from findViewById is a View. To treat it as a button, you must explicitly tell the compiler that it is a Button.
More information on findViewById here, in the Android documentation:
http://developer.android.com/reference/android/app/Activity.html
when you are calling findViewByID(...) it returns a View type. Button is a child class of View and by saying (Button) you are type casting it which will allow you to use the methods/functions in the Button class.
Related
So this is my first time using Kotlin and Android studio and I really got to say thus far my experience has been horrible. No other language or IDE has put me in the position where I had to do more than hours research to try and make a simple button load the next page and then still being unable to do it.
This is my Login.kt page
and there you can see its having problems with the findviewbyID and setonlclicklistener.
Top of my Activity_login.xml page
The button im trying to create the onclick event for.
Please keep in mind I did try just the normal creation of setonclick for the button but then I didnt seem to recognize that I have a button with the ID of login_btn
change the init button to
val button=findViewById<Button>(R.id.login_btn)
in my case i have removed onclick from xml.
val button : Button = findViewById(R.id.login_btn);
button.setOnClickListener {
}
If no answers above can help you, you can use viewbinding to access your view. I had so many problems while using findViewById as a newbie. Here is the documentation,
https://developer.android.com/topic/libraries/view-binding
EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();
Hi im new to android programming an need a little help. :) I just want to clarify if text1 is an object? Because it can call a method. But if text1 is an object how come that there is no "new" keyword. Thanks in advance for any response. :)
It is not necessary that all variables do initialization with new keyword.
Like if you write
String s = "";
Your String has been initialized without new keyword.
Same like this EditText is provided initialization in findViewById(). Here findViewById returned Edittext instance.
I suggest you complete Java Tutorial before continue work on Android. Because Android is based on Java language.
EditText is derived from the Super class View. Here findViewById method is returning an an object of the View class. You are explicitly typecasting it to EditText and assigning it to text1. So new is not required. It is being managed in findViewById method. Alternatively you can do this as:
EditText text1;
text1 = new EditText(An instance of Context); //Create an object of Edittext class
Now do whatever with this object text1.
It's me 2 years after, Now I want to answer your question bud. I know you just started and had a dream of creating apps that will be used by others, and guess what? You already achieved that and you can now also create apps not just for android but for IOS since you're using flutter now, you also have your own account on google play store now. Listen Bud, on the first line you declare what type is the text1. on the second line that's where you declare the text1 as an object now, then on the third line you are then using that object's capabilities like getting text. You've learned so much in this journey, and still learning not just in programming but in life.
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
im using android studio and im making an app but i have this problem when i pass the variable from my main to the class, my app crash.
its show me a massage that the app has stopped
this is how i pass the variable
bravo b = new bravo();
b.updatetext(Correctcounter);
and this is my method in the class
public void updatetext(int x) {
TextView scoreView = (TextView)findViewById(R.id.score);
scoreView.setText(""+x);
}
You can't access views from classes that are not UI.
You should never access a view from a custom class.
Views must be accessed from Activity or Fragment!
Change your logic to match this requirement.
I can't understand the logic behind the following code in android studio when we create an instance of a Button:
Button btn = (Button) findViewById(R.id.btnRegister);
From what I know an instance of a class is defined like:
MyClass myInstance = new MyClass(MyParameter)
If we apply the above logic the button code should be like this:
Button btn = new Button(findViewById(R.id.btnRegister));
But in fact the above code does not work. Why is "button" code so complicated to understand and does not relate to the code of a mere class? What does "(Button)" in brackets mean in the above example????
Thank you
The find view line doesn't create a button. Your views are all created using the standard new syntax by the framework when you call setContentView. The setContentView function parses your xml and creates the appropriate classes. What findViewById does is search the views that it created in setContentView checking the ids for one with a matching id and returns it.
As for what (Button) does- its a standard Java cast. findViewById returns a View object. But some subclasses of View have advanced functionality not found on view. To use them, you have to have an object of the correct type. So you cast it to the correct type (which will throw an exception if it isn't the right type). For example you need to have a TextView to call setText, a normal View doesn't have that function.
This question already has answers here:
Correct method for setKeepScreenOn / FLAG_KEEP_SCREEN_ON
(5 answers)
Closed 9 years ago.
I try to set the screen to always on however I cant figure out how to do it within a fragment. I have tried to get access to the ViewPager but it returns with a null value. I want the screen to be on for the whole application but it should be able to be changed by the user within the settings of the app.
Here is my code from within my fragment:
private void setIsAlwaysOn(boolean b)
{
ViewPager pager = (ViewPager)getView().findViewById(R.id.pager);
pager.setKeepScreenOn(b);
sharePropertiesEditor.putBoolean(sp_alwaysOn, b);
sharePropertiesEditor.commit();
}
I get a nullpointerexception at line 4 of the visible code above.
Yeah. Can't you use this?
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
For Kotlin, use:
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)