Android – how do I retrieve fragment instances of XML definitions?
In short, the problem is that I can find my element through ID, but it always requires me to convert it to view, and I can't convert it to my object name type. I'm not sure whether converting to view is a hypothesis, but the Android example just creates a fragment and adds it, so it is not defined in XML. But the key is that in the example, the object is not a type view
More information: this may be a quick and simple solution. I have a fragment class, which contains the custom view I create with code (it is a game of continuous drawing). I added a fragment in the upper left corner of the XML layout. Below the fragment is the button (defined in the XML layout). I have a main activity. I want a handle to the fragment instance containing my custom view, In this way, when you click the button, some variables in the clip will be changed and the view will be redrawn
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.dcubebluetooth"
android:id="@+id/lEDView1"
android:layout_width="400px"
android:layout_height="400px" />
<Button
android:id="@+id/layer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lEDView1"
android:layout_marginTop="50px"
android:text="Layer1"
android:textSize="12dp" />
<Button
android:id="@+id/layer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer1"
android:layout_toRightOf="@id/layer1"
android:text="Layer2"
android:textSize="12dp" />
<Button
android:id="@+id/layer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer2"
android:layout_toRightOf="@id/layer2"
android:text="Layer3"
android:textSize="12dp" />
<Button
android:id="@+id/layer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer3"
android:layout_toRightOf="@id/layer3"
android:text="Layer4"
android:textSize="12dp" />
</RelativeLayout>
Main activities:
public class MainActivity extends Activity implements LEDGridFragment.TriggerBlueTooth{
Button l1;
Button l2;
Button l3;
Button l4;
BluetoothService bt;
LEDGridFragment gridUI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
@Override
public void onledSelected(int layer, int led) {
// TODO Auto-generated method stub
String msg = Integer.toString(layer);
if(led<10) msg+=0;
msg+= Integer.toString(led);
bt.write(msg);
}
//Full method below not showing. I don't kNow why
public void initialize(){
gridUI = findViewById(R.id.lEDView1);
bt = new BluetoothService();
l1 = (Button) findViewById(R.id.layer1);
l2 = (Button) findViewById(R.id.layer2);
l3 = (Button) findViewById(R.id.layer3);
l4 = (Button) findViewById(R.id.layer4);
l1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 0;
gridUI.update();
}
});
l2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 1;
gridUI.update();
}
});
l3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 2;
gridUI.update();
}
});
l4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 3;
gridUI.update();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Segment:
public class LEDGridFragment extends Fragment{
LEDView view;
TriggerBlueTooth mCallBack;
boolean[][][] states = new boolean[4][4][4];
int currentLayer = 0;
public interface TriggerBlueTooth{
public void onledSelected(int layer, int led);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallBack = (TriggerBlueTooth) activity;
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) (event.getX())/100;
int y = (int) (event.getY())/100;
int led = y+(x*4);
mCallBack.onledSelected(currentLayer, led); //Notify Main Activity of the led that was toggled
//So it can send data over Bluetooth
return false;
}
});
}
public void update(){
view.drawGrid(states, currentLayer); //Update the view
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
for(int i=0; i<states.length; i++){ //Initialize the board off which the view will be drawn
for(int j=0; j<states[0].length; j++){
for(int k=0; k<states[0][0].length; k++){
states[i][j][k] = false;
}
}
}
view = new LEDView(getActivity());
return view;
//return super.onCreateView(inflater, container, savedInstanceState); //Auto-formed. Commented out
}
}
Next to (well, if you don't answer this): the graphic layout doesn't show its assumptions in the initial drawing of the game. If I add the view itself, it does. But now, it's a gray box with "select preview layout from the 'fragment layout' context menu". Where is the menu? I want to know if my point is valid. I can't test it myself because 1. It's not compiled, 2. I don't have an Android phone
resolvent:
This should be done:
FragmentManager mgr = getFragmentManager();
Fragment fragment = mgr.findFragmentById(R.id.lEDView1);