Java Android Change Position of an ImageView dynamically - java

I am new in program Java/Android and got a small problem.
I searched why my code doesn't work but I didn't find an answer - so here is my Problem:
I want to change the position of an ImageView at runtime by a Button. I have two buttons - left and right. But the curious thing is - only one is working (the button for moving right). If I click the button for moving left nothing changed.
I hope someone can help me because I searched a long time for this detail.
Tanks a lot for any help!
Here is the Code:
package de.androidnewcomer.bildattribute;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button leftButton=(Button)findViewById(R.id.buttonLeft);
leftButton.setOnClickListener(this);
Button rightButton=(Button)findViewById(R.id.buttonRight);
rightButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonLeft:
moveLeft();
case R.id.buttonRight:
moveRight();
}
}
private void moveLeft() {
ImageView ball=(ImageView)findViewById(R.id.ball);
FrameLayout.LayoutParams paramsLeft=(FrameLayout.LayoutParams)ball.getLayoutParams();
paramsLeft.width=50;
paramsLeft.height=50;
paramsLeft.rightMargin +=20;
ball.setLayoutParams(paramsLeft);
}
private void moveRight() {
ImageView ball=(ImageView)findViewById(R.id.ball);
FrameLayout.LayoutParams paramsRight=(FrameLayout.LayoutParams) ball.getLayoutParams();
paramsRight.width=50;
paramsRight.height=50;
paramsRight.leftMargin +=20;
ball.setLayoutParams(paramsRight);
}
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="de.androidnewcomer.bildattribute.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/layout1">
<Button
android:text="Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonLeft"
android:layout_gravity="bottom|left"
android:layout_marginLeft="50dp" />
<Button
android:text="Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonRight"
android:layout_gravity="bottom|right"
tools:layout_marginRight="50dp" />
<ImageView
app:srcCompat="#drawable/image1"
android:id="#+id/image1"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_height="20dp"
android:layout_width="20dp" />
</FrameLayout>

The problem is in your onClick() - The break; statements are missing:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonLeft:
moveLeft();
break; // MISSING
case R.id.buttonRight:
moveRight();
break; // MISSING
}
}
Without the break statement , when you hit buttonLeft, you perform both moveLeft() & moveRight() both , which result in no change in the layout.

Even though you need to modify you onClick method as it has been said, your moveLeft method should update the left margin (just as moveRight)
private void moveLeft() {
ImageView ball=(ImageView)findViewById(R.id.ball);
FrameLayout.LayoutParams paramsLeft=(FrameLayout.LayoutParams)ball.getLayoutParams();
paramsLeft.width=50;
paramsLeft.height=50;
paramsLeft.leftMargin -=20;
ball.setLayoutParams(paramsLeft);
}
You can experiment on your layout and see what happens if you add both left and right margin to an element, surely it is not what you expect.

Related

How to align imageView after change by button

I started learning making Android Apps, and I have stumbled upon a problem.
I have made my app to change an imageView by a button
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring); // changes the image
so, now, when I click on the button, it changes an image.
The problem is, the original image was centered, and the image that replaces it is set on top left. I can't center it, or move it or anything. I guess I am missing a tiny detail somewhere.
MainActivity.java:
package com.maxa.testaplikacija;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/udajSe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bttOK"
android:layout_centerHorizontal="true"
android:text="#string/udaj_se_za_mene" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:adjustViewBounds="false"
android:contentDescription="#string/poklon"
android:foregroundGravity="center_horizontal"
app:srcCompat="#drawable/present" />
<Button
android:id="#+id/bttOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="115dp"
android:text="#string/otvori_me" />
</RelativeLayout>
I am still new to this, but I am a quick learner.
Inside the onclick method align the imageView like this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
imageView.setLayoutParams(params);
}
});
Your view seems to have the image set to top left, see these XML tags in your ImageView:
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
Are you sure the original image files aren't spaced differently / have extra padding as part of the image itself?

Android: Display images in full screen with caption on `onClick` event

