Background object drawn in foreground. How to correct?

Hello, I am a complete beginner and I try to learn OpenGL. Please firgive if my English is imperfect. The program draws two triangles. It is a bit modified example found on the Internet. As far as I understand the code I think that the green triangle should be drawn partially “behind” the red triangle. I expect that the green triangle (drawn as second) should be covered partially by the red one. Unfortunately the program draws these triangles in the sequence like in program, and the triangle which is drawn as second covers the first, which is not in accordance to 3D coordinates.In this particular case it is not a problem to change sequence of drawing triangles in the program, but in much more complicated models I may not be able to find out correct drawing sequence.

Please help possibly detailed so that I am able to modify the code so that the OpenGL engine automatically draws the background triangle partially covered by the foreground one. Thanks.

There are three classes MainActivity, MyOpenGLRenderer and Triangle.
Sorry for weird quoting - it is because the forum limited quoting code.
Fragment from MainActivity and MyOpenGLRenderer:

public class MainActivity extends Activity {
    private GLSurfaceView 	glView;
    private Triangle		triangle1;
    private Triangle		triangle2;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
	glView = new GLSurfaceView(this);								
	glView.setRenderer(new MyOpenGLRenderer());						

    class MyOpenGLRenderer implements Renderer {						
    	public void onSurfaceChanged(GL10 gl, int width, int height) { 
    		Log.d("MyOpenGLRenderer", "Surface changed. Width=" + width
    				+ " Height=" + height);
    		triangle1 = new Triangle(0.5f, 1, 0, 0);
    		triangle2 = new Triangle(0.5f, 0, 1, 0);
    		gl.glViewport(0, 0, width, height);
               //Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 
				0.1f, 100.0f);
    	public void onSurfaceCreated(GL10 gl, EGLConfig config) {	
    		Log.d("MyOpenGLRenderer", "Surface created");

	public void onDrawFrame(GL10 gl) {								
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);						
		gl.glTranslatef(0.0f, 0.0f, -4.0f);
		//gl.glTranslatef(2.0f, 0.0f, -5.0f);
		gl.glTranslatef(0.5f, 0.0f, -1.0f);

Fragment from Triangle:

public class Triangle {
	private FloatBuffer vertexBuffer;
	private float base = 1.0f;
	private float red, green, blue;
	private float vertices[] = {
	      -0.5f, -0.5f, 0.0f,        // V1 - first vertex (x,y,z)
           0.5f, -0.5f, 0.0f,        // V2 - second vertex
           0.0f,  0.5f, 0.0f         // V3 - third vertex
	public Triangle(float scale, float red, float green, float blue) {
		vertices = new float[] {
			      -base * scale, -base * scale, 0.0f, // V1 - first vertex
			       base * scale, -base * scale, 0.0f, // V2 - second vertex
		           0.0f,  base * scale, 0.0f          // V3 - third vertex
			}; = red; = green; = blue;
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 3 * 4);
		vertexBuffer = byteBuffer.asFloatBuffer();
	public void draw(GL10 gl) {
		// set the colour for the triangle
		gl.glColor4f(red, green, blue, 0.5f);
		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
		// Disable the client state before leaving

Did you enable depth testing and have you selected an apropriate compare function?

Thanks for your hint, I succeeded using depth testing :), I didn’t know there is such feature in OpenGL.