Why difference LayoutInflater cause difference SearchView Icon - java

Hello every Android geek, i has using LayoutInflater to customer My Android ActionBar.
but i found that difference LayoutInflater will cause generate difference SearchView Icon
This is my Difference LayoutInflater and capture image
LayoutInflater mInflater1 =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//You can change mInflater1 =LayoutInflater.from(this); is a same result
LayoutInflater mInflater2 =LayoutInflater.from(getApplicationContext());
My Question is Why difference LayoutInflater cause difference SearchView Icon ?

The problem is that in the second example (which I believe correlates to the first picture), you are using an Application Context to inflate Views instead of an Activity Context.
The framework does a lot of work to ensure that an Activity's Context is correctly configured with the correct screen density, theme, and other such information. An Application is not expected to ever need to worry about displaying content, so it is not configured to do so.
Do not use an Application context to inflate Views or obtain resources. Use an Activity context.

Well its that the context determines from where(theme) the icons are created. I guess your OS and the app has different themes
Read more here http://developer.android.com/reference/android/view/LayoutInflater.html
The Context in which this LayoutInflater will create its Views; most importantly, this supplies the theme from which the default values for their attributes are retrieved.

Related

Android: How to modify the layout of a class from another class?

I have an app with multiple activities and I would like to modify some of its layout properties to recycle them and avoid creating more activities. I tried with LayoutInflater, but when I modify the layout, the methods atached to that layout won´t work. Before that, I had used LayoutInflater to change the layout of an AlertDialog and it worked fine.
Another thing I could do is send parameters with an Intent and retrieve them in the class of the layout I want to change, but like I said, I have a lot of activities and I think there has to be an easier solution.
I include here my try with LayoutInflater ;act2 is the class with the layout I want to change:
LayoutInflater Inflater = act_main.getLayoutInflater();
lay = Inflater.inflate(R.layout.act2, null);
ImageButton restart = (ImageButton)lay.findViewById(R.id.restart);
restart.setVisibility(View.INVISIBLE);
act_main.setContentView(lay);
PD: Sorry if my English is not that good.

How to create a button and a text view programmatically in activity class

I want to create a button (in scrollView/vertical layout) that when you click it, it disappears and in its place a number and two text views appear. I could just create them in the xml file, disable them and make them invisible and when the button is clicked they appear but I want to have 15 buttons.
Questions
A. Is there a way that I can do this in the java class programmatically so that I avoid doing 15 times the same thing in xml.
B. If I end up doing it in xml will it be ram and cpu consuming?
C. If I use Java to create a new layout each time how can I disable the previous, so if the user clicks nothing happens.
You can use LayoutInflater to create View programatically using an XML layout file.
LayoutInflater inflater = getLayoutInflater();
View myRootView = inflater.inflate(R.layout.yourXMLFile, mainLayout, false);
Now your root view in xml = myRootView, you can generate it as much as you want.
Secondly, you can create a button or textview using just pure java like :
Button btn = new Button(this);//this refers to Context,
btn.setText("Some Text Here")
parentLayout.addView(btn);

Java - Android - view.getContext() meaning

Hello i have just started learning android application development and i am watching a lot of tutorials but none of them really describe step by step so my question is :
i have created a simple app which contains on TextView one EditText and one Button
i have added android:onClick="onButtonClick" to my Button so it will trigger the onButtonClick method , now , i would like it to print out the userinput from EditText so what i did is :
public void onButtonClick(View v){
Toast.makeText(v.getContext(), email.getText().toString(), Toast.LENGTH_SHORT).show();
}
but why the method has to contain the View v ? where is it passed from ? and what does it contain ? it contains the button which i clicked ? and what does the v.getContext() do? why my app does the same when replacing the v.getContext() with this ?
That are many questions at once, but I try to answer them one by one.
but why the method has to contain the View v ? where is it passed from ? and what does it contain ?
Consider the documentation of View.OnClickListener:
View: The view that was clicked.
So you are correct in your assumption that it is the View that has been clicked.
and what does the v.getContext() do?
The first parameter of the Toast#makeText method is a Context. Basically the Context is a container of global information in an Android application. The Toast needs it to retrieve information to show itself.
why my app does the same when replacing the v.getContext() with this ?
I assume your method resides in an Activity. An Activity is a subclass of Context and can be used as a parameter.
If you click a button then View is passed. ViewGroup is a group of View example LinearLayout, Relative Layout, FrameLayout,etc. View is a part of ViewGroup. According to Official Documentation, A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
I hope you understand well about what is View and ViewGroup!!

Android 5.1 : Attempt to write to field 'java.util.ArrayList android.animation.AnimatorSet$Node.nodeDependents' on a null object reference

java.lang.NullPointerException: Attempt to write to field 'java.util.ArrayList android.animation.AnimatorSet$Node.nodeDependents' on a null object reference
I got this issue after move from Android 5.0 to 5.1, it happens when I am trying to inflate with cloned LayoutInflater. It will be OK if I just use the regular layout inflater. Also this inflation is happening on background thread with threadPool executor because two same layouts were needed to inflate at same time for performance reason. It would also be OK if I switch to use serialized executor.
final LayoutInflater bgLayoutInflater = layoutInflater.cloneInContext(getContext());
final ViewGroup rootView = (ViewGroup) bgLayoutInflater.inflate(resourceId, null, false);
link to the AOSP where crash happens, line 699
Any ideas?
So finally I figured out this problem on my own. Regardless the new 5.1 AOSP change should return a nicer error to tell me what went wrong, I was inflating a big layout in background thread on my own risk.
Here is I believe what happened.
final LayoutInflater layoutInflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
I need to inflate two big layout (600ms each) with the fastest time, so I decided to have asyncTask with threadPoolExecutor to execute the two inflation tasks. But I can't achieve it because the layoutInflater from SystemService is synchronized. So I made a cloned inflater that solved the problem until 5.1.
Now I am able to fix it with
final LayoutInflater layoutInflater = LayoutInflater.from(getContext());
So that each inflater is doing its own job in parallel. Again I am still at my own risk of inflating not on UI thread, in the future a new update may still break it somewhere.

Programmatically access IntelliJ UI Designer created objects?

I have created a TextView in the UI designer, but I can't figure out how I should access it from the code. I have tried Go To Declaration but that just brings me to the XML file where the TextView is 'made'. Does anyone know how to do this? Help is very much appreciated!
This is independent of the IDE. First you need to "find" the TextView, then you can modify its properties:
TextView myTextView = (TextView) findViewById(R.id.yourid); // The ID is declared in the XML file as android:id atrribute.
myTextView.setText("New Text");
What do you mean by "access it from the code"? If you're talking about navigating from where it's referenced in the code to viewing it in the UI designer, newer versions of Intellij with Android support enabled put tabs at the bottom of the editor when you're editing XML files to let you switch between a text representation and a visual representation of layout files.
If you're talking about how to instantiate the view in code, post some samples of what you've been trying (the most common way is to use a LayoutInflater).
Edit:
Changing the actual text that's displayed in the TextView isn't an IDE-specific issue. You have two ways to do this (well, three if you count the visual and text views of the XML file as separate methods). You can set the text either in the XML file by setting the android:text attribute on the TextView widget, or in the code by calling setText(). Whichever way you decide to do it, you should consider not referring to your text as a raw string but as a String resource as described here.
Edit 2:
OK, you're looking for instructions on how to inflate the view in the first place to get access to it. This is what I answered initially, but here's a little more code. In your Activity (you do have an Activity set up, right?):
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.<your layout ID>, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.<your TextView's id>);

Categories