Skip navigation

Category Archives: technology

android_awesome

Okay, finally had some time to revisit this issue and I believe I’ve found the answer: First, before the xml layout or its components can be addressed they need to be inflated. I knew this, but I wasn’t sure when exactly they were inflated. It turns out that setContextView (and probably addContextView) trigger xml inflations. In order to have completely modular activity/view classes, I needed to do something like the following:

Activity Class–

package com.ai.ultimap;

import com.ai.ultimap.views.HomeView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class UltiMapActivity extends Activity {
private View hv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hv = new HomeView(this);
}
}

Custom View Class-

package com.ai.ultimap.views;

import com.ai.ultimap.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;

public class HomeView extends View implements OnClickListener{

private RadioButton twodRB;
private RadioButton threedRB;
private TextView locTV;
private EditText editlocET;

public HomeView(Activity hAct) {
super(hAct);
//THE FOLLOWING LINE INFLATES– IT (or another function which calls xml inflation)
//MUST COME BEFORE ANY JAVA ADDRESSING OF WIDGETS IN
//THE XML LAYOUT
//Also note that even though you could invoke findViewById from a class extending
//View, in this case you must use hAct.findViewById. I believe this is due to the
//fact that the activity referenced by hAct is the object responsible for inflating
//the xml and thus the widgets need to be instantiated from it.
hAct.setContentView(R.layout.ultimap);
twodRB = (RadioButton) hAct.findViewById(R.id.twodRBV);
threedRB = (RadioButton) hAct.findViewById(R.id.threedRBV);
locTV = (TextView) hAct.findViewById(R.id.locationTV);
editlocET = (EditText) hAct.findViewById(R.id.locationETV);
//After instantiation however they can be freely accessed from java in
//non-activity classes, which is the point; see the next line…
twodRB.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
locTV.setText(“yo”);
}

}

This code works properly to load up the pre-defined xml view ultimap.xml and then address the widgets dynamically from Java (completely outside the activity class), changing the text of the location text view from ‘Location’ to ‘yo’ when the twodRB radiobutton is clicked!  To get a direct handle to the layout inflater service, invoke the following:

LayoutInflater instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use an Activity object’s getLayoutInflater() or getSystemService(String) method to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);

To create a new LayoutInflater with an additional LayoutInflater.Factory for your own views, you can use cloneInContext(Context) to clone an existing ViewFactory, and then call setFactory(LayoutInflater.Factory) on it to include your Factory.

(ref: http://developer.android.com/reference/android/view/LayoutInflater.html)

Hope this helps some googlers :)

Advertisements

linux_android_iphone1

Ever been vexed by defining layout parameters in android views via the “new LayoutParams(LayoutParams.whatever,LayoutParams.whatever)” syntax you see all over the place? Well I certainly am, and it’s also bad practice for runtime performance since you use the ‘new’ operator repeatedly for essentially the same task.

Below follows a convenience class for defining simple layout parameters:


import android.view.ViewGroup.LayoutParams;

public class DefineLayoutParams {
//the controls
private static final int MATCHMATCH = 1;
private static final int WRAPMATCH = 2;
private static final int MATCHWRAP = 3;
private static final int WRAPWRAP = 4;

//the ONE layoutparams object
private static LayoutParams floatParams = new LayoutParams(0,0);

public DefineLayoutParams(){

}

public static LayoutParams getParams(int type){
if (type == MATCHMATCH){
floatParams.width = LayoutParams.MATCH_PARENT;
floatParams.height = LayoutParams.MATCH_PARENT;
}
else if(type == WRAPWRAP){
floatParams.width = LayoutParams.WRAP_CONTENT;
floatParams.height = LayoutParams.WRAP_CONTENT;
}
else if (type == WRAPMATCH){
floatParams.width = LayoutParams.WRAP_CONTENT;
floatParams.height = LayoutParams.MATCH_PARENT;
}
else if (type == MATCHWRAP){
floatParams.width = LayoutParams.MATCH_PARENT;
floatParams.height = LayoutParams.WRAP_CONTENT;
}
return floatParams;
}

public static LayoutParams getCustomParams(int w, int h){
floatParams.width = w;
floatParams.height = h;
return floatParams;
}

public static int getMM(){
return MATCHMATCH;
}
public static int getWW(){
return WRAPWRAP;
}
public static int getWM(){
return WRAPMATCH;
}
public static int getMW(){
return MATCHWRAP;
}

}

Once this class is added to your project’s src directory, use it like so–
1. [predefined] hActivity.addContentView(hView,DefineLayoutParams.getParams(DefineLayoutParams.getWW()));
2.[custom] hActivity.addContentView(hView,DefineLayoutParams.getCustomParams(150,250));

I’ve been working with parallel algorithms a lot lately, so here’s another digimal related to semaphores:

Digimals::SerpentineSemaphore := A variant semaphore structure configured to unobtrusively watch or slither between asynchronous threads, ominously gathering statistics until the perfectly opportunistic conditions (for collision, deadlock, starvation etc.) are met, at which point it mercilessly strikes target threads  with precise and undeniable directives!

Green-Beautiful-Emerald-Tree-Boa-snake (7) Emerald_Tree_Boa_by_Track_Maidens

As mechanical engineers learned long ago, there is much we human scientists can glean from nature (and then improve upon) which is applicable to our innovative endeavors.  This is true also for software engineers… to that end, I present the first in a series of nature inspired programming strategies, partly for laughs, mostly serious 🙂

Digimals::Octopus Oriented Programming := a multi-threaded (at least 8) application design pattern where processing and resource sharing is managed by a single amorphous semaphore
octopus-RAW-IN-THE-OCEANHPC

It’s still early in design and the developers (possibly including me on the Android front) haven’t decided on a definite solution yet, but big changes are coming to robocode in version 2.0.  A major focus will be mobile device support for the simulation, bots, and/or the whole programming environment!  See the robocode-developer’s google group and this thread for details… Image

For JVM/DVM Java memory usage, the Eclipse Memory Analyzer Tool (MAT)

For native memory debugging (allocations in C/C++ etc.), Valgrind is awesome and supports quite a few platforms!

st-george-dragonThe Chivalrous Valgrind Logo!

As I understand the issue, there can be multiple causes for this error (not building against the correct machine architecture, permissions set incorrectly, etc.) but for me none of these common issues solved the problem.  I was in a bit of a unique situation you see– I had built an application as a static library to be utilized by a different application, and I wanted to return to development of the original application.  This required rolling its settings back to build as an executable rather than a static library.  I did this by changing the artifact extension from static library (.a) to executable, but I noticed the executable icon in eclipse was still a ‘plugin symbol’ (jigsaw puzzle piece) rather than a ‘go symbol’ (circle with an inscribed arrow pointing right).  After much googling to little effect and executing the program from the terminal to verify that it produced the same error [cannot execute binary file and ‘echo $?’ to see the last exit code yields 126], I took another look at the command string eclipse was generating and saw the odd switch ‘-dynamiclib’ appended after the various ‘-framework x’ linker switches I needed.  Looking deeper into my linker settings I noticed that the ‘Shared’ box was checked under ‘Shared Library Settings’. The solution of course was to uncheck this box, which removed -dynamiclib from the command string.

In sum, the missing step was as follows:

1. Go to project->properties->C/C++ Build->Settings->MacOS X C++ Linker->Shared Library Settings and uncheck the ‘Shared (-dynamiclib)’ box.  I don’t know why that was checked to begin with since I was building static libs before, but whatever the case this fixed the problem and made the program fully executable again.