I have two view as the picture shows, the first I drew a circle and lines, the second I have plenty of buttons, I want to superimpose the two view so that each line points to a buttons
enter image description here "Canvas"
[enter image description here]2
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView (Context context) {
super (context) ;
}
#Override
protected void onDraw (Canvas canvas){
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius ;
radius = 50 ;
Paint paint = new Paint () ;
Paint paint1 = new Paint () ;
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
canvas.drawCircle(x / 2, y / 2, radius, paint);
int margin = 500;
int margin1 = 300;
int top = 0 + margin;
int bottom = canvas.getHeight() - margin;
int left = 0 + margin1;
int right = canvas.getWidth() - margin1;
int centerX = x / 2;
int centerY = y / 2;
canvas.drawLine(centerX, top, centerX, bottom,paint1);
canvas.drawLine(left, centerY, right, centerY,paint1);
}}}
RelativeLayout 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">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button1" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_alignParentEnd="true"
android:id="#+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/button1"
android:id="#+id/button4" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp"
android:id="#+id/button5" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_alignStart="#+id/button3"
android:id="#+id/button6" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignParentStart="true"
android:layout_marginBottom="58dp"
android:id="#+id/button7" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button7"
android:layout_alignParentEnd="true"
android:id="#+id/button8" />
</RelativeLayout>
Won't this work? It'll add 4 buttons at the centers horizontally and vertically at the ends of the lines that you've drawn.
<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">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button1" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:id="#+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/button4" />
</RelativeLayout>
Related
I am using this class as item decoration in a horizontal RecyclerView (#2), that is also included in a vertical recyclerView(#1):
holder.recyclerView.addItemDecoration(new CirclePagerIndicatorDecoration());
CirclePagerIndicatorDecoration.java:
public class CirclePagerIndicatorDecoration extends RecyclerView.ItemDecoration {
private int colorActive = 0xDEE54242;
private int colorInactive = 0x33000000;
private static final float DP = Resources.getSystem().getDisplayMetrics().density;
private final int mIndicatorHeight = (int) (DP * 16);
private final float mIndicatorStrokeWidth = DP * 4;
private final float mIndicatorItemLength = DP * 4;
private final float mIndicatorItemPadding = DP * 8;
private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
private final Paint mPaint = new Paint();
public CirclePagerIndicatorDecoration() {
mPaint.setStrokeWidth(mIndicatorStrokeWidth);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
int itemCount = parent.getAdapter().getItemCount();
// center horizontally, calculate width and subtract half from center
float totalLength = mIndicatorItemLength * itemCount;
float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
float indicatorTotalWidth = totalLength + paddingBetweenItems;
float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2F;
// center vertically in the allotted space
float indicatorPosY = parent.getHeight() - mIndicatorHeight * 2F;
drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount);
// find active page (which should be highlighted)
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
int activePosition = layoutManager.findFirstVisibleItemPosition();
if (activePosition == RecyclerView.NO_POSITION) {
return;
}
// find offset of active page (if the user is scrolling)
final View activeChild = layoutManager.findViewByPosition(activePosition);
int left = activeChild.getLeft();
int width = activeChild.getWidth();
int right = activeChild.getRight();
// on swipe the active item will be positioned from [-width, 0]
// interpolate offset for smooth animation
float progress = mInterpolator.getInterpolation(left * -1 / (float) width);
drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, progress);
}
private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
mPaint.setColor(colorInactive);
// width of item indicator including padding
final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
float start = indicatorStartX;
for (int i = 0; i < itemCount; i++) {
c.drawCircle(start, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
start += itemWidth;
}
}
private void drawHighlights(Canvas c, float indicatorStartX, float indicatorPosY,
int highlightPosition, float progress) {
mPaint.setColor(colorActive);
// width of item indicator including padding
final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
if (progress == 0F) {
// no swipe, draw a normal indicator
float highlightStart = indicatorStartX + itemWidth * highlightPosition;
c.drawCircle(highlightStart, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
} else {
float highlightStart = indicatorStartX + itemWidth * highlightPosition;
// calculate partial highlight
float partialLength = mIndicatorItemLength * progress + mIndicatorItemPadding*progress;
c.drawCircle(highlightStart + partialLength, indicatorPosY, mIndicatorItemLength / 2F, mPaint);
}
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.bottom = mIndicatorHeight;
}
}
There is an issue with this code.
When scrolling down recyclerView #1, the circle indicators from recyclerView #2 are not always shown at the same place in recyclerview #2 .
Example: Item 1 from recyclerView #1 and circle pager indicators from item 1 from recyclerView #2.
Example: Item 5 from recyclerView #1 and circle pager indicators from item 1 from recyclerView #2.
And a second question is how to change the position from the circle indicators to the top side of the recyclerView #2.
EDIT: RecyclerView #1 layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/colorWhite"
android:padding="10dp"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_account_circle_black_24dp"
card_view:civ_border_color="#FF000000"
card_view:civ_border_width="2dp" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_toRightOf="#id/profile_image"
android:text="Sample"
android:textColor="#color/grey_700"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtUsername"
android:layout_alignStart="#id/txtUsername"
android:layout_toRightOf="#id/profile_image"
android:text="TextView"
android:textSize="10sp" />
<LinearLayout
android:id="#+id/layout_deportes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/txtUsername"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#id/txtUsername"
android:orientation="horizontal">
<ImageView
android:id="#+id/sp1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp6"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#+id/layout_deportes"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp2"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp3"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp4"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp5"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp7"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/sp8"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:visibility="visible"
card_view:srcCompat="#drawable/skate" />
</LinearLayout>
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_image"
android:layout_marginStart="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:minLines="3"
android:paddingRight="40dp"
android:text="Vivimos rodeados de objetos cotidianos que, a pesar de verlos cada día, todavía no tenemos muy claro para qué sirven. Es el caso del cuadrado con dos ranuras que tienen algunas mochilas, el bolsillo pequeño de los vaqueros, sus remaches de cobre e incluso las siglas 'YKK' que aparecen en tu cremallera. ¿Alguna vez te habías preguntado para qué sirven los agujeros laterales de algunas zapatillas? "
android:textAlignment="textStart"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/addUser"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/layout_deportes"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
card_view:srcCompat="#drawable/home_add_user" />
<ImageView
android:id="#+id/tres"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/addUser"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
card_view:srcCompat="#drawable/home_submenu" />
<ImageView
android:id="#+id/ivAmbassador"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="#id/profile_image"
android:layout_alignBottom="#id/profile_image"
android:layout_marginLeft="50dp"
card_view:srcCompat="#drawable/home_ambassador" />
<ImageView
android:id="#+id/spot_sport"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
card_view:srcCompat="#drawable/skate" />
<ImageView
android:id="#+id/tipo_spot"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/spot_sport"
android:fadeScrollbars="true"
card_view:srcCompat="#drawable/negocio" />
<ImageView
android:id="#+id/spot_map"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/txtDescription"
android:layout_alignBottom="#id/tipo_spot"
android:layout_marginLeft="9dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/tipo_spot"
android:fadeScrollbars="true"
android:foregroundGravity="bottom"
card_view:srcCompat="#drawable/home_pin_mapa" />
<TextView
android:id="#+id/spot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtDescription"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#id/verified"
android:layout_toRightOf="#id/spot_map"
android:ellipsize="end"
android:maxLines="1"
android:minLines="1"
android:text="Skatepark Macba, Barcelona, Spain"
android:textSize="12sp" />
<ImageView
android:id="#+id/verified"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignBottom="#id/spot_map"
android:layout_marginLeft="24dp"
android:layout_marginEnd="5dp"
android:layout_toLeftOf="#id/txtVerified"
card_view:srcCompat="#drawable/home_ambassador" />
<TextView
android:id="#+id/txtVerified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/verified"
android:layout_alignParentEnd="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="-18dp"
android:layout_marginEnd="25dp"
android:text="Verified"
android:textAlignment="textEnd"
android:textSize="12sp" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_below="#id/spot_sport"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:visibility="gone" >
</com.daimajia.slider.library.SliderLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerMarcas"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#id/spot_sport" />
<ImageView
android:id="#+id/fire"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
card_view:srcCompat="#drawable/home_fire_activado" />
<ImageView
android:id="#+id/comments"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="#id/fire"
card_view:srcCompat="#drawable/home_comentarios" />
<ImageView
android:id="#+id/fixpin"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/recyclerMarcas"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="#id/comments"
card_view:srcCompat="#drawable/home_check" />
<TextView
android:id="#+id/txtEstadoAqui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recyclerMarcas"
android:layout_alignBottom="#id/fixpin"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/fixpin"
android:gravity="center_horizontal|center_vertical"
android:lines="2"
android:text="He estado aqui"
android:textSize="14sp" />
<TextView
android:id="#+id/txtComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recyclerMarcas"
android:layout_alignBottom="#id/fixpin"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="#id/fireresult"
android:layout_toRightOf="#id/txtEstadoAqui"
android:gravity="center_horizontal|center_vertical"
android:lines="2"
android:text="10 comments"
android:textAlignment="viewEnd"
android:textSize="14sp" />
<ImageView
android:id="#+id/fireresult"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="#id/recyclerMarcas"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginTop="-10dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="10dp"
card_view:srcCompat="#drawable/home_fire_activado" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
I have solved the issue just putting the circle indicator at the top of the recyclerView.
float indicatorPosY = 40;
Now its position doesn't change at any item.
Here is my problem. I have 6 LinearLayouts wich are all in centerVertical and centerHorizontal. And now I want them move, when the Activity starts, to all corners and sides, each. ( In each corner should be one Layout and on the left center and on the right center ). I achieve this, for Example, with this TranslateAnimation :
xbox360animation = new TranslateAnimation(0.0f, distanceWidth,
0.0f, 0.0f);
xbox360animation.setDuration(1500);
xbox360animation.setFillAfter(true);
xbox360Layout.startAnimation(xbox360animation);
animationxboxone = new TranslateAnimation(0.0f, -distanceWidth,
0.0f, 0.0f);
animationxboxone.setDuration(1500);
animationxboxone.setFillAfter(true);
xboxOneLayout.startAnimation(animationxboxone);
animationps3 = new TranslateAnimation(0.0f, distanceWidth,
0.0f, -distanceHeight);
animationps3.setDuration(1500);
animationps3.setFillAfter(true);
ps3Layout.startAnimation(animationps3);
animationps4 = new TranslateAnimation(0.0f, -distanceWidth,
0.0f, -distanceHeight);
animationps4.setDuration(1500);
animationps4.setFillAfter(true);
ps4layout.startAnimation(animationps4);
animationwiiu = new TranslateAnimation(0.0f, distanceWidth,
0.0f, distanceHeight);
animationwiiu.setDuration(1500);
animationwiiu.setFillAfter(true);
wiiuLayout.startAnimation(animationwiiu);
animationpc = new TranslateAnimation(0.0f, -distanceWidth, 0.0f, distanceHeight);
animationpc.setDuration(1500);
animationpc.setFillAfter(true);
pcLayout.startAnimation(animationpc);
My XML File:
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="#id/header"
android:layout_above="#id/nextBtn"
android:layout_marginLeft="36dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="36dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/xboxOneLayout"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/xboxOne"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/xbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xbox One"
android:id="#+id/xboxOnetv"
android:visibility="invisible"
android:duplicateParentState="true"
android:textColor="#fff"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/xbox360Layout"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/xbox360"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/xbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xbox 360"
android:id="#+id/xbox360tv"
android:textColor="#fff"
android:duplicateParentState="true"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ps3Layout"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ps3"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/playstation"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ps3"
android:duplicateParentState="true"
android:id="#+id/ps3tv"
android:textColor="#fff"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ps4Layout"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ps4"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/playstation"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ps4"
android:duplicateParentState="true"
android:id="#+id/ps4tv"
android:textColor="#fff"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/wiiuLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/wiiu"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/wii"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiiU"
android:duplicateParentState="true"
android:id="#+id/wiiutv"
android:visibility="invisible"
android:textColor="#fff"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/pcLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/pc"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/desktop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PC"
android:duplicateParentState="true"
android:textColor="#fff"
android:id="#+id/pctv"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</RelativeLayout>
I can't click on them after the animation. When I click on the position where it should be before the Animation, I get the onclick on one of the Layouts.
How can I set the Layouts to the new position, so I recieve the onClick Event from the right Layout?
Here i am showing you example of Gesture Listener in Code i have using image View but you can change it into Linear Layout
public class MainActivity extends Activity {
private ImageView iv;
private Matrix matrix = new Matrix();
private float scale = 1f;
private ScaleGestureDetector SGD;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView);
SGD = new ScaleGestureDetector(this,new ScaleListener());
}
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5.0f));
matrix.setScale(scale, scale);
iv.setImageMatrix(matrix);
return true;
}
}
}
TranslateAnimation only moves the pixels on the screen, and does not really move your LinearLayouts, it only makes it look like it is moving, so an OnClickListener will not animate with it. You still should be able to click at the old position before the animation.
Try ObjectAnimator or ViewPropertyAnimator.
Example with ViewPropertyAnimator for your xbox 360 layout:
xbox360Layout.animate().translationX(distanceWidth).setDuration(1500);
Now your layout really moves on the screen and your OnClick should work on the new position.
i want clickable buttons in zoomview .but my problem is that when i set the view in
zoomview then buttons are not responding while click,i am stuck at this point any help
is apreciated
my code is
public class SongsActivity extends Activity {
ImageView imvw;
DrawView drawView;
ImageView imageView;
View dv;
RelativeLayout rdown;
private static final float MIN_ZOOM = 1f, MAX_ZOOM = 1f;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
private static final RelativeLayout New = null;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
RelativeLayout main_container;
private ZoomView zoomView;
Button BtnWelcome;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_layout);
try{
imvw = (ImageView) findViewById(R.id.imageView1);
View v = ((LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.songs_layout, null, false);
BtnWelcome = (Button) findViewById(R.id.button4);
v.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
main_container = (RelativeLayout) findViewById(R.id.Map);
zoomView = new ZoomView(this);
zoomView.addView(v);
main_container.addView(zoomView);
//BtnWelcome.bringToFront();
BtnWelcome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SongsActivity.this,
Wavepointsactivity.class);
intent.putExtra("Welcome", "Welcome");
startActivity(intent);
}
});
}catch (Exception e) {
// TODO: handle exception
}
}
}
and my xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Screen Design for the SONGS -->
<RelativeLayout
android:id="#+id/tabconteffnt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:background="#2E2E2E"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Cedar Breaks-Map"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:layout_marginRight="37dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#+id/button4"
android:layout_toLeftOf="#+id/button4"
android:text="Button" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button5"
android:layout_marginTop="18dp"
android:text="Button" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/button6"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Button" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/button2" />
<Button
android:id="#+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/button9"
android:text="Button" />
<Button
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/button9"
android:layout_alignRight="#+id/button11"
android:text="Button" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button10"
android:layout_alignBottom="#+id/button10"
android:text="Button" />
<Button
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_alignLeft="#+id/button8"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/button3" />
<RelativeLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/view1" >
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
There is some problem in initialising your id.button1 is not declared in your xml.So it is a null pointer exception.
BtnWelcome = (Button) findViewById(R.id.button1);
Give proper id of the button.It may resolve the error..
This question already has answers here:
Vertical (rotated) label in Android
(10 answers)
Closed 1 year ago.
I am trying to create something like the following using LinearLayout and TableLayout:
I am stuck on Vertical Text, though. It is taking more space than it should take after rotation.
The following is the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_CONSEQUENCES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rotation="-90"
android:text="CONSEQUENCES"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
</LinearLayout>
and the output is like this:
The vertical text is taking more space than it should need, and not all of the text is appearing. :(
You can use code below:
android:rotation="270"
None of the solutions worked for me but I found this blog post by Mark Allison:
https://blog.stylingandroid.com/verticaltext-part-1/
public class VerticalTextView extends TextView
{
final boolean topDown;
public VerticalTextView( Context context,
AttributeSet attrs )
{
super( context, attrs );
final int gravity = getGravity();
if ( Gravity.isVertical( gravity )
&& ( gravity & Gravity.VERTICAL_GRAVITY_MASK )
== Gravity.BOTTOM )
{
setGravity(
( gravity & Gravity.HORIZONTAL_GRAVITY_MASK )
| Gravity.TOP );
topDown = false;
}
else
{
topDown = true;
}
}
#Override
protected void onMeasure( int widthMeasureSpec,
int heightMeasureSpec )
{
super.onMeasure( heightMeasureSpec,
widthMeasureSpec );
setMeasuredDimension( getMeasuredHeight(),
getMeasuredWidth() );
}
#Override
protected void onDraw( Canvas canvas )
{
TextPaint textPaint = getPaint();
textPaint.setColor( getCurrentTextColor() );
textPaint.drawableState = getDrawableState();
canvas.save();
if ( topDown )
{
canvas.translate( getWidth(), 0 );
canvas.rotate( 90 );
}
else
{
canvas.translate( 0, getHeight() );
canvas.rotate( -90 );
}
canvas.translate( getCompoundPaddingLeft(),
getExtendedPaddingTop() );
getLayout().draw( canvas );
canvas.restore();
}
}
The rotation is done by the gravity. So be sure to set this in your xml:
<com.stylingandroid.verticaltext.VerticalTextView
style="#style/verticalTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:text="#string/text" />
val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
textPaint.textSize=toPx(16)
textPaint.color=Color.parseColor("#FFFFFF")
canvas.save()
canvas.translate(50F, 60F)
canvas.rotate(90F)
canvas.drawPaint(textPaint)
canvas.drawText("YourText", 0F,0F, textPaint)
canvas.restore()
Here is a hack to create vertical text view.
So the scenario was I need to show a static tag on the right side of the screen.
so I just used "\n" for the new line as my text was less.
android:text="A\nC\nT\nI\nO\nN"
You can check the image I've attached. although it's not suggested if you showing data from Dynamically.
<TextView
android:id="#+id/tv_action"
android:layout_width="20dp"
android:layout_height="126dp"
android:background="#drawable/bg_red_left_rcorner"
android:gravity="center"
android:padding="2dp"
android:textStyle="bold"
android:text="A\nC\nT\nI\nO\nN"
android:textColor="#color/white"
app:layout_anchor="#+id/drawer_layout"
app:layout_anchorGravity="end|center" />
Put all the Text view in Linear Layout and try to give proper Layout weight to solve your problem
android:rotation="-90"
android:layout_gravity="center"
Worked for me.
in .xml file just put
<TextView>
android:layout_width="wrap content"
android:layout_height="wrap content"
android:rotation="270"
</TextView>
why not put padding?so it will go in the center of your screen.just try it out if you like
If you want to reproduce your image I would use a GridView or a TableLayout with no buttons, just set the proper listeners if you need to do any action. This way you can set fixed heights and widths.
I want to create a custom view like this.
I tried the following
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/customView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/sample_image" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:text="Button" />
</FrameLayout>
How can i create a view like this? How can i place buttons over imageview like this?
Thanks in Advance
you can try to use relative layout to do this,
for btn1;
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
for btn1;
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
[EDIT 1]
to give space for button use margin, android:layout_margin="20dp"
Example layout
<RelativeLayout android:layout_width="300dp"
android:layout_height="300dp">
<ImageView
android:id="#+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffaadd"
android:layout_margin="20dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="btn1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="btn2" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:padding="60dp"
android:src="#drawable/abs__ab_stacked_solid_dark_holo" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:background="#drawable/ic_launcher" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="32dp"
android:layout_marginTop="102dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
Use a RelativeLayout. This will allow you to have different Views overlap on the screen.
Here is a tutorial I always use http://www.learn-android.com/2010/01/05/android-layout-tutorial/
You should find what you need. It is good explained, so you shouldn't have any problems.
This solved for me
<?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/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "#drawable/basic"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button1"
android:layout_alignEnd="#+id/button1" />
<ImageButton
android:id="#+id/button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="btn1"
android:background="#drawable/ic_cancel_black"/>
</RelativeLayout>
This is my solution programatically, for diferent size of devices.
I have three buttons that i have to positioning in the right place of an image 1080x1920
on positions (157,927), 387,927), (617,927)
final ImageView iv = (ImageView)findViewById( R.id.imageView );
iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// Once data has been obtained, this listener is no longer needed, so remove it...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
iv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
RectF bounds = new RectF();
Drawable drawable = iv.getDrawable();
if (drawable != null) {
iv.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds()));
}
float x = bounds.left;
float y = bounds.top;
float new_width = bounds.right - x;
float new_height = bounds.bottom - y;
int original_width = 1080;
int original_height = 1920;
int x1 = (int) (x + (new_width*157/original_width));
int x2 = (int) (x + (new_width*387/original_width));
int x3 = (int) (x + (new_width*617/original_width));
int newY = (int) (y + (new_height*927/original_height));
enroll.setX(x1);
ccfront.setX(x2);
verify.setX(x3);
enroll.setY(newY);
ccfront.setY(newY);
verify.setY(newY);
}
});