3

What I have done:

Figure 1

What I am looking for:

Figure 2

I do not care about design, but I do not know how to connect buttons to Main button with lines that are similar to the second image. Note: I am creating buttons dynamically. Thus, I do not use XML file as I do not know how many lines/buttons I will have.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);
    //
    RelativeLayout FirstLayout = (RelativeLayout)findViewById(R.id.second_layou);
    RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton = new Button(this);
    yourButton.setText("1");
    yourButton.setWidth(2);
    parms1.setMargins(w, h+250, 0, 0);
    FirstLayout.addView(yourButton, parms1);
    //
    RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton2 = new Button(this);
    yourButton2.setText("2");
    yourButton2.setWidth(2);
    parms2.setMargins(w-300, h+300, 0, 0);
    FirstLayout.addView(yourButton2, parms2);
    // and so on with other buttons

Edit:

First: When I tried this answer by adding this code at the end of onCreate():

drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

a line view is shown but buttons are gone! and I want the line to be drawn from the center of MAIN button to center of other buttons not from fixed points.

Second: when I add the same code before defining the buttons I got runtime error, and this is logcat:

01-29 15:25:25.956 26170-26170/com.example.user.raywenderlich E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.example.user.raywenderlich, PID: 26170                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.raywenderlich/com.example.user.raywenderlich.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:155)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                                    at com.example.user.raywenderlich.SecondActivity.onCreate(SecondActivity.java:46)
                                                                                    at android.app.Activity.performCreate(Activity.java:5958)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:155) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5696) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

 

Community
  • 1
  • 1
Nora
  • 167
  • 2
  • 3
  • 14
  • your answer is from here http://stackoverflow.com/a/3616831/4848308 – Gueorgui Obregon Jan 28 '16 at 21:26
  • "got run time error" - Then post your logcat – OneCricketeer Jan 28 '16 at 22:50
  • @g2o I mentioned that I tried this answer, I will edit the question to give you details – Nora Jan 29 '16 at 12:03
  • 1
    `findViewById(R.id.second_layou)` is returning null because it cannot find that id within the content view you have defined for the Activity. Probably because you have done `setContentView(drawView);`, which means `findViewById` won't work anymore. – OneCricketeer Jan 29 '16 at 15:26
  • 1
    I believe that is also a typo because you have `setContentView(R.layout.second_layout);` above it. – OneCricketeer Jan 29 '16 at 15:28
  • @cricket_007 Thank you for your patience! I got the problem, but how to merge the layout with the view to show the line along with buttons? – Nora Jan 30 '16 at 12:59
  • 1
    Just add your new view directly on your current one, don't completely replace it – OneCricketeer Jan 30 '16 at 14:31
  • @cricket_007 Yes `FirstLayout.addView(drawView)` worked, thank you a lot! lastly if you could help please, how to make the line starts exactly from center of `MAIN` button to center of other buttons or as in the image? – Nora Jan 30 '16 at 14:57
  • You might have to do some math with getX and getY. I don't really know – OneCricketeer Jan 30 '16 at 16:06

1 Answers1

2

I actually needed something similar and was bummed that there was no answer to this.

After some searching and combining of information I got to this state:

Some textviews connected by curved lines.


What I did was take a FrameLayout, so the TextViews could be stacked on top of the drawing View. I made a custom drawing View as suggested in this tutorial: Basic Painting with Views.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_layout">

   <com.dev.example.CustomDrawingView
       android:id="@+id/custom_drawing_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

</FrameLayout>

Then I add the TextViews dynamically via code to the FrameLayout and use Path objects to draw the lines in the CustomDrawingView, from center to center of each TextView:

Path path = new Path();

int x1 = (int) firstView.getX() + firstView.getWidth() / 2;
int y1 = (int) firstView.getY() + firstView.getHeight() / 2;

int x2 = (int) secondView.getX() + secondView.getWidth() / 2;
int y2 = (int) secondView.getY() + secondView.getHeight() / 2;

path.moveTo(x1, y1);
path.lineTo(x2, y2);

Now the curve is made with the answer from this question: How to draw a curved line between 2 points on canvas?

Simply adjust the curveRadius in that answer to have different looking curves.

Hope this helps someone in the future.


EDIT: Oh and what's really important is to only call the onDraw method of the CustomDrawingView after all the layouts and views have been displayed. Because else the TextView's getX() & getY() methods still return 0 and you won't see any lines.
The suggested position to start drawing them was in the onWindowFocusChanged method of an activity.

Big_Chair
  • 2,781
  • 3
  • 31
  • 58