I've created a custom RelativeLayout that has two ImageView in it, one on top of the other, The first one is supposed to show the user selected image and the second on is shown up after he selected and acts as a delete button (small X icon), the problem that i have is even at the start of the application this custom view is invisible, but i can still click on it. and if i use a background for it i can see it.
Here is my Custom View class and XML
public class ImageViewWithIcon extends RelativeLayout{
public ImageView image, icon;
private final Context context;
public ImageViewWithIcon(final Context context)
{
super(context);
this.context = context;
}
public ImageViewWithIcon(final Context context, final AttributeSet attrs)
{
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.image_view_with_icon, this, true);
RelativeLayout rl = (RelativeLayout) getChildAt(0);
icon = (ImageView) findViewById(R.id.ImageViewWithIcon_icon);
image = (ImageView) findViewById(R.id.ImageViewWithIcon_image);
// image.setLayoutParams(new LayoutParams(400, 400));
this.context = context;
}
public ImageViewWithIcon(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
}
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.new_message_picture_button);
int width;
if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
width = bmp.getWidth();
else
if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().width == android.view.ViewGroup.LayoutParams.FILL_PARENT)
width = MeasureSpec.getSize(widthMeasureSpec);
else
width = getLayoutParams().width;
int height = 100;
if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
height = bmp.getHeight();
else
if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().height == android.view.ViewGroup.LayoutParams.FILL_PARENT)
height = MeasureSpec.getSize(heightMeasureSpec);
else
height = getLayoutParams().height;
bmp.recycle();
setMeasuredDimension(width | MeasureSpec.EXACTLY, height | MeasureSpec.EXACTLY);
}}
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ImageViewWithIcon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:background="#drawable/new_message_picture_button" />
<ImageView
android:id="#+id/ImageViewWithIcon_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/ImageViewWithIcon_image"
android:visibility="visible"
android:src="#drawable/new_message_close_picture"/>
</RelativeLayout>
</merge>
The first line in onMessure has bmp with width and height of 234px
even if I create a method like setImage() to call it from the Activity, the image is set but it is still invisible.
Do you mean?
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageViewWithIcon
android:id="#+id/ImageViewWithIcon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:background="#drawable/new_message_picture_button" />
<ImageViewWithIcon
android:id="#+id/ImageViewWithIcon_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/ImageViewWithIcon_image"
android:visibility="visible"
android:src="#drawable/new_message_close_picture"/>
Related
I am looking to set the ImageView height before loading the image with Picasso. Trying to prevent the parent view from changing its height to the image. I am getting a height and width of the image and setting it inside of of Picasso and then scaling it down.
How do I scale the ImageView based on the "webformatHeight" and "webformatWidth" before loading the image with Picasso to prevent resizing.
Data
{
...
"hits":[
{
"webformatHeight":426,
"webformatWidth":640,
"webformatURL":"https://pixabay.com/get/eb32b5062dfd013ed95c4518b74a4291ea77ead204b0144190f8c478a2ebb3_640.jpg",
...
},
...
]
}
Recycler Implementation
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
mPixabayAdapter = new PixabayAdapter();
recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recycler.setAdapter(mPixabayAdapter);
ViewHolder
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle;
private ViewHolder(View view) {
super(view);
mImage = view.findViewById(R.id.image);
mTitle = view.findViewById(R.id.title);
}
void bindView() {
ColorDrawable[] shotLoadingPlaceholders = new ColorDrawable[]{new ColorDrawable(ContextCompat.getColor(itemView.getContext(), R.color.background_light))};
Hit hit = mHitList.get(getAdapterPosition());
mTitle.setText(hit.getUser());
Picasso
.with(mImage.getContext())
.load(hit.getWebformatURL())
.placeholder(shotLoadingPlaceholders[0])
.resize(hit.getImageWidth(), hit.getImageHeight())
.priority(Picasso.Priority.HIGH)
.onlyScaleDown()
.into(mImage);
}
}
XML
<?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="wrap_content"
android:padding="4dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/image"
android:background="#color/background_light"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#color/white_light"
android:textSize="13sp" />
</RelativeLayout>
Calc the aspect ratio of your image by width/height and wrap your ImageView inside something like this:
public class RelativeSizeLayout extends FrameLayout {
private float aspectRatio = 1.77f;
public RelativeSizeLayout(Context context) {
this(context,null);
}
public RelativeSizeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RelativeSizeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean shouldDelayChildPressedState() {
return false;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width / aspectRatio), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setAspectRatio(float ratio) {
aspectRatio = ratio;
}
}
you can set the layoutParams of the ImageView on the bindView with the width and hight the Hit class like this.
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(hit.getImageWidth(), hit.getImageHeight());
mImage.setLayoutParams(layoutParams);
regards.
i use Universal image loader to load some imageview(large height) to listview.
this is listview(custom for zoom in/out) in activity:
<noname.test1.CustomListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listviewImg" />
this is list_img.xml for listview:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1">
</ImageView>
this is image i want to load (size 400px X 5312px):
link
this is result:
I want image when show is full screen or larger.
How to fix it?
create this class and use this instead of imageView
/**
* Created by farazkhonsari on 1/26/16.
*/
public class ScaledImageView extends ImageView {
public ScaledImageView(Context context) {
super(context);
}
public ScaledImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScaledImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
int width = measuredWidth;
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
setMeasuredDimension(width, height);
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
and use this class in layout like this:
<ir.farazGroup.YeJoke.tools.ScaledImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/c"
android:scaleType="fitCenter"
/>
you should change scaleType of imageViwe and set adjustViewBound true:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1"
android:scaleType="centerCrop"
android:adjustViewBounds="true">
</ImageView>
you can try other option for scaleType and see which one is good for you!
and you can read about scaleType here:
scaleTypes
Try this solution: [Set this inside onCreate or onCreateView after inflating the list]
imageView = (ImageView) findViewById(R.id. imageview1);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float screenWidth = metrics.widthPixels;
imageView.getLayoutParams().width = (int) screenWidth; // Takes the whole screen width
try this i'll hope i will help you
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/locked_image"
android:scaleType="fitXY"
/>
I have Staggered Grid view in which each item contains an image and text. Have a look at this
Below code is item layout xml. DynamicHeightImageView is extended ImageView which is use to change height of image. Here I have tried to set
android:layout_width="fill_parent"
But I think it is not working.
<?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:background="#drawable/background_card"
android:orientation="vertical" >
<com.etsy.android.grid.util.DynamicHeightImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:gravity="center" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:textColor="#ED430F"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:textSize="15sp"
android:typeface="sans" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:textSize="12sp"
android:textColor="#848484"
android:textStyle="italic" />
</LinearLayout>
DynamicHeightImageView class has following code
public class DynamicHeightImageView extends ImageView {
private double mHeightRatio;
public static float radius = 2.0f;
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public DynamicHeightImageView(Context context, AttributeSet attrs) {
super(context, attrs);
if (Build.VERSION.SDK_INT < 18) {
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public DynamicHeightImageView(Context context) {
super(context);
if (Build.VERSION.SDK_INT < 18) {
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
public void setHeightRatio(double ratio) {
if (ratio != mHeightRatio) {
mHeightRatio = ratio;
requestLayout();
}
}
public double getHeightRatio() {
return mHeightRatio;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mHeightRatio > 0.0) {
// set the image views size
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * mHeightRatio);
setMeasuredDimension(width, height);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
#Override
protected void onDraw(Canvas canvas) {
rect.left = 0;
rect.top = 0;
rect.right = this.getWidth();
rect.bottom = this.getHeight();
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
Below code is Grid View Adapter class. This actually binds data with GridView. Here I have also tried to set image view width to fill the parent but it is not working result is the same. Can anyone tel me where I am making mistake?
public class DataAdapter extends ArrayAdapter<Video> {
Activity activity;
int resource;
List<Video> datas;
public DataAdapter(Activity activity, int resource, List<Video> objects) {
super(activity, resource, objects);
this.activity = activity;
this.resource = resource;
this.datas = objects;
}
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final DealHolder holder;
if (row == null) {
LayoutInflater inflater = activity.getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new DealHolder();
holder.image = (DynamicHeightImageView)row.findViewById(R.id.image);
holder.title = (TextView)row.findViewById(R.id.title);
holder.date = (TextView)row.findViewById(R.id.description);
row.setTag(holder);
}
else {
holder = (DealHolder) row.getTag();
}
final Video data = datas.get(position);
Picasso.with(this.getContext())
.load(data.getImgURL())
.into(holder.image);
holder.image.setHeightRatio(getRandomHeight());
holder.image.getLayoutParams().width = LayoutParams.FILL_PARENT;
holder.image.requestLayout();
holder.title.setText(data.getTitle());
holder.date.setText(data.getUploadedDate().toGMTString().subSequence(0, 16));
return row;
}
static class DealHolder {
DynamicHeightImageView image;
TextView title;
TextView date;
}
private float getRandomHeight(){
ArrayList<Float> lista = new ArrayList<Float>();
lista.add((float) 0.5);
lista.add((float) 1.0);
lista.add((float) 0.75);
lista.add((float) 1.5);
Collections.shuffle(lista);
return lista.get(0);
}
}
I have following code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 1"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 2"
android:textSize="24sp" />
</LinearLayout>
that results:
How can I have equal size of View's height with its width (that is resulted by layout_weight)?
You have to override the onMeasure method set the same height as the height. Please see this question: Simple way to do dynamic but square layout
This is the complete solution that I come up with:
SquareLinearLayout.java :
public class SquareLinearLayout extends LinearLayout {
public SquareLinearLayout(Context context) {
super(context);
}
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int widthDesc = MeasureSpec.getMode(widthMeasureSpec);
int heightDesc = MeasureSpec.getMode(heightMeasureSpec);
int size = 0;
if (widthDesc == MeasureSpec.UNSPECIFIED
&& heightDesc == MeasureSpec.UNSPECIFIED) {
size = getContext().getResources().getDimensionPixelSize(R.dimen.default_size); // Use your own default size, for example 125dp
} else if ((widthDesc == MeasureSpec.UNSPECIFIED || heightDesc == MeasureSpec.UNSPECIFIED)
&& !(widthDesc == MeasureSpec.UNSPECIFIED && heightDesc == MeasureSpec.UNSPECIFIED)) {
//Only one of the dimensions has been specified so we choose the dimension that has a value (in the case of unspecified, the value assigned is 0)
size = width > height ? width : height;
} else {
//In all other cases both dimensions have been specified so we choose the smaller of the two
size = width > height ? height : width;
}
setMeasuredDimension(size, size);
}
}
activity_my.xml :
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/listview"
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:listitem="#layout/item"
tools:context=".MyActivity">
</ListView>
item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.appsrise.squarelinearlayout.SquareLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 1"
android:textSize="24sp" />
</com.appsrise.squarelinearlayout.SquareLinearLayout>
<com.appsrise.squarelinearlayout.SquareLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_light">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 2"
android:textSize="24sp" />
</com.appsrise.squarelinearlayout.SquareLinearLayout>
</LinearLayout>
MyActivity.java :
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(new BaseAdapter() {
private LayoutInflater mInflater = LayoutInflater.from(MyActivity.this);
#Override
public int getCount() {
return 100;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.item, parent, false);
return convertView;
}
});
}
}
Simpler solution is to pass the width measurement specification into the height specification when calling onMeasure (or the other way around if you want the height to determine the square size).
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLinearLayout extends LinearLayout{
public SquareLinearLayout(Context context) {
super(context);
}
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
// or super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
}
Xamarin version
[Register("SquareFrameLayout")]
public class SquareFrameLayout : LinearLayout
{
public SquareFrameLayout(Context context) : base(context)
{
}
public SquareFrameLayout(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public SquareFrameLayout(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
var width = MeasureSpec.GetSize(widthMeasureSpec);
var height = MeasureSpec.GetSize(heightMeasureSpec);
var widthDesc = MeasureSpec.GetMode(widthMeasureSpec);
var heightDesc = MeasureSpec.GetMode(heightMeasureSpec);
if (widthDesc == MeasureSpecMode.Unspecified && heightDesc == MeasureSpecMode.Unspecified)
{
//size = Context.Resources.GetDimensionPixelSize(Resource.Dimension.default_size); // Use your own default size, for example 125dp
height = width = 0;
}
else if(heightDesc != MeasureSpecMode.Unspecified && widthDesc == MeasureSpecMode.Unspecified)
{
width = height;
}
else if (widthDesc != MeasureSpecMode.Unspecified && heightDesc == MeasureSpecMode.Unspecified)
{
height = width;
}
else
{
//In all other cases both dimensions have been specified so we choose the smaller of the two
var size = width > height ? height : width;
width = height = size;
}
SetMeasuredDimension(width, height);
}
and
[Register("SquareLinearLayout")]
public class SquareLinearLayout : LinearLayout
{
public SquareLinearLayout(Context context) : base(context)
{
}
public SquareLinearLayout(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public SquareLinearLayout(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
var width = MeasureSpec.GetSize(widthMeasureSpec);
var height = MeasureSpec.GetSize(heightMeasureSpec);
var widthDesc = MeasureSpec.GetMode(widthMeasureSpec);
var heightDesc = MeasureSpec.GetMode(heightMeasureSpec);
if (widthDesc == MeasureSpecMode.Unspecified && heightDesc == MeasureSpecMode.Unspecified)
{
//size = Context.Resources.GetDimensionPixelSize(Resource.Dimension.default_size); // Use your own default size, for example 125dp
height = width = 0;
}
else if(heightDesc != MeasureSpecMode.Unspecified && widthDesc == MeasureSpecMode.Unspecified)
{
width = height;
}
else if (widthDesc != MeasureSpecMode.Unspecified && heightDesc == MeasureSpecMode.Unspecified)
{
height = width;
}
else
{
//In all other cases both dimensions have been specified so we choose the smaller of the two
var size = width > height ? height : width;
width = height = size;
}
SetMeasuredDimension(width, height);
}
}
You can do this also, If you need only one TextView needs to be square :
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = textview.getMeasuredWidth();
int height = textview.getMeasuredHeight();
ViewGroup.LayoutParams params = textview.getLayoutParams();
if (width > height)
params.height = width;
else
params.width = height;
textview.setLayoutParams(params);
}
});
You can divide a LinearLayout into two equal LinearLayout like this..
<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 1"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test 1"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
This will divide the LinearLayout into two equal LinearLayouts and each of these LinearLayouts will contain the TextViews
I'm newbie to android and have a problem with it.
I'm trying to make a customized view in android for menu.
I want it to have 4 elements which have images.
But these are invisible though I coded like below :( What can I do for it ?
I've googled it for a while and there's no reasonable solution in my case ...
Menuindicator.java
public class MenuIndicator extends FrameLayout {
private static final int MIN_WIDTH = 50;
private static final int MIN_HEIGHT = 50;
private final float ACTUAL_PIXELS = getContext().getResources().getDisplayMetrics().density;
private ImageView menuComponentAbove = null;
private ImageView menuComponentBelow = null;
private ImageView menuComponentFront = null;
private ImageView menuComponentBack = null;
public MenuIndicator(Context context) {
super(context);
init(null, 0);
}
public MenuIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public MenuIndicator(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MenuIndicator, defStyle, 0);
LayoutInflater.from(getContext()).inflate(R.layout.layout_menu_indicator, this);
menuComponentAbove = (ImageView) findViewById(R.id.menu_component_above);
menuComponentBelow = (ImageView) findViewById(R.id.menu_component_below);
menuComponentFront = (ImageView) findViewById(R.id.menu_component_front);
menuComponentBack = (ImageView) findViewById(R.id.menu_component_back);
typedArray.recycle();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = 0;
switch(widthMode) {
case MeasureSpec.UNSPECIFIED: widthSize = widthMeasureSpec; break;
case MeasureSpec.AT_MOST: widthSize = (int)(MIN_WIDTH * ACTUAL_PIXELS); break;
case MeasureSpec.EXACTLY: widthSize = MeasureSpec.getSize(widthMeasureSpec); break;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = 0;
switch(heightMode) {
case MeasureSpec.UNSPECIFIED: heightSize = heightMeasureSpec; break;
case MeasureSpec.AT_MOST: heightSize = (int)(MIN_HEIGHT * ACTUAL_PIXELS); break;
case MeasureSpec.EXACTLY: heightSize = MeasureSpec.getSize(heightMeasureSpec); break;
}
setMeasuredDimension(widthSize, heightSize);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
menuComponentAbove.draw(canvas);
menuComponentBelow.draw(canvas);
menuComponentFront.draw(canvas);
menuComponentBack.draw(canvas);
invalidate();
}
}
layout_menu_indicator.xml
<!-- layout_menu_indicator.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#171717">
<RelativeLayout
android:id="#+id/menu_indicator"
android:layout_width="#dimen/dimen_menu_indicator"
android:layout_height="#dimen/dimen_menu_indicator"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<ImageView
android:id="#+id/menu_component_front"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:antialias="true"
android:src="#drawable/menu_component" />
<ImageView
android:id="#+id/menu_component_back"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:antialias="true"
android:src="#drawable/menu_component" />
<ImageView
android:id="#+id/menu_component_above"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_above="#id/menu_component_front"
android:layout_centerVertical="true"
android:antialias="true"
android:src="#drawable/menu_component" />
<ImageView
android:id="#+id/menu_component_below"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#id/menu_component_front"
android:layout_centerVertical="true"
android:antialias="true"
android:src="#drawable/menu_component" />
</RelativeLayout>
<LinearLayout
android:id="#+id/command_container_horizontal"
android:layout_width="wrap_content"
android:layout_height="#dimen/dimen_menu_indicator"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/menu_indicator"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/command_container_vertical"
android:layout_width="#dimen/dimen_menu_indicator"
android:layout_height="wrap_content"
android:layout_below="#+id/menu_indicator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
Add this:
public MenuIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(getContext()).inflate(R.layout.layout_menu_indicator, this);
menuComponentAbove = (ImageView) findViewById(R.id.menu_component_above);
menuComponentBelow = (ImageView) findViewById(R.id.menu_component_below);
menuComponentFront = (ImageView) findViewById(R.id.menu_component_front);
menuComponentBack = (ImageView) findViewById(R.id.menu_component_back);
}
And remove that code from your init() method