I'm a beginner to the android development. I have an android image view that import web image by using Picasso 2.5.2 library. It's working. After click on that imageview, I want to create a full screen Android dialog with that image. I' used following code for it. But after click on the image view, a dialog is displayed without full screen. Finally, I want to zoom in and zoom out that full screen dialog. I want this kind of full screen on imageview onClickListener. Also see how the below image has a title and I also need the image caption (like in Facebook)
Here is my code
Mainactivity.java
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
ImageView imageView1;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}
public void initUi(){
imageView1 = (ImageView)findViewById(R.id.image);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView1);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showImage();
}
});
}
public void showImage() {
Dialog builder = new Dialog(this, android.R.style.Theme_Light);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(this);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
builder.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You can use Chrisbane's PhotoView lib for zooming the image.
Open the detail page by sending the path,
public void showImage() {
Intent intent = new Intent(this, ImageDeatilActvity.class);
intent.putExtra("path", "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
startActivity(intent);
}
Complete ImageDeatilActvity.java
public class ImageDeatilActvity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener {
private boolean mIsFullScreen;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_deatil_actvity);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
mIsFullScreen = true;
String path = getIntent().getStringExtra("path");
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
#Override
public void onPhotoTap(View view, float x, float y) {
updateView();
}
#Override
public void onOutsidePhotoTap() {
updateView();
}
});
Glide.with(this).load(path).into(photoView);
}
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
mIsFullScreen = false;
}
updateView();
}
public void updateView() {
mIsFullScreen = !mIsFullScreen;
if (mIsFullScreen) {
hideSystemUI();
} else {
showSystemUI();
}
}
private void hideSystemUI() {
mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
}
private void showSystemUI() {
mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Detail Activity Layout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.muthu.stackoverflow.ImageDeatilActvity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#33000000"
android:fitsSystemWindows="true"
app:contentScrim="#android:color/transparent"
app:popupTheme="#style/AppTheme.AppBarOverlay"/>
<uk.co.senab.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In My example, I am using Glide not Picaso, because Android
official recommendation is Glide, If you want you can change that.
set image to centre crop and resize it with your screen size like
Picasso.with(MainActivity.this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Use below xml for layout, i added one more ImageView just use make the visibility visible on click of small image_view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>

TextView and scale

I use this code
TextView.animate().setDuration(0).scaleX(scale).scaleY(scale).start();
When scale value increase to some point, it make TextView disappear. I dont know why, and how to prevent it?
[Edit] Attach my test code
[test_text_scale.xml] It is Layout xml file for TestTextScale.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:orientation="vertical">
<TextView
android:id="#+id/tv_test_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Test" />
<Button
android:id="#+id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/bt_down"
android:layout_toRightOf="#+id/bt_down"
android:text="Up" />
<Button
android:id="#+id/bt_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Down" />
</RelativeLayout>
[TestTextScale.java]
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestTextScale extends AppCompatActivity {
// View
private TextView mTvTestZoom;
private Button mBtUp, mBtDown;
// Model
private int mCurrentScale = 1;
// OnClick listener
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_up : {
upScale();
setText();
} break;
case R.id.bt_down : {
downScale();
setText();
} break;
}
}
};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_text_scale);
initView();
initEvent();
}
private void initView() {
mTvTestZoom = (TextView) findViewById(R.id.tv_test_scale);
mBtUp = (Button) findViewById(R.id.bt_up);
mBtDown = (Button) findViewById(R.id.bt_down);
}
private void initEvent() {
mBtDown.setOnClickListener(mOnClickListener);
mBtUp.setOnClickListener(mOnClickListener);
}
private void upScale() {
mCurrentScale += 1;
mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start();
}
private void downScale() {
mCurrentScale -= 1;
mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start();
}
private void setText() {
mTvTestZoom.setText(mCurrentScale + "");
}
}
This may happen because the textview exceeds the layout bounds of its parent. To overcome this, simple put android:clipChildren="false" in the xml description of the immediate parent of the textview. You may also need to do it for their parents too.
EDIT:
Upon testing the code myself, I found that the textview did disappear with following log message:
E/OpenGLRenderer: Font size too large to fit in cache. width, height = 635, 1028
This is due to a possible issue in the Android Hardware acceleration modules. One of the ways to avoid this is:
mTvTestZoom.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
This works well and the textView does not disappear after a certain size but, since it disables hardware acceleration for the specific textview, it may lose antialiasing.

How to zoom second TouchImageView on setOnTouchImageViewListener event of first TouchImageView?

I have two TouchImageView. What I want to do is that, When I zoom in first TouchImageView, second one also get zoom in automatically. And when I swipe from left to right or right to left on first TouchImageView, second one also need to be get swiped automatically but in reverse order. i.e If I swipe first TouchImageView from left to right, second one need to be get swiped automatically right to left. I am using TouchImageView class. My code is below. Thanks in advance.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed_mirror"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
</LinearLayout>
MainActivity.java
package com.PinchZoom.pinchzoomexampletwo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.PinchZoom.pinchzoomexampletwo.TouchImageView.OnTouchImageViewListener;
public class MainActivity extends ActionBarActivity {
TouchImageView img_to_be_zoomed;
TouchImageView img_to_be_zoomed_mirror;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_to_be_zoomed_mirror = (TouchImageView) findViewById(R.id.img_to_be_zoomed_mirror);
img_to_be_zoomed = (TouchImageView) findViewById(R.id.img_to_be_zoomed);
img_to_be_zoomed
.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_mirror.setZoom(img_to_be_zoomed
.getCurrentZoom());
img_to_be_zoomed_mirror.setScrollPosition(
img_to_be_zoomed.getScaleX(), 0);
Log.e("Left Img X: ", "" + img_to_be_zoomed.getScaleX());
}
});
}
}
TouchImageView.java
TouchImageView.java Find Here
I have achieved it for Zoom, but not getting for swiping.

how to draw line between buttons in android

i want to draw line between middle point of two buttons.i created that useing below code i check it in my android emulator it is working correctly.i put that apk to my phone but those lines are not in correct position.
how to position line between two middle points of two buttons?
it should be applicable for every android phones with different screen size
this is how i want to draw lines
![IMG]http://i58.tinypic.com/1o7hj9.png[/IMG]
[![android emulator]1]1
//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffaa" >
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_weight="1"
android:id="#+id/bx1y1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="" />
<Button
android:layout_weight="1"
android:id="#+id/bx2y1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_weight="1"
android:id="#+id/bx1y2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="" />
<Button
android:layout_weight="1"
android:id="#+id/bx2y2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
//MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.graphics.Color;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
DrawView drawView;
Button bx1y1,bx2y1,bx1y2,bx2y2;
RelativeLayout rl1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bx1y1 = (Button) findViewById(R.id.bx1y1);
bx2y1 = (Button) findViewById(R.id.bx2y1);
bx1y2 = (Button) findViewById(R.id.bx1y2);
bx2y2 = (Button) findViewById(R.id.bx2y2);
rl1 =(RelativeLayout)findViewById(R.id.rl1);
bx2y1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
drawline(1);
}
});
bx1y2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
drawline(3);
drawline(6);
}
});
bx2y2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
drawline(2);
drawline(4);
drawline(5);
}
});
}
protected void drawline(int linenum) {
switch (linenum) {
case 1://x start
drawView = new DrawView(MainActivity.this,bx1y1,bx2y1,18,18,18,18);
rl1.addView(drawView);
break;
case 2:
drawView = new DrawView(MainActivity.this,bx1y2,bx2y2,18,54,18,54);
rl1.addView(drawView);
break;
case 3:
drawView = new DrawView(MainActivity.this,bx1y1,bx1y2,18,18,18,54);
rl1.addView(drawView);
break;
case 4:
drawView = new DrawView(MainActivity.this,bx2y1,bx2y2,18,18,18,54);
rl1.addView(drawView);
break;
case 5:
drawView = new DrawView(MainActivity.this,bx1y1,bx2y2,18,18,18,54);
rl1.addView(drawView);
break;
case 6:
drawView = new DrawView(MainActivity.this,bx2y1,bx1y2,18,18,18,54);
rl1.addView(drawView);
break;
default:
break;
}
}
}
//DrawView
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
View startView;
View endView;
int sx,sy,ex,ey;
public DrawView(Context context,View startView,View endView,int sx,int sy,int ex,int ey) {
super(context);
paint.setColor(Color.BLACK);
this.startView = startView;
this.endView = endView;
this.sx=sx;
this.sy=sy;
this.ex=ex;
this.ey=ey;
}
#SuppressLint("NewApi")
public void onDraw(Canvas canvas) {
canvas.drawLine(startView.getX()+sx, startView.getY()+sy, endView.getX()+ex, endView.getY()+ey, paint);
}
}
<View android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/white" />
This will create a horizontal line that is 2dp high. Add this between your two buttons, with appropriate positioning in your RelativeLayout
The other option would be to use a shape drawable if you want a more customized line between the two buttons.

Categories