Ripple animation effect over MapView - android

Is there a way to create a ripple effect over the MapView?
My layout is presented in the end of the post.
I know about android:background="?attr/selectableItemBackground" and it works great on different kinds of Views, but not with the MapView.
What I want is the indivisible ripple effect over header_container and map_view.
To reach this I have tried putting them inside RelativeLayout and creating View with selectableItemBackground above them, but I don't like this solution because I could only make it work by specifying the exact height of that overlapping view and I want it to match the height of header and map.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:id="#+id/cv_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="#dimen/card_corner_radius"
card_view:cardElevation="#dimen/card_elevation"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
android:paddingLeft="#dimen/card_horizontal_padding"
android:paddingRight="#dimen/card_horizontal_padding">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_text_small_margin"
android:layout_marginTop="#dimen/card_text_big_margin"
android:text="Name"
android:textSize="#dimen/card_title_text_size" />
<TextView
android:id="#+id/tv_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_text_small_margin"
android:text="Address"
android:textSize="#dimen/card_subtitle_text_size" />
</LinearLayout>
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="#dimen/card_media_height"
map:liteMode="true"
map:mapType="none" />
<LinearLayout
android:id="#+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="#dimen/card_action_padding"
android:paddingTop="#dimen/card_action_padding">
<android.support.v7.widget.AppCompatButton
android:id="#+id/b_drive"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/card_action_padding"
android:layout_marginRight="#dimen/card_action_padding"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="#dimen/card_action_padding"
android:text="#string/card_action_drive"
android:textColor="#color/color_primary" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/b_walk"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="#dimen/card_action_padding"
android:text="#string/card_action_walk"
android:textColor="#color/color_primary" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>

the following class is a recipe to put any overlay on top of any view.
All you have to do is replace extends View to extends Anything and then call setOverlay(...); with the overlay (in you specific question a Ripple).
so in your case it will be:
extends MapView
and then
mapView.setOverlay(mapView.getResources().getDrawable(R.drawable.ripple));
and here is the "recipe" class
public class PressedStateView extends View {
public PressedStateView(Context context) {
super(context);
}
public PressedStateView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PressedStateView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public PressedStateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
private Drawable overlay;
public void setOverlay(Drawable overlay) {
if (this.overlay != null) {
this.overlay.setCallback(null);
}
this.overlay = overlay;
this.overlay.setCallback(this);
this.overlay.setBounds(0, 0, getWidth(), getHeight());
invalidate();
}
#Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.overlay.setBounds(0, 0, w, h);
}
#Override protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || who == overlay;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP) #Override
public void drawableHotspotChanged(float x, float y) {
super.drawableHotspotChanged(x, y);
if (overlay != null) {
overlay.setHotspot(x, y);
}
}
#Override protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (overlay != null) {
overlay.draw(canvas);
}
}
}

Related

Can we create a canvas programmatically from a view in Android

I am trying to create a dummy view from already existing View.
Original Image:
Need to create dummy view like this.
I tried with paint and canvas.
public class MyView extends View {
Paint paint;
Path path;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawRect(30, 50, 200, 350, paint);
// canvas.drawRect(100, 100, 300, 400, paint);
//drawRect(left, top, right, bottom, paint)
}
}
But I cannot draw like this. Because some time image will be circle or Ovel or any shape. So, I need to deduct the existing view and draw new view as same. Can anyone help me to create a dummy view from existing view?
I am trying to do this for shimmer animation only. For facebook shimmer I need to give the view inside the shimmerFramelayout. But My view will be dynamic. So, I need to create a dummy view programmatically for every time. For facebook Shimmer:
<com.facebook.shimmer.ShimmerFrameLayout
android:id="#+id/shimmerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--add several shimmer placeholder layout -->
<include layout="#layout/shimmer_placeholder_layout"></include>
<include layout="#layout/shimmer_placeholder_layout"></include>
<include layout="#layout/shimmer_placeholder_layout"></include>
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
Here shimmer_placeholder_layout is static view. I need to create dynamic view.
Finally got the solution.
Here I attached the solution for someone will get benefit on this.
MyView class:
public class MyView extends View {
Paint paint;
ViewGroup viewGroup;
Context context;
List<Rect> rectList = new ArrayList<>();
public MyView(Context context, ViewGroup viewGroup) {
super(context);
this.viewGroup = viewGroup;
this.context = context;
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(context.getResources().getColor(R.color.gray));
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
View child = viewGroup.getChildAt(i);
Rect rect = new Rect();
int x = (int) child.getX() - dpToPx(20);
int y = (int) child.getY() - dpToPx(20);
rect.left = x;
rect.top = y;
rect.right = x + child.getWidth();
rect.bottom = y + child.getHeight();
rectList.add(rect);
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Rect rect : rectList) {
canvas.drawRect(rect, paint);
}
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
}
My MainActivity:
ConstraintLayout constraintLayout;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = findViewById(R.id.parent_view);
imageView = findViewById(R.id.item_profile_img);
constraintLayout.post(() -> {
ShimmerFrameLayout shimmerFrameLayout = new ShimmerFrameLayout(MainActivity.this);
Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
.setDuration(2000)
.setBaseAlpha(0.9f)
.setHighlightAlpha(0.93f)
.setWidthRatio(1.5f)
.setDirection(Shimmer.Direction.RIGHT_TO_LEFT)
.setAutoStart(true)
.setBaseColor(getColor(android.R.color.darker_gray))
.setBaseColor(getColor(android.R.color.darker_gray))
.setHighlightColor(getColor(android.R.color.white))
.build();
shimmerFrameLayout.setShimmer(shimmer);
shimmerFrameLayout.addView(new MyView(MainActivity.this,constraintLayout));
constraintLayout.addView(shimmerFrameLayout);
shimmerFrameLayout.startShimmer();
});
}
My activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:ignore="HardcodedText,MissingConstraints"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
tools:ignore="HardcodedText,MissingConstraints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:id="#+id/parent_view">
<ImageView
android:id="#+id/item_profile_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription,MissingConstraints" />
<TextView
android:id="#+id/item_student_name_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Student name"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/item_profile_img" />
<TextView
android:id="#+id/item_student_college"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="8dp"
android:text="Student college"
app:layout_constraintStart_toEndOf="#+id/item_profile_img"
app:layout_constraintTop_toBottomOf="#+id/item_student_name_title" />
<TextView
android:id="#+id/item_student_specialization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="8dp"
android:text="Student specialization"
app:layout_constraintStart_toEndOf="#+id/item_profile_img"
app:layout_constraintTop_toBottomOf="#+id/item_student_college" />
<TextView
android:id="#+id/item_student_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="8dp"
android:text="Student description"
app:layout_constraintTop_toBottomOf="#+id/item_profile_img"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

How to draw a Line between two ImageViews by getting the positions of the ImageViews?

This is MainActivity.java code which gets the positions of the imagViews and tries to generate a lineView between the ImageViews by getting the coordinates of the ImageViews.
I'm getting the coordinates of the ImageViews but it's not showing the line between them.
public class MainActivity extends AppCompatActivity {
private ImageView one,two;
private LineView lineView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineView=findViewById(R.id.lineView);
one=findViewById(R.id.one);
two=findViewById(R.id.two);
Random random = new Random();
int x=random.nextInt(500);
int y=random.nextInt(500);
one.setX(x);
one.setY(y);
int c=x;
int d=y;
PointF pointA=new PointF(c,d);
x=random.nextInt(500);
y=random.nextInt(500);
two.setX(x);
two.setY(y);
c=x;
d=y;
PointF pointB=new PointF(c,d);
lineView.setPointA(pointA);
lineView.setPointB(pointB);
lineView.draw();
}
}
This is activity_main.xml that contains the ImageViews and LineView.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<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=".MainActivity"
>
<ImageView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="116dp"
android:layout_marginTop="197dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/one" />
<ImageView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="133dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/two" />
<com.example.testing.LineView
android:id="#+id/lineView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
This is the Lineview.java code that generates the line view widget and its operations .
public class LineView extends View {
PointF pointA,pointB;
private Paint paint =new Paint();
public LineView(Context context) {
super(context);
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
paint.setStrokeWidth(20);
canvas.drawLine(pointA.x,pointA.y,pointB.x,pointB.y,paint);
}
public void setPointA(PointF point)
{
pointA=point;
}
public void setPointB(PointF point)
{
pointB=point;
}
public void draw()
{
invalidate();
requestLayout();
}
}
You can do it in your view - put this View between your images:
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
The horizontal line between two image views in android
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF0000FF"/>
The vertical line between two image views in android
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#FF0000FF"/>

Linear Layout can not type cast to Keyword Layout

