Reputation: 1280
First off, I am very new to Android. There might be silly mistakes, please correct the one(s) you see.
The problem I am having is to add a view to the existing view. There is a button that you can press that should decrease a value. It works if I keep them in separate fragments, however I want one fragment to handle all of it. The problem occurs in Playfield
, in it's onCreateView
method.
addView(pc.getView(getActivity())); // this I know is the error, but absolutely no clue of how to solve it.
This is what I want to accomplish. I want to directly add that view into the current view.
I tried adding another widget and give it the properties of this view, however that failed.
fragment_playfield.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="@string/fire_button" />
</RelativeLayout>
Playfield
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Playfield extends Fragment
{
private View view;
private PowerComponent pc = new PowerComponent();;
private final static int BUTTON_FIRE = R.id.button1;
private Button buttonFire;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_playfield, container, false);
addView(pc.getView(getActivity())); // this I know is the error, but absolutely no clue of how to solve it.
buttonFire = (Button) view.findViewById(BUTTON_FIRE);
buttonFire.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fire ();
}
});
return view;
}
private void fire () {
if (pc.firePower()) {
System.exit(0);
}
}
}
PowerComponent
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class PowerComponent
{
private PowerComponentView view;
private static final int POWER_MAX = 100;
private static int power = POWER_MAX;
public View getView (Activity activity) {
view = new PowerComponentView(activity);
return view;
}
public boolean firePower () {
if (power < 5) {
return false;
}
power -= 5;
view.invalidate();
return true;
}
private class PowerComponentView extends View
{
/**
* Power GUI
*/
private final Paint paint = new Paint();
private final int GUI_BAR_X = 100;
private final int GUI_BAR_Y = 10;
private final int GUI_BAR_HEIGHT = 30;
private final int GUI_BAR_WIDTH_MAX = 200;
private final Rect powerFrame = new Rect (GUI_BAR_X-1, GUI_BAR_Y-1, GUI_BAR_X + GUI_BAR_WIDTH_MAX+1, GUI_BAR_HEIGHT+1);
private final Rect powerBar = new Rect (GUI_BAR_X, GUI_BAR_Y, GUI_BAR_X + GUI_BAR_WIDTH_MAX, GUI_BAR_HEIGHT);
/**
*
* @param context
*/
public PowerComponentView (Context context) {
super (context);
}
@Override
public void onDraw (Canvas canvas) {
super.onDraw(canvas);
// Frame
paint.setColor (Color.BLACK);
canvas.drawRect (powerFrame, paint);
// Bar
paint.setColor (Color.rgb (255, 255, 0));
powerBar.right = GUI_BAR_X + (int)(((double)power/(double)POWER_MAX) * (double)GUI_BAR_WIDTH_MAX);
canvas.drawRect (powerBar, paint);
}
}
}
The problem is that I am unable to add the View
returned from my PowerComponent.getView()
to my current View
.
Upvotes: 0
Views: 35
Reputation: 3553
Fragment class doesn't have addView() method. addView is a method from ViewGroup class. Use:
container.addView()
Upvotes: 1