Skip navigation

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: