Reputation: 11
its the left circle unpressed pressed
so i have imageview = (shaped drawble src), when i touch it change the drawble res to other so the color change .but i also want to bring it above other imageviews how can i do that i try bringToFront but is not working, is there another way to do it ??
I want the same for ALL of them when i pressed one imageview its be in front of others.
final ImageView imageView = (ImageView) findViewById(R.id.cir);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.cir_press);
break;
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.cir);
break;
}
}
return false;
}
});
<RelativeLayout
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom|center">
<ImageView
android:id="@+id/cir"
android:elevation="4dp"
android:layout_width="184dp"
android:layout_height="313dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_marginBottom="43dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir2"
android:layout_width="150dp"
android:layout_height="186dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="211dp"
android:layout_marginLeft="211dp"
android:layout_marginTop="38dp"
android:layout_marginBottom="132dp"
android:gravity="center"
android:padding="5dp"
android:src="@drawable/cir2"
/>
<ImageView
android:id="@+id/cir3"
android:layout_width="205dp"
android:layout_height="140dp"
android:layout_alignBottom="@+id/cir2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="-127dp"
android:padding="5dp"
android:src="@drawable/cir3" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="231dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="38dp"
android:gravity="bottom">
<ImageView
android:id="@+id/cir4"
android:layout_width="wrap_content"
android:layout_height="212dp"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:elevation="10dp"
android:gravity="bottom|center"
android:src="@drawable/cir4"
/>
</RelativeLayout>
</RelativeLayout>
Upvotes: 0
Views: 113
Reputation: 345
I might be late for answer but I've noticed that you have given elevations
in your layout. imageView.bringToFront()
won't work if you will give your view different elevations
,also your @+id/layoutTop
is set to match_parent
that may cause imageView.setOnTouchListener
to stop working so I've adjusted your layout like this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:gravity="bottom|center">
<ImageView
android:id="@+id/cir"
android:layout_width="184dp"
android:layout_height="313dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_marginBottom="43dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir2"
android:layout_width="150dp"
android:layout_height="186dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="211dp"
android:layout_marginLeft="211dp"
android:layout_marginTop="38dp"
android:layout_marginBottom="132dp"
android:gravity="center"
android:padding="5dp"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir3"
android:layout_width="205dp"
android:layout_height="140dp"
android:layout_alignBottom="@+id/cir2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="-127dp"
android:padding="5dp"
android:src="@drawable/cir"/>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="wrap_content"
android:layout_height="231dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:gravity="bottom">
<ImageView
android:id="@+id/cir4"
android:layout_width="wrap_content"
android:layout_height="212dp"
android:layout_alignWithParentIfMissing="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:gravity="bottom|center"
android:src="@drawable/cir"
/>
</RelativeLayout>
</RelativeLayout>
Also since you've used RelativeLayout
as topLayout in-order to bring imageView(cir)
to back of topLayout you will have to bring @+id/layoutTop
to front while in unpressed state consider following code:
final ImageView imageView = (ImageView) findViewById(R.id.cir);
final ImageView topLayout = (ImageView) findViewById(R.id.layoutTop);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.cir_press);
imageView.bringToFront();
break;
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.cir);
topLayout.bringToFront();
break;
}
}
return false;
}
});
Upvotes: 1
Reputation: 577
use RelativeLayout as the parent of the imageViews and on each imageView click call view.bringToFront()
Upvotes: 0
Reputation: 23
imgView.setVisibility(View.VISIBLE);
imgView.setVisibility(View.INVISIBLE);
imgView.setVisibility(View.GONE);
OR
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:src="@drawable/your image" />
Upvotes: 1