[Q] Android Drawing: Using invalidateViews Method vs. Automatic - Android Q&A, Help & Troubleshooting

Basically what I want to know is what is the impetus for Android to redraw views.
If I change a resource like the image resource in an ImageView that has already been drawn the ImageView will be redrawn. But it seems I can't just change or add a view in something like GridView. Instead I have to use the adapter and I need to use invalidateView() for the view for the GridView be redrawn.
Why are ViewGroups like GridView designed in a way that make it use a different mechanism in the code to be redrawn? Why is the ImageView automatically redrawing, but they way the made GridView is that it has to have a method called to get redrawn?
Also just generally, what is ultimately causing the views to redrawn.

Related

[Q] traversing through Hierarchy viewer

I have a peculiar request.
Currently we see the home screen of the phone . Contains home icon,
maybe Browser icon (atleast 4 icon present)
Now i am given a coordinate (x,y) i know that coordinates is the point
where user clicked on the screen.
Using that coordinate i need to get the icon/image which was
clicked .eg. coordinate x,y is of the Browser image. my aim is to
fetch the browser image based on the coordiantes. For this i checked
hierarchy viewer.
1.
Now how to traverse through the hierarchy to get the exact location
(i.e Browser image) the issue..
Does hierachyviewer uses xml? Can i use xpath to get to the node
element?
2.
Now the cooridnate points : can be part of the Full frame view, it is
also part of the pane without status bar, it also exists on the
relative layout of the last row. It also exists inside the relative
layout of the Browser , it also exists inside the Imageview of the
Browser.
i hope you understood my scenario.
3.
How to reach to the last leaf node to be shown to user (Browser Image
View) which contains the coordinate. How can i traverse through the
Hierarchy and determine where the point is and what to show to the
user.
I just want the Browser image view to be shown as the png image to the
user (the last leaf node).
is it possible to traverse and fetch the image as shown in Hierarchy
viewer?

[Q] How to get a reference to the stock launcher activity object?

I want to read out the shortcuts' and widgets' position and name on the screens of the launcher. Since every launcher is different, I will focus on the stock launcher of Android for the moment. I have not found an API to do this, so I am trying my luck with Java's Reflection API. So far I have been able to get the class and read out the type of the variable holding the items on the desktop called mDesktopItems. But to read out the variable's content, I need a reference to the launcher activity object. It's obvious that this object does not have to exist, but I can probably check regularly and once it does, I would fetch the data.
So how do I get a reference to the launcher activity object? Or could you think of a better way of fetching the required data?

jquery Mobile orientation problem

Hi everyone.
I'm building an application with Html5 and jquery Mobile and I have a problem with orientation change. I have a controlgroup with 3 buttons. I want to make this buttons to be aligned horizontally while in portrait (data-type="horizontal") and aligned vertically while in landscape (data-type="vertical"). Im using this code:
Code:
$(window).bind('orientationchange', function(event){
if(event.orientation == 'portrait') {
$('.operation').attr('data-type', 'horizontal');
} else if (event.orientation == 'landscape') {
$('.operation').attr('data-type', 'vertical');
}
});
It changes the value of the data-type attribute on the page, but the buttons are still aligned horizontally. I figured this with inspect element on Chrome and I tried it on my Android phone but the buttons don't change. Any help what should I do to rearrange the buttons?
This might work. Call:
Code:
$(".operation").trigger("create")
after you change the data-type. It basically fires an event that tells jquery mobile to re-read all the data-* attributes and re-style the element.
footboydog said:
This might work. Call:
Code:
$(".operation").trigger("create")
after you change the data-type. It basically fires an event that tells jquery mobile to re-read all the data-* attributes and re-style the element.
Click to expand...
Click to collapse
I already tried that too but it didn't help. It helps if I try to change the class or something else with css but it doesn't help in changing the value of data-type.
Changing an attribute won't cause a redraw, like changing a css value or adding/removing a class name.
Where in the code do you define how to draw the buttons, depending on the attribute value? That's what you need to trigger again (document.ready, for example).
Archer said:
Changing an attribute won't cause a redraw, like changing a css value or adding/removing a class name.
Where in the code do you define how to draw the buttons, depending on the attribute value? That's what you need to trigger again (document.ready, for example).
Click to expand...
Click to collapse
This code is in the document.ready. How to trigger that?
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums & Read the Forum Rules
Thanks ✟
Moving to Q&A

Help with spinner

So this is actually help making an app not a ROM. I am trying to use a spinner in my app as a drop down menu.
Here is the xml code:
<Spinner
android:id="@+id/fromSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:entries="@array/from_array" />
Here is the Java code:
Spinner fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
Spinner toSpinner = (Spinner) findViewById(R.id.toSpinner);
ArrayAdapter<String> toAdapter = new ArrayAdapter<String>(this, R.id.toSpinner, R.array.to_array);
toSpinner.setAdapter(toAdapter);
ArrayAdapter<String> fromAdapter = new ArrayAdapter<String>(this, R.id.fromSpinner, R.array.from_array);
fromSpinner.setAdapter(fromAdapter);
String toCurrency = (String) toSpinner.getSelectedItem();
String fromCurrency = (String) fromSpinner.getSelectedItem();
I'm not sure what the adapters do but I think I need them. Anyway toCurrency and fromCurrency aren't getting the values they are supposed to get. Anyone know what I am doing wrong. I am a complete noob say it may be something really dumb.
Don't know if this is helpful. Just put the source for the whole app at https://github.com/Dmobbjr/CurrencyCalculator
Really? I know some developers here could figure this out in ~10 minutes.
I'm not able to look deeper but it would help for next time if you could post the value you are getting and the value that should be there.
I'm going to guess you are getting either the first element in the array or null?
You haven't selected a value anywhere and there is no evidence of a user interaction before you try and get the selected value so either there is no value selected or the first element in the array is selected (by default) and you are getting its value.
Have you looked through the documentation for Spinners in the API Guides section of the Android Developers website? I can tell the answer is no because if you had you would know why you need the Adapters.
Kavrocks said:
I'm not able to look deeper but it would help for next time if you could post the value you are getting and the value that should be there.
I'm going to guess you are getting either the first element in the array or null?
You haven't selected a value anywhere and there is no evidence of a user interaction before you try and get the selected value so either there is no value selected or the first element in the array is selected (by default) and you are getting its value.
Have you looked through the documentation for Spinners in the API Guides section of the Android Developers website? I can tell the answer is no because if you had you would know why you need the Adapters.
Click to expand...
Click to collapse
I will change it to see what the value is later but I know it is not getting the value of the first element.
The user interaction happens before any of the code above is executed. I assumed that when I create the spinners and adapters in java it just takes what was already selected. Do I have to initialize the spinners and adapters before user selection?
Also I did read the documentation which is why I even knew about adapters but I am still a noob so some of the docs can be pretty confusing.
dmobbjr said:
I will change it to see what the value is later but I know it is not getting the value of the first element.
The user interaction happens before any of the code above is executed. I assumed that when I create the spinners and adapters in java it just takes what was already selected. Do I have to initialize the spinners and adapters before user selection?
Click to expand...
Click to collapse
The spinner needs to be initialised with data before the user selects it otherwise it would be empty. That's what the ArrayAdapter in your OP is for (which is explained in the API Guides section on spinners), however the android:entries attribute in your xml file is also adding data to the spinner so you have redundant code.
The getSelectedItem() will return an Object containing the currently selected item but as you are changing the data in the spinner beforehand this could be altering the data in the selected item position.
I recommend you remove the android:entries attribute and move the code except for the getSelectedItem() lines to the onCreate() method of your Activity.
You really need to provide more info for me about what is happening for me to help. I can't view the source on github at the moment.
If I implement the spinners in onCreate(), how do I access them from my button's onClick() method. Wouldn't the spinners be local to the onCreate method and therefore not accessible outside of it?
dmobbjr said:
If I implement the spinners in onCreate(), how do I access them from my button's onClick() method. Wouldn't the spinners be local to the onCreate method and therefore not accessible outside of it?
Click to expand...
Click to collapse
You aren't implementing them in onCreate(), they are implemented in xml which is normally inflated in onCreate() when you call setContentView(). All you are doing is adding the data to them in onCreate(). After the layout is inflated everything in that layout file is global (can be accessed anywhere) to that class.
You can get a reference to them anywhere in your code, providing the layout that contains them has been inflated into memory, using findViewById() and passing in the id of the spinner you want to get a reference to.
You can declare a class variable to store the reference to these and then just get the reference once in onCreate() and access it anywhere else in your code, ie. in onClick(), instead of having persistant calls to findViewById(). This approach is more efficient and cleaner.
When I try to call findViewById(toSpinner).getSelectedItem() The compiler says it can't resolve toSpinner as a variable. When I try to call R.id.toSpinner.getSelectedItem() it says it can't invoke getSelectedItem() on and int. I still cant access the spinners from within my onClick(). Sorry for the extreme noobishness but I just clearly do not know as much java as I should.
dmobbjr said:
When I try to call findViewById(toSpinner).getSelectedItem() The compiler says it can't resolve toSpinner as a variable. When I try to call R.id.toSpinner.getSelectedItem() it says it can't invoke getSelectedItem() on and int. I still cant access the spinners from within my onClick(). Sorry for the extreme noobishness but I just clearly do not know as much java as I should.
Click to expand...
Click to collapse
You are calling findViewById() returns a View object, you can't call getSelectedItem() on a View object. You first need to cast the View returned by findViewById to a spinner and then call getSelectedItem() on the spinner.
R.id.toSpinner is just an integer value pointing to a resource.
I already pointed out how you should go about calling getSelectedItem() on your spinner.
You can declare a class variable to store the reference to these and then just get the reference once in onCreate() and access it anywhere else in your code, ie. in onClick(), instead of having persistant calls to findViewById(). This approach is more efficient and cleaner.
Click to expand...
Click to collapse
I'll write the outline of the code you need for you.
Code:
public class TestActivity extends Activity {
private Spinner toSpinner;
public void onCreate() {
toSpinner = (Spinner) findViewById(R.id.toSpinner);
}
public void onClick() {
toSpinner.getSelectedItem();
}
}

specifying widget parameter

The code below is from a backup of Total Launcher (my personal favorite).
Apparently there will be one instance of similar code for each widget defined on each launcher page.
As you can see, this example is for Zooper widget. But each Zooper widget use can have a different "skin".
SO, how is this "skin" specified in the below code? It seems that the parameter "i":715 would have something to do with it, but I don't know what the parameter means. Some sort of index maybe?
Other parameters are easily deduced like T , L, W and H for top, left, width and height.
This seems to be a somewhat normal android paradigm; I've seen references to lots of other widgets (not Zooper) and most utilize the "i" parameter.
I have added two ZW's to a page in TL and set each to a different "skin". The lines below will be added twice to the parameter file with ONLY the "I" parameter different.
Anyone??
Boowho??
{"w":360,"aw":360,"b":[{"T":3,"p":"org.zooper.zwpro\/org.zooper.zwlib.provider.WidgetProvider4x4","i":707,"X":-2.5,"Y":-41,"W":360,"H":360},{"T":3,"p":"com.xwidgetsoft.xwidget\/.appwidget.XWidgetMain4x4","i":715,"X":-2,"Y":195,"W":360,"H":418.5}]}

Categories

Resources