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. */
    (Override)
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_NO_TITLE);					
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
			WindowManager.LayoutParams.FLAG_FULLSCREEN);
	glView = new GLSurfaceView(this);								
	glView.setRenderer(new MyOpenGLRenderer());						
        setContentView(glView);											
    }

    class MyOpenGLRenderer implements Renderer {						
    	(Override)
    	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);
    		gl.glMatrixMode(GL10.GL_PROJECTION);
    		gl.glLoadIdentity();
               //Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 
				0.1f, 100.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
    	}
    	
    	(Override)
    	public void onSurfaceCreated(GL10 gl, EGLConfig config) {	
    		Log.d("MyOpenGLRenderer", "Surface created");
    	}

    	(Override)
	public void onDrawFrame(GL10 gl) {								
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);						
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -4.0f);
		triangle1.draw(gl);
		//gl.glTranslatef(2.0f, 0.0f, -5.0f);
		gl.glTranslatef(0.5f, 0.0f, -1.0f);
		triangle2.draw(gl);	
	}
    }
}

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
			};
		this.red = red;
		this.green = green;
		this.blue = blue;
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 3 * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.flip();
	}
	
	public void draw(GL10 gl) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		// 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
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
}

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.