Casting result of findViewById is redundant [duplicate] - android

This question already has answers here:
No need to cast the result of findViewById?
(6 answers)
Closed 5 years ago.
I'm new to Android and I'm working through some exercises on using various views. One such example is:
TextView messageView = (TextView) findViewById(R.id.message);
My question is this: what would be the benefit of casting TextView? My IDE tells me that casting the method is redundant. Are there any use cases where I would want to cast in this way?

Before API level 26, the method findViewById returned the reference of View class. So you needed to cast it.
//old signature
public View findViewById(int id){
//
}
But starting from API level 26, it has been updated and it returns subclass of View using template so that you can assign the returned reference without casting.
//new signature
public <T extends View > T findViewById(int id){
//
}
The example which you referred used the older API level while building the project, so you can see the casting there. It was compulsory earlier but is not necessary now. So you are getting the warning.

Related

setActionView deprecated [duplicate]

This question already has an answer here:
How to cast MenuItem to LinearLayout
(1 answer)
Closed 3 years ago.
Bellow code works fine but it gives me setActionView and getActionView is deprecated
val item = menu?.findItem(R.id.action_mini_basket)
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout)
val toolbarLayout = MenuItemCompat.getActionView(item)
android developer site says " This method was deprecated in API level 26.1.0. Use setActionView(int) directly. "
I managed to fix getActionView but have no idea about setActionView it only takes one argument.
val item = menu?.findItem(R.id.action_mini_basket)
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout) //??
val toolbarLayout = item.actionView as RelativeLayout
thanks
change
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout);
to
item.setActionView(R.layout.toolbar_mini_basket_layout);
Compat version isn't needed anymore if you are using AppCompatActivity
According to the Android documentation you indeed need to use setActionView(int).
In this case the parameter is the layout resource you want to use.
So it will be:
MenuItemCompat.setActionView(R.layout.toolbar_mini_basket_layout)

how is an object created?

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.

when i pass a variable from the main method, my app crash [duplicate]

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.

Adding Custom ImageButton to layout in Android causes error

I will start by saying this, while I have some Java training, is my first foray into development for Android.
What I have done is created a custom ImageButton called MapCell that is basically an ImageButton that holds a few extra pieces of information, and it compiles fine.
My problem comes when I want to procedurally create a MapCell in the relevant Activity and add it to my TableLayout which is defined in the xml and has the id 'mapTable'. The relevant bit looks like this:
Random randy = new Random();
MapCell n = new MapCell(randy.nextInt(4), this); //the random number is part of my extra info
findViewById(R.id.mapTable).addView((View)n, 20, 20); //add cell to display
I get one error out of that:
The method addView(View, int, int) is undefined for the type View
Which to me sounds like utter nonsense. I put that View cast in there as desperation after I got this same error with n sitting by itself and nothing changed (Obviously my MapCell is already a View since it extends ImageButton).
I hope a new pair of eyes can tell me what this is about, since I've checked for similar problems and I didn't find any quite like this. Let me now if you need to see more code.
The method findViewById returns a View and the View class doesn't have the method addView(this method is implemented in the ViewGroup and its subclasses). Instead you should write:
((TableLayout)findViewById(R.id.mapTable)).addView(n, 20, 20);
I've cast the return of the findViewById method in a class that actually has the addView method.
You got this problem because method findViewById(R.id.mapTable) returns View object.
In android you can't add one View to another.
You can use addView function with ViewGroup, and all LinearLayout (etc.) objects.

How can a custom view get access to its activity? [duplicate]

This question already has answers here:
How to get hosting Activity from a view?
(8 answers)
Closed 6 years ago.
I have a custom view and would like to access a String that is available in its activity. I've seen code that uses getContext() in the view class, but there is no method to access the String that has been made available to its activity via an intent. How to make a String in an activity available to its custom view?
The getContext() method in the View class returns the context that was passed on its constructor. Usually that's the Activity you want (Activity extends Context).
So this probably works for you:
Java:
((Activity)getContext()).someMethod(...);
Kotlin:
(context as? Activity)?.someMethod(...)
I'm a noob to java and android still, so this may not work, but I'm just trying to help.
You should be able to pass in parameters to intents/activities using "putExtra" and "getExtra", for example:
In your main activity:
Intent EditDebtActivity = new Intent(getBaseContext(), EditDebt.class);
EditDebtActivity.putExtra(DbAdapter.KEY_ROWID,dRowID);
EditDebtActivity.putExtra(DbAdapter.KEY_DEBT, dName);
EditDebtActivity.putExtra(DbAdapter.KEY_STARTINGAMOUNT, dStartAmount);
EditDebtActivity.putExtra(DbAdapter.KEY_CURRENTAMOUNT, dCurrentAmount);
EditDebtActivity.putExtra(DbAdapter.KEY_DUEDATE, dDueDate);
EditDebtActivity.putExtra(DbAdapter.KEY_INTERESTRATE, dInterestRate);
EditDebtActivity.putExtra(DbAdapter.KEY_MINPAYMENT, dMinPayment);
startActivity(EditDebtActivity);
Then in the "onCreate" method in your new view, use the following:
Bundle extras = getIntent().getExtras();
dRowID = extras.getLong(DbAdapter.KEY_ROWID);
String rowidname = extras.getString(DbAdapter.KEY_DEBT);
currentamount = extras.getDouble(DbAdapter.KEY_CURRENTAMOUNT);
startingamount = extras.getDouble(DbAdapter.KEY_STARTINGAMOUNT);
duedate = extras.getInt(DbAdapter.KEY_DUEDATE);
obviously I've used my own code here, but I am passing row information into a new activity when a user clicks a button.
See this SO link for more info on using putextra and getextra: Sending arrays with Intent.putExtra
Hope this helps, it's my first contribution back to the community :)

Categories

Resources