StartingGroovy
StartingGroovy

Reputation: 2860

How do you add a custom view on a view in Android?

I have a graph view (custom view) in Android and a main view. Depending on the user's preference I want to add 1-5 graph views onto my main view but am not quite sure how to. (I'm using purely Java and not xml). I was reading that I might have to use a Relative Layout or something in order to stack views.

Any advice or suggestions are welcome

Upvotes: 0

Views: 292

Answers (2)

Lumis
Lumis

Reputation: 21629

Here is an example of a custom view as graph. One needs to have a LinearLayout somewhere in the layout which has ID set to @+id/ll and size of the graph:

public class RootActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int[] graphData = {3,5,2,7,4,8,1,5,9};

    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);  
    GraphView graphView = new GraphView(this);
    ll.addView(graphView);


    //call this method with every new set of data
    graphView.drawGraph(graphData);


}


class GraphView extends View{
 int[] graphData;
 Paint graphPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 int screenH;
 int screenW;
 int colW;
 int colH;
 int columnCount;

 public GraphView(Context context) {
    super(context);

    graphPaint.setColor(Color.MAGENTA);
    graphPaint.setStyle(Style.FILL);
}


 @Override     
 public void onSizeChanged (int w, int h, int oldw, int oldh) {         
     super.onSizeChanged(w, h, oldw, oldh);         
     screenW = w;         
     screenH = h; 
 }


public void drawGraph(int[] graphData){
    this.graphData = graphData;
    columnCount = graphData.length;
    invalidate();
}


@Override     
public void onDraw(Canvas canvas) {         
    super.onDraw(canvas); 

    colW = (screenW - 10) / columnCount;
    int graphStep = 20;
    int columnSpace = 5;

    canvas.drawText("GRAPH", 10, 10,  graphPaint);

    for (int i= 0 ; i < columnCount; i++){
        //draw columns from bottom up
        canvas.drawRect(
                new Rect(
                i * colW + 5, 
                screenH - 5 - (graphData[i] * graphStep), 
                i * colW + 5 + colW - columnSpace, 
                screenH - 5     
                ), 
                graphPaint);
    }



}

Upvotes: 1

FoamyGuy
FoamyGuy

Reputation: 46856

in your activity you probably have something like this towards the begining of your onCreate() method:

setContentView(R.layout.main);

inside your main.xml file you probably have an element that is some kind of layout. I will assume LinearLayout for now, but it works similarly with all types. You'll need to get a reference to this layout and to do that it must have an id. So if that layout does not have something like this in it you need to add it:

android:id="@+id/myMainLayout"

Then back in your java sometime after you've called setContentView() you can find the reference to your layout with something like this:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.myMainLayout);

Once you have a reference to your layout you can add your graph views to it with something like this:

myLayout.addView(graph1);
myLayout.addView(graph2);
//etc...

If you want to skip the xml layout all together you are allowed to make your layout in java. To do that it would like this:

LinearLayout myLayout = new LinearLayout(YourActivity.this);
myLayout.addView(graph1);
myLayout.addView(graph2);
setContentView(myLayout);

Note that you can only call setContentView() once so you'll need to pass some kind of Layout to it if you want to add more than 1 View.

Edit:

I have never specifically tried but I would think you could call addView() from the constructor in your custom view:

public CustomView() {
    this.addView(anotherView);
}

do you have a custom view for your layout too?

Upvotes: 3

Related Questions