I am having one LinnearLayout and one Keyword layout. I want to type cast Linearlayout to Keyword layout but It gives exception.
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.codiant.prortc.rtc.KeywordView
I want to use the object reference of Linearlayout. I am posting the code of activity where I implement code .
try {
mKeywordView = (KeywordView) getContnetView();
mKeywordView.setListner(new KeywordView.KeyboardListner() {
#Override
public void onVisbile(boolean isVisibile) {
Toast.makeText(getApplicationContext(), "" + isVisibile, Toast.LENGTH_LONG).show();
}
});
}catch (ClassCastException e)
{
Log.e(TAG, "onCreate: "+e);
}
keywordChatView.java
This is KeywordChatView class
public class KeywordView extends LinearLayout {
private KeyboardListner mListner;
public void setListner(KeyboardListner listner) {
mListner = listner;
}
public KeywordView(Context context) {
super(context);
}
public KeywordView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public KeywordView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public KeywordView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d("Search Layout", "Handling Keyboard Window shown");
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight){
// Keyboard is shown
mListner.onVisbile(true);
} else {
// Keyboard is hidden
mListner.onVisbile(false);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public interface KeyboardListner
{
public void onVisbile(boolean isVisibile);
}
}
this is Linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:id="#+id/main_layout1"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.codiant.prortc.PrivateChatActivity"
tools:showIn="#layout/private_activity_chat">
<FrameLayout
android:id="#+id/close_fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"></FrameLayout>
<FrameLayout
android:id="#+id/swipe_fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:background="#800b0b0b">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:background="#color/colorAccent">
<TextView
android:id="#+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/dp_5"
android:gravity="center"
android:text="Private Chat" />
<ImageButton
android:id="#+id/close_iv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top|right"
android:background="#android:color/transparent"
android:src="#drawable/ic_close" />
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp_5"
android:layout_marginRight="#dimen/dp_5"
android:layout_marginTop="#dimen/dp_40" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="#dimen/dp_5"
android:layout_marginLeft="#dimen/dp_5"
android:layout_marginRight="#dimen/dp_5"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/chat_et"
android:layout_width="0dp"
android:layout_height="#dimen/dp_40"
android:layout_marginRight="#dimen/dp_5"
android:layout_weight=".9"
android:background="#drawable/circluar_border" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="#dimen/dp_40"
android:layout_alignParentRight="true"
android:layout_weight=".1">
<android.support.design.widget.FloatingActionButton
android:id="#+id/send_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/ic_send"
app:backgroundTint="#color/blue_500"
app:borderWidth="0dp"
app:elevation="6dp" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Inheritance works in the opposite direction than you have written. You can set KeywordView to the variable of LinearLayout, but not vice-versa.
You have object of type of LinearLayout ( as specified in XML ) and it can not be cast to its Subclass. Specify proper layout in your XML
So you need to convert your XML linear layout to your KeywordView then you can do what you task.
<?xml version="1.0" encoding="utf-8"?>
<packagename.KeywordView
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"
android:orientation="vertical"
android:id="#+id/main_layout1"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.codiant.prortc.PrivateChatActivity"
tools:showIn="#layout/private_activity_chat">
</packagename.KeywordView >

Android Compound View: cannot make child view match parent

I have created a custom view (compound view) that is inherited from FrameLayout and contains several child views in it:
MediaComponentView.java:
final public class MediaComponentView extends FrameLayout {
private int ratio = 1;
private ImageView imageView;
private CircularProgressView progressView;
private View downloadButton;
private View cancelButton;
private View playButton;
public MediaComponentView(Context context) {
super(context);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initializeViews();
}
private void initializeViews() {
inflate(getContext(), R.layout.view_media_component, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
imageView = (ImageView) this.findViewById(R.id.image_view);
progressView = (CircularProgressView) this.findViewById(R.id.progress_view);
downloadButton = this.findViewById(R.id.download_button);
cancelButton = this.findViewById(R.id.cancel_button);
playButton = this.findViewById(R.id.play_button);
}
public void setRatio(int ratio) {
this.ratio = ratio;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth() / ratio);
}
}
view_media_component.xml:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:riv_border_color="#eee"
app:riv_border_width="1px"
app:riv_corner_radius="3dp"
/>
<com.github.rahatarmanahmed.cpv.CircularProgressView
android:id="#+id/progress_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:visibility="gone"
/>
<ImageView
android:id="#+id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_chat_media_download"
android:visibility="gone"
/>
<ImageView
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#android:drawable/ic_menu_close_clear_cancel"
android:visibility="gone"
/>
<ImageView
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_chat_media_play"
android:visibility="gone"
/>
</merge>
Usage of the compound view:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
>
<some.hidden.package.MediaComponentView
android:id="#+id/media_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#00ff00"
/>
<some.hidden.package.MediaComponentView
android:id="#+id/media_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#ff0000"
/>
</LinearLayout>
The issue is that the "image_view" is not layouted correctly. It does not match parent.
I have checked that the MediaComponentView itself has correct size by setting background to it. It HAS correct size.
But the child view has incorrect size. I guess it happens because of the overriden onMeasure() method in the MediaComponentView.
Any help or explanation would be appreciated.
You need to call measureChildren(int widthMeasureSpec, int heightMeasureSpec) at the end of your onMeasure() override. The child views don't know that you changed the height.

Composite View widgets not visible

I am trying to make a composite view by extending RelativeLayout It consists of three widgets. An ImageView , a ProgressBar and a TextView . But for some reason I am unable to see the ImageView and TextView. Attaching code below. Any suggestions?
public class ProgressView extends RelativeLayout {
#InjectView(R.id.ivItemPic)
ImageView ivItemPic;
#InjectView(R.id.tvMessage)
TextView tvMessage;
#InjectView(R.id.pbLoading)
ProgressBar pbLoading;
int defColor = 0xff669900;
public ProgressView(Context context) {
super(context);
init(context,null, 0);
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs, 0);
}
public ProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs, defStyle);
}
public void init(Context context, AttributeSet attrs, int defStyleAttr) {
inflate(context, R.layout.component_progress_view, this);
ButterKnife.inject(this);
TypedArray aTypedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView, defStyleAttr, 0);
final int textSize = aTypedArray.getDimensionPixelSize(
R.styleable.ProgressView_pvTextSize,
dipsToPix(R.dimen.font_medium));
final int textColor = aTypedArray.getColor(R.styleable.ProgressView_pvTextColor,defColor);
final Drawable imgSrc = aTypedArray.getDrawable(R.styleable.ProgressView_pvImageSrc);
ivItemPic.setImageDrawable(imgSrc);
tvMessage.setTextSize(textSize);
tvMessage.setTextColor(textColor);
aTypedArray.recycle();
}
/**
* Helper method to convert dips to pixels.
*/
private int dipsToPix(float dps) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps,
getResources().getDisplayMetrics());
}
public void setText(String message) {
tvMessage.setText(message);
}
public void imageVisibility(boolean visible) {
ivItemPic.setVisibility(visible ? VISIBLE : GONE);
}
public void progressIndeterminate(boolean indeterminate) {
pbLoading.setIndeterminate(indeterminate);
}
public void setItemImage(Drawable drawable) {
ivItemPic.setImageDrawable(drawable);
}
public void setItemImageRes(int res) {
ivItemPic.setImageResource(res);
}
The XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/ivItemPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/pbLoading"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:src="#drawable/ic_action_info"/>
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="true"
android:visibility="visible" />
<TextView
android:id="#+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/pbLoading"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginRight="#dimen/horizontal_margin"
android:layout_marginLeft="#dimen/horizontal_margin"
android:textSize="#dimen/font_medium"
android:textColor="#color/grey_normal"
android:gravity="center"
android:text="Message"/>
The Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:errorview="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_color"
tools:context="com.mobility.ui.SyncFragment">
<com.mobility.customviews.SuccessView
android:id="#+id/svSuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="center"
app:svText="Success"
app:svTextColor="#android:color/holo_green_dark"/>
<tr.xip.errorview.ErrorView
android:id="#+id/evError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
errorview:ev_errorSubtitle="#string/err_conn"
errorview:ev_errorTitle="#string/oops"
errorview:ev_showRetryButton="true"
errorview:ev_showSubtitle="true"
errorview:ev_showTitle="true"
android:gravity="center"
android:visibility="gone"
android:paddingLeft="#dimen/horizontal_margin"
android:paddingRight="#dimen/horizontal_margin"
errorview:ev_retryButtonTextColor="#color/error_retry"/>
<com.mobility.customviews.ProgressView
android:id="#+id/progressView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:pvText="ZzZZ"/>

Categories

Resources