Categories
discuss

Android: How to handle drawing with canvas methods on different screen sizes?

I’ve been following the best practices for creating layouts for differing screen sizes as per this guide: http://developer.android.com/guide/practices/screens_support.html

I’ve now come to doing some drawing work using methods of the Canvas class:

eg. canvas.drawLine(60, 320, 120, 320, linePaint);

Can any one help with how I am supposed to make those pixel values (60/120/etc) in to density pixels so that the line is scaled correctly? Otherwise the line will not appear as I intend, it’ll be too far down and too long on smaller screens for example.

Thanks for any replies.

Answer

I missed it the first time going through the Android document; the necessary info is in section “3. Do not use hard-coded pixel values in your code” of the link in my original post.

Categories
discuss

Getting an option text/value with JavaScript

Answer: var option_user_selection = element.options[element.selectedIndex].text

I am trying to build a form that fills in a person’s order.

<form name="food_select">
    <select name="maincourse" onchange="firstStep(this)">
        <option>- select -</option>
        <option text="breakfast">breakfast</option>
        <option text="lunch">lunch</option>
        <option text="dinner">dinner</option>
    </select>
</form>

What I’m trying to do is send in the select object, pull the name and the text/value from the option menu AND the data in the option tag.

function firstStep(element) {
    //Grab information from input element object
    /and place them in local variables
    var select_name = element.name;
    var option_value = element.value;
}

I can get the name and the option value, but I can’t seem to get the text=”” or the value=”” from the select object. I only need the text/value from the option menu the user selected. I know I can place them in an array, but that doesn’t help

var option_user_selection = element.options[ whatever they select ].text 

I also need to use the passed in select reference as that is how it’s set up in the rest of my code.

Later on, that text/value is going to be used to pull the XML document that will populate the next select form dynamically.

Answer

You can use:

var option_user_selection = element.options[ element.selectedIndex ].text
Categories
discuss

How to define a Map in a YAML file in the Play! framework?

I’m using the Play! framework and I have a model (an Entity) that has a variable of type Map.

To make some tests, I load a YAML file every time I start the application, but I don’t know the syntax to define a Map.

Let’s say I have this class

public class aClass {
    public int arg1;
    public String arg2;
    public Map<String, Integer> arg3;
}

What would my YAML file look like? I tried this:

aClass(object1)
    arg1:   34
    arg2:   aString
    arg3:   [key1: value1, key2: value2] <- What do I do here?

This is one of my many tries. I don’t know the syntax and I can’t find it on the Internet. I think the Play! uses SnakeYAML as YAML parser.

In the doc I found some examples, but none of them will work.

In my application, everything works except that the Map variable (arg3 in the example) has a size of 0 element.

Thanks.

Answer

According to this thread on Google Groups it appears that the syntax you need is to use a question mark.

The thread has a working map in the following syntax.

Bloc(b2): 
  labelSeries: testDeValeur 
  criterias: 
    ? key1 
    : value1 
    ? key2 
    : value2 

So, I guess your example would be

aClass(object1)
  arg1:   34
  arg2:   aString
  arg3:   
    ? key1
    : value1
    ? key2
    : value2

I have tested this however and it does not work! I would suggest raising a bug to take a look into this, because all documentation points at this being correct.

Categories
discuss

Android SeekBar setProgress is causing my MediaPlayer to skip

I’m trying to use a SeekBar to display both the length of a track played by a MediaPlayer class and to enable seeking within the track.

Seeking within the track works well. However, updating the progress value using setProgress while the track is playing seems to cause a slight skip.

In the onCreate method I create a Thread with a loop which updates the SeekBar’s progress value for the current track. This loop resets when the track is changed.

private void createProgressThread() {

    _progressUpdater = new Runnable() {
        @Override
        public void run() {
            //Exitting is set on destroy
            while(!_exitting) {
                _resetProgress = false;
                if(_player.isPlaying()) {
                    try
                    {
                        int current = 0;
                        int total = _player.getDuration();
                        progressBar.setMax(total);
                        progressBar.setIndeterminate(false);

                        while(_player!=null && current<total && !_resetProgress){
                            try {
                                Thread.sleep(1000); //Update once per second
                                current = _player.getCurrentPosition();
                                 //Removing this line, the track plays normally.
                                progressBar.setProgress(current); 
                            } catch (InterruptedException e) {

                            } catch (Exception e){

                            }            
                        }
                    }
                    catch(Exception e)
                    {
                        //Don't want this thread to intefere with the rest of the app.
                    }
                }
            }
        }
    };
    Thread thread = new Thread(_progressUpdater);
    thread.start();
}

Ideally I would rather not use a thread as I understand that this has disadvantages. Also please excuse the exception swallowing – it is difficult to keep checking for all MediaPlayer states in response to UI events. However, my real problem is that the music is skipping.

Could anyone suggest an alternative way to update the progress and explain why the call to setProgress is causing the track to skip even with the use of a seperate thread?

Thanks in advance.

Answer

I think the problem is that when you call the setProgress(), the onProgressChanged event is fired.

The listener (OnSeekBarChangeListener) have a method public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser). Here you should test if the listener was fired by a user action or from code. In your case, the fromUser variable should be false.

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(fromUser){
           player.seekTo(x);
        }
        else{
         // the event was fired from code and you shouldn't call player.seekTo()
        }
}
Categories
discuss

Terminate debug on device

I’m a bit confused about debugging on a device.
When I use the emulator, after the debug session, I simply close the emulator window…
but when debugging on my device, how can I close the session??simply disconnecting the USB cable?

And the installed .apk ( installed by Eclipse ) should be removed manually?

Answer

You can do this by going to the Debug perspective, right clicking your application, and selecting “Disconnect” or “Terminate”.

alt text

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..