I have a bunch of gifs of different sizes and I want to align then in a horizontal ListView so that they're all the same height, obviously not necessarily the same width.
We are using https://github.com/koral--/android-gif-drawable
Example:
Gif display 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="match_parent"
android:background="#android:color/transparent"
android:paddingLeft="10dp"
android:id="#+id/holder"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<pl.droidsonroids.gif.GifTextureView
android:layout_width="100dp"
android:id="#+id/gif_rec_view"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
Container
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/title1"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:id="#+id/xD">
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/gif_search_1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#e3e3e3"
/>
</RelativeLayout>
Load gif into view:
final GifTextureView textureView = (GifTextureView) convertView.findViewById(R.id.gif_rec_view);
LoadGif gLoad = new LoadGif(_objects.get(position).url, textureView);
gLoad.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
How would I best solve this?
Tried bunch of stuff.
//textureView.setLayoutParams(new RelativeLayout.LayoutParams(_objects.get(position).width, _objects.get(position).height));
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
//textureView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Edit 1:
So looking at this thread: How to scale an Image in ImageView to keep the aspect ratio
using
android:adjustViewBounds="true"
android:scaleType="centerCrop"
worked wonders, however the images cropped like so:
and
android:adjustViewBounds="true"
android:layout_centerInParent="true"
did not work at all.
Okay so this is how I managed to solve it:
Gif display xml
<pl.droidsonroids.gif.GifTextureView
android:layout_width="150dp"
android:layout_height="match_parent"
android:id="#+id/gif_rec_view"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
Adapter
/*Absolutely fucking ridiculous*/
textureView.setLayoutParams(new RelativeLayout.LayoutParams(Utils.pxToDp(_objects.get(position).width), Utils.pxToDp(_objects.get(position).height)));
Utils ...
public static int pxToDp(int px)
{
//return (int) (px / Resources.getSystem().getDisplayMetrics().density);
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, Resources.getSystem().getDisplayMetrics());
}
Hope this may help some lone wanderer in the future.
Related
After creating a GridView, I noticed that some GridView items have a bigger height than others. What needs to be done to ensure that all GridView items are the same height(regardless of screen orientation & different text lengths) + to ensure that GridView content doesn't get clipped?
sw600dp/activity_main.xml
<?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:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity">
<GridView
android:id="#+id/abslistview_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:numColumns="auto_fit"
/>
</RelativeLayout>
sw600dp/item_abslistview.xml
<?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/tools"
android:id="#+id/cardview_mainitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:background="#drawable/ripple_background"
android:orientation="horizontal">
<ImageView
android:id="#+id/griditem_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/griditem_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#android:color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</android.support.v7.widget.CardView>
MainActivity.java
public class MainActivity extends Activity {
private ArrayList<MainItem> mainArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AbsListView absListView = findViewById(R.id.abslistview_main);
settingData();
ArrayAdapter<MainItem> adapter = new AbsListViewAdapter(this, R.layout.item_abslistview, mainArrayList);
absListView.setAdapter(adapter);
}
private void settingData() {
mainArrayList = new ArrayList<>();
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "Fruit and vegetables"));
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "Potatoes, bread, rice, pasta and other starchy carbohydrates"));
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "Oils and spreads"));
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "Dairy and alternatives"));
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "Beans, pulses, fish, eggs, mint and other proteins"));
mainArrayList.add(new MainItem(R.drawable.ic_info_outline_white, "More information"));
}
}
Use fixed height instead of wrap_content in the xml of your custom list item.
If you want to show all items of the grid view instead of scrolling behavior, use nested linear layouts with weights, or try some other layouts.
try this: Change the attribute of the Image view in the file to a specific height of your choice. Thanks
android:layout_height="50dp"
That might help in solving the problem. The specified attribute would make it to have a definite height and remains equal for all items .
This is my xml created file for a layout and it displays as I want it to.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#android:drawable/dialog_holo_light_frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/imageSectionCustomRowL"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:id="#+id/titleCustomRowL"
android:paddingRight="10dp"
android:textSize="30sp"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#959595"
android:paddingBottom="10dp"
android:paddingTop="5dp"
android:id="#+id/subTitleCustomRowL"
android:paddingLeft="20dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And this is more or less the same thing but created dynamically via code
LinearLayout parentLL = (LinearLayout)findViewById(R.id.newsMainPopulationLL);
LinearLayout parentRowLL = new LinearLayout(news.this);
parentRowLL.setOrientation(LinearLayout.VERTICAL);
Drawable bg = news.this.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame);
//Drawable bbg = getDrawable(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertDpToPx(120)));
LinearLayout parentRowChildHLL = new LinearLayout(news.this);
parentRowChildHLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentRowChildHLL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout containerForPic = new LinearLayout(news.this);
containerForPic.setLayoutParams(new LinearLayout.LayoutParams(convertDpToPx(75), convertDpToPx(75)));
containerForPic.setOrientation(LinearLayout.VERTICAL);
ImageView picInList = new ImageView(news.this);
picInList.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout contentVerticalLL = new LinearLayout(news.this);
contentVerticalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
contentVerticalLL.setOrientation(LinearLayout.VERTICAL);
TextView headingTV = new TextView(news.this);
headingTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
headingTV.setPadding(convertDpToPx(10), convertDpToPx(10), convertDpToPx(10), 0);
headingTV.setTextColor(Color.parseColor("#000000"));
headingTV.setTextSize(30);
TextView contentTV = new TextView(news.this);
contentTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentTV.setPadding(convertDpToPx(10), convertDpToPx(5), convertDpToPx(10), convertDpToPx(10));
contentTV.setTextColor(Color.parseColor("#959595"));
contentVerticalLL.addView(headingTV);
contentVerticalLL.addView(contentTV);
parentRowChildHLL.addView(contentVerticalLL);
containerForPic.addView(picInList);
parentRowChildHLL.addView(containerForPic);
parentRowLL.addView(parentRowChildHLL);
parentLL.addView(parentRowLL);
Here convertDptoPx is a int() that works properly. However for some reason the Linear layout parentRowChildLL even though set to LinearLayout.HORIZONTAL for orientation for some reason displays vertically (According to the layout boundaries I see using the developer options setting)
Is there something basic I'm missing out yet? When I see the boundaries I have 3 rows. And also I tried encasing my image view in the layout because otherwise it is not even visible.
Thank You.
I want to change the width percentage of items inside a PercentRelativeLayoutdynamically in Java based on some events, I found this, but I cannot find where to put the percentages:
mLinerLayout.setLayoutParams(new PercentRelativeLayout.LayoutParams());
My xml looks something like this:
<android.support.percent.PercentRelativeLayout
android:id="#+id/ad_prl_tags"
android:layout_width="match_parent"
android:layout_height="48dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/ad_rv_tags"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
app:layout_widthPercent="70%" />
<!--Add Tags Button-->
<LinearLayout
android:id="#+id/ad_ll_add_tags_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ad_rv_tags">
<ImageView
android:id="#+id/itd_animatable_plus_iv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="#+id/ad_et_add_tags_and_info"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
I want to set the width percentage of the LinearLayout to 70% and the width of the RecyclerView to 30% in Java.
You can use it pragmatically with this method :
public static void setWidthPercent(View view, float width) {
PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.widthPercent = width;
}
So I have 4 normal imagebuttons that I have set up and then a 5th imagebutton that is a drag and drop. I want the 4 non drag and drop imagebuttons to stay in a particular spot no matter what happens with the imagebutton that is drag and drop. When I move my drag and drop imagebutton, the other imagebuttons get moved and are being squished also.
Any idea how I can move this drag and drop imagebutton without affecting the other imagebuttons in any way?
Here is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/mars" >
<TextView
android:id="#+id/scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainhut" />
<ImageButton
android:id="#+id/polarCapButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/polarCapButton2"
android:layout_marginTop="-70dp"
android:layout_marginLeft="575dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/spaceRockButton1"
android:layout_marginTop="100dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spacerock" />
<ImageButton
android:id="#+id/spaceRockButton2"
android:layout_marginTop="-140dp"
android:layout_marginLeft="520dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spacerock" />
<ImageButton
android:id="#+id/meteor"
android:visibility="invisible"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/meteor"
android:layout_marginTop="-100dp"
android:layout_marginLeft="400dp" />
</LinearLayout>
Here is the code for my drag and drop imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)(event.getRawX() - event.getRawY() / 2), (int) (event.getRawY() - v.getHeight() * 1.5), (int) (event.getRawX() - v.getWidth() / 2), (int) (event.getRawY() - v.getHeight() * 1.5));
v.setLayoutParams(params);
}//end if
}//end else
return false;
}//end onTouch function
});//end setOnClickListener
Thanks in advance for the help.
I suggest you use a FrameLayout. This will make your imageviews stackable.
<FrameLayout>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
</FrameLayout>
You will need to set them all to have a static position on the screen based on its parents origin. The 5th ImageButton, you can use it to set the onTouch listener like you did.
Edit: OR you can do something like this (If you wanna keep the linear layout):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/mars"
android:orientation="vertical" >
<TextView
android:id="#+id/scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/polarCapButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap"
android:orientation="vertical" />
<ImageButton
android:id="#+id/polarCapButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="575dp"
android:layout_marginTop="-70dp"
android:background="#drawable/polarcap"
android:orientation="vertical" />
<ImageButton
android:id="#+id/spaceRockButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/spacerock"
android:orientation="vertical" />
<ImageButton
android:id="#+id/spaceRockButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="520dp"
android:layout_marginTop="-140dp"
android:background="#drawable/spacerock"
android:orientation="vertical" />
<ImageButton
android:id="#+id/meteor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="400dp"
android:layout_marginTop="-100dp"
android:background="#drawable/meteor"
android:orientation="vertical"
android:visibility="invisible" />
</LinearLayout>
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="put offset here"
android:background="#drawable/mainhut" />
</FrameLayout>
Just change the marginTop on mainHut to the offset you want to set it at!
Without looking at your XML file, it is probably due to the use of a relative layout - the 4 static buttons may be positioned relative to the button that is being moved, and will move in real-time when dragging and dropping. Try using absolute positioning (e.g., android:layout_marginTop="30dp") instead of relative, or using another type of layout. If you could post both your Activity where this happens and your XML file for that layout, that will better help provide some information for more appropriate answers.
I'm not sure if this is what you want.
But you could use a floating imageButton, using the Window Manager. But this way the button would be a standalone overlay and no longer be part of the 4-buttons View. See this post: What APIs in Android is Facebook using to create Chat Heads?
I would like to know the new size of my image when it is set in an imageview.
I tried this :
System.out.println("Imagesize 1 w:" + imageView.getDrawable().getIntrinsicWidth()+" h "+imageView.getDrawable().getIntrinsicHeight());
System.out.println("Imagesize 2 w:" +loadedImage.getWidth()+ " h " +loadedImage.getHeight());
xml1 of imageView:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
I have this result
Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577
xml2 of imageView:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Same result:
Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577
Obviously my image is not displayed in my screen with the same size in case of xml1 and xml2. I guess when I getWidth or getHeight it is the real size of the image, but I would like the size of the image displayed on my screen.
Any ideas?
Update 1:
I forgot to say that loadedImage is the bitmap of my image
Update 2:
xml.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dip" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:layout_alignParentBottom="true" />
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
To get the size of screen objects after they have been created you need to use a view observer like so:
ViewTreeObserver viewTreeObserver = YOURLAYOUT.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
YOURLAYOUT.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//do some things
//YOURLAYOUT.getWidth() should give a correct answer
}
});
}