11

I need to create a layout that's split the screen diagonally into two parts with different colors as their background.Something like this:

enter image description here

How can I achieve this?

Anshul
  • 7,914
  • 12
  • 42
  • 65
  • http://looksok.wordpress.com/2013/08/24/android-triangle-arrow-defined-as-an-xml-shape/ – Illegal Argument Jul 28 '14 at 09:20
  • Do you want just to draw it like this or actually create new GroupView with right and left sub views with ability to add children views inside? – sromku Jul 28 '14 at 09:25
  • @sromku I want to create a groupview with right and left subviews with ability to add children views inside. – Anshul Jul 28 '14 at 09:27
  • Extend LinearLayout in your own custom view group. http://www.jayway.com/2012/06/25/creating-custom-android-views-part-1-extending-standard-views-and-adding-new-xml-attributes/ – Simon Jul 28 '14 at 09:36

3 Answers3

0

This can be done as follows:

  • Create a FrameLayout (lets say 50x50 pixels).
  • Create two ImageViews (inside the FrameLayout and set them to match_parent) and as source give them the two triangles.
  • Create an onTouchListener for the FrameLayout.

Now comes the tricky part:

public boolean onTouch(View v, MotionEvent me){ 
  float time  = System.getCurrentTimeInMilles();

  if(me.action == MotionEvent.DOWN)
    lastTimePress = time;   /// global var

  if(me.action == MotionEvent.UP && lastTimePress - time < 200){
    if(calcPlace(me.getX()) < me.getY())
      /// go to onClick for the right triangle
    else
      /// go to onClick for the left triangle
  }
}

public int calcPlace(int x){
  return 50 -x; 
}

You don't have to set onClickListener for the two triangles (ImageViews), just have a method that handles the clicks.

Some fields might be wrong, sorry about it :) I hope you get the point.

honk
  • 9,137
  • 11
  • 75
  • 83
Yakir Yehuda
  • 650
  • 7
  • 23
0

It should be possible to create a View as the background, then put another one above it with a 45 degree angle. Put both inside a FrameLayout to clip it to a rect again. You may assign a onClick handler to each of them.

Jonas Köritz
  • 2,606
  • 21
  • 33
-1

You can set the background by xml. Make the abc image of the same background in a different size:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/abc"
        android:orientation="vertical" >
</LinearLayout>
honk
  • 9,137
  • 11
  • 75
  • 83