I want to make a view slowly scrolls into mobile page, first like this:
then like this:
the view with red background slowly scrolls into mobile page.
Here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_clean_complete"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginTop="80dp"
android:src="#mipmap/img_clean_completed_ok"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:id="#+id/layout_btm"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_height="288dp">
<TextView
android:text="Hello World"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Here is the code:
public class MainActivity extends AppCompatActivity {
private RelativeLayout layoutBtm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutBtm = findViewById(R.id.layout_btm);
int topMargin = getHeightPixels();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
marginLayoutParams.topMargin = topMargin;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(layoutBtm, "translationY", -dip2px(this, 288));
objectAnimator.setDuration(1000);
objectAnimator.start();
}
private int getHeightPixels(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
return dm.heightPixels;
}
private int getStatusBarHeight(Activity activity){
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int dpHeight = Integer.parseInt(field.get(object).toString());
return activity.getResources().getDimensionPixelSize(dpHeight);
} catch (Exception e) {
return 0;
}
}
private int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
It seems to be fine. But after the red view scrolls in, there is some space left at the bottom.The page doesn't change its height automatically
Finally I know how to solve this problem. I edit the code of change the translationY of the view to change the top margin of the view.Here is the code after change:
public class MainActivity extends AppCompatActivity {
private RelativeLayout layoutBtm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutBtm = findViewById(R.id.layout_btm);
int topMargin = getHeightPixels();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
marginLayoutParams.topMargin = topMargin;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
ValueAnimator animator = ValueAnimator.ofInt(getHeightPixels(), getHeightPixels() - getStatusBarHeight(this) -dip2px(this, 288));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
int currentValue = (int) animator.getAnimatedValue();
ViewGroup.MarginLayoutParams marginLayoutParams =
new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(MainActivity.this, 288));
marginLayoutParams.topMargin = currentValue;
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutBtm.setLayoutParams(layoutParams2);
layoutBtm.requestLayout();
}
});
animator.setDuration(1000);
animator.start();
}
private int getHeightPixels(){
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
return dm.heightPixels;
}
private int getStatusBarHeight(Activity activity){
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int dpHeight = Integer.parseInt(field.get(object).toString());
return activity.getResources().getDimensionPixelSize(dpHeight);
} catch (Exception e) {
return 0;
}
}
private int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
Related
I want to dynamically create rectangle shape by using float values from server. The shapes should be precise like if any value is 25.2 and another is 25.3 then the 25.3 one should look bigger like we see in charts. So is there any way to achieve this? Here's the image:
I was trying to change the view size by using this:
itemView.tv_value.layoutParams = LinearLayout.LayoutParams(width,height)
But this seems to be accepting integer values only and if float converted to int using double then it will round off to the nearest number and it won't work.
How to achieve this either by using canvas or views?
First of all width or height of any view can not be float value.
You can set int value based on pixel of screen and float value ratio.
Layout for adapter to generate graph...
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal|left"
android:id="#+id/lo_dynamic_view_container">
</LinearLayout>
<TextView
android:id="#+id/tv_chart_value"
android:layout_width="0dp"
android:layout_weight="0.2"
android:minHeight="50dp"
android:textColor="#000"
android:gravity="center_horizontal|center_vertical"
android:layout_height="match_parent"/>
</LinearLayout>
Generate Ration
private float getRatio(int width, float value, float highestValue){
float result = 0;
result = ( (float)width/highestValue) * value;
Log.e("Result", "width: "+ width +" "+(int) Math.floor(result)+"");
return result;
}
Combine Activity and adapter calss
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Data> listData = new ArrayList<>();
BarChartAdapter barChartAdapter;
int[] colors = {Color.GREEN, Color.CYAN, Color.MAGENTA, Color.RED };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_bar_chart);
listData.add(new Data(8.0f,Color.GREEN));
listData.add(new Data(4.0f,Color.CYAN));
listData.add(new Data(2.0f,Color.MAGENTA));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
barChartAdapter = new BarChartAdapter(listData);
recyclerView.setAdapter(barChartAdapter);
}
public class BarChartAdapter extends RecyclerView.Adapter<BarChartAdapter.MyViewHolder> {
ArrayList<Data> listData = new ArrayList<>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
LinearLayout layout;
public MyViewHolder(View v) {
super(v);
textView = v.findViewById(R.id.tv_chart_value);
layout = (LinearLayout) v.findViewById(R.id.lo_dynamic_view_container);
}
}
public BarChartAdapter(ArrayList<Data> listData) {
this.listData = listData;
}
#Override
public BarChartAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View v = (View) LayoutInflater
.from(parent.getContext())
.inflate(R.layout.barchart_layout, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.setIsRecyclable(false);
holder.textView.setText(listData.get(position).value+"");
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
width = width - (100/width)*80;
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(MainActivity.this);
tv.setLayoutParams(lparams);
tv.setWidth((int) Math.floor( getRatio(width, listData.get(position).value,getHighestValue(listData))));
float redious [] = { 0, 0, 8.3f, 8.5f, 8.2f, 8.9f, 0, 0 };
ShapeDrawable shape = new ShapeDrawable (new RoundRectShape(redious,null,null));
shape.getPaint().setColor(listData.get(position).color);
//shape.getPaint().setColor(colors[new Random().nextInt((colors.length-1) - 0 + 1) + 0]);
//shape.getPaint().setColor(Color.GREEN);
tv.setBackground(shape);
holder.layout.addView(tv);
}
#Override
public int getItemCount() {
return listData.size();
}
}
private float getRatio(int width, float value, float highestValue){
float result = 0;
result = ( (float)width/highestValue) * value;
Log.e("Result", "width: "+ width +" "+(int) Math.floor(result)+"");
return result;
}
private float getHighestValue(ArrayList<Data> listData){
float result = 0.0f;
if(listData!=null){
if(listData.size()>0){
for (int i = 0; i < listData.size(); i++) {
result = listData.get(i).value>result?listData.get(i).value:result;
}
}
}
return result;
}
class Data{
float value;
int color;
public Data(float value, int color) {
this.value = value;
this.color = color;
}
}
}
Screen Shoot
Full Project link on GitHub
I have a WebView, and load iframe in loadData. When a softkeyboard appears - the Webview change height, but when the softkeyboard disappears - the height of the WebView doesn't change. How to change height of the WebView back to fullscreen?
My Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.wbv);
webView.getSettings().setJavaScriptEnabled(true);
String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width="100%" height="100%"></iframe></body></html>";
webView.loadData(html,"text/html","UTF-8");
}
}
xml:
<?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="app.hr_bot.MainActivity">
<WebView
android:id="#+id/wbv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Screenshots:
EDIT:
So, Now i have something like this:
final EKRAN myEkran = new EKRAN(MainActivity.this);
MAIN = findViewById(R.id.rootLayout);
MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
MAIN.getWindowVisibleDisplayFrame(r);
int screenHeight = MAIN.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.10) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
Toast.makeText( MainActivity.this , "COOL1 " , Toast.LENGTH_SHORT).show();
ViewGroup.LayoutParams params1 = webView.getLayoutParams();
params1.width = myEkran.W(100); // IN %
params1.height = myEkran.H(45); // IN %
webView.setLayoutParams(params1);
webView.requestLayout();
}
else {
Toast.makeText( MainActivity.this , "COOL2 " , Toast.LENGTH_SHORT).show();
// keyboard is closed
ViewGroup.LayoutParams params1 = webView.getLayoutParams();
params1.width = myEkran.W(100); // IN %
params1.height = myEkran.H(92); // IN %
webView.setLayoutParams(params1);
webView.requestLayout();
}
}
});
webView = (WebView) findViewById(R.id.wbv);
webView.getSettings().setJavaScriptEnabled(true);
String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width=\"100%\" height=\"100%\"></iframe></body></html>";
webView.loadData(html,"text/html","UTF-8");
Detect keyboard isHihhen or visible :
// MAIN is root view see in design window (also enter id for root view ):
View MAIN;
...
//########################
// Put this intro OnCreate
//########################
MAIN = findViewById(R.id.main);
MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
MAIN.getWindowVisibleDisplayFrame(r);
int screenHeight = MAIN.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
Toast.makeText( context_pass , "COOL1 " , 1);
}
else {
Toast.makeText( context_pass , "COOL2 " , 1);
// keyboard is closed
}
}
});
If your webview is deformed use this code after keyboard hides:
ViewGroup.LayoutParams params1 = YOUR_CONTROL.getLayoutParams();
params1.width = MY_EKRAN.W(100); // IN %
params1.height = MY_EKRAN.H(100); // IN %
YOUR_CONTROL.setLayoutParams(params1);
OR
runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
YOUR_CONTROL.getLayoutParams().height = ( MY_EKRAN.HEIGHT()/100*10);
}
});
DEF :
//global
EKRAN MY_EKRAN;
// in onCreate
MY_EKRAN = new EKRAN( context_pass );
Class for diametric dimensions and positions (add like new file - new java class) :
import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
* Created by nikola on 10/17/16.
*/
//##############################################
// SCREEN - EKRAN CLASS
//##############################################
public class EKRAN {
DisplayMetrics dm = new DisplayMetrics();
Point size_ = new Point();
static int width;
static int height;
EKRAN(Context CONTEXT_) {
dm = CONTEXT_.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
height = dm.heightPixels;
width = dm.widthPixels;
}
public static int WIDTH() {
return width;
}
public static int HEIGHT(){
return height;
}
public int W( int PER_ ){
return width/100*PER_;
}
public int H( int PER_ ){
return height/100*PER_;
}
//////////////////
//extras
/////////////////
public int GET_PIX_FROM_DP ( float DP_VALUE )
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_VALUE , dm );
}
public int GET_PIX_FROM_DP2 ( float DP_VALUE )
{
float res = DP_VALUE * ( dm.ydpi / 160f);
return (int) res;
}
}
I Have already done this in activity and I will provide part of that code as well. Basically I have a linear layout inside HorizontalScroll View and I want to add imageViews to it dynamically. The activity code for this is below and it works perfectly fine all I want is to have exact same functionality for 1 of my fragments as well. XML for the code is given below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
tools:context="com.example.scrolls_tabs.OneFragment"
android:background="#drawable/wooden_back">
<HorizontalScrollView
android:id="#+id/HorizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#00ffffff"
android:scrollbars="none">
<LinearLayout
android:id="#+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/imgLayout1"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
and code for this that works perfectly in activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout =(RelativeLayout)findViewById(R.id.activity_main);
layout.setBackgroundResource(R.drawable.wooden_back);
// Bitmap bdrawable = new Bitmap(this.getResources().getIdentifier("drawable/wood_back", "id", getPackageName()));
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
int screenWidth = displaymetrics.widthPixels;
int imgHeight = (int) (screenHeight* 0.30);
int imgWidth = (int) (screenWidth* 0.25);
for (int j=1; j<10; j++)
{
final int b1=j;
create_img1("drawable/a"+j, b1, imgHeight, imgWidth);
ImageView drag = (ImageView)findViewById(b1);
dragAndDropImage(drag, "drawable/c"+j);
}
ImageView imgh=(ImageView)findViewById(R.id.bottomlinear);
imgh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle localBundle = new Bundle();
localBundle.putInt("Item #1", item1);
localBundle.putInt("Item #2", item2);
localBundle.putInt("Item #3", item3);
localBundle.putInt("Item #4", item4);
localBundle.putInt("Item #5", item5);
localBundle.putInt("Item #6", item6);
localBundle.putInt("Item #7", item7);
localBundle.putInt("Item #8", item8);
localBundle.putInt("Item #9", item9);
Intent i = new Intent(getApplicationContext(),test_activity.class);
i.putExtras(localBundle);
startActivityForResult(i, 1);
}
});
}
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
linearLayout.addView(imageView);
imageView.setId(ID);
imageView.getLayoutParams().height = H;
imageView.getLayoutParams().width = W;
}
This is basically me looking for a solution where I get to do the same thing in fragments that I have already done in activity. I'm very new to android so anything helpful is appreciated.
Current Fragment code which is not working.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.fragment_one, container, false);
int imgHeight = 50;
int imgWidth = 50;
for (int j=1; j<10; j++)
{
final int b1=j;
create_img1("drawable/a"+j, b1, imgHeight, imgWidth);
}
return ll;
}
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) ll.findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(getActivity());
imageView.setLayoutParams(parms);
//int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(R.drawable.a1);
linearLayout.addView(imageView);
imageView.setId(ID);
//imageView.getLayoutParams().height = H;
//imageView.getLayoutParams().width = W;
}
}
you should add img view to linear layout like below
void create_img1(String ss, int ID, int H, int W)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 0, 20, 0);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
imageView.setId(ID);
imageView.getLayoutParams().height = H;
imageView.getLayoutParams().width = W;
**linearLayout.addView(imageView);**
}
Hi Stackoverflow.
I've been trying to handle this issue for two days now.
We have a UnswipableViewPager, which is a custom implementation of ViewPager to intercept touch events and stop 'em (and nothing else), and right by it's right side we have a FrameLayout that we want to replace (through a FragmentTransaction) with our fragment. Nothing out of ordinary here if it wasn't for the fact our ViewPager has to shrink to fit the new Fragment. We have a custom implementation of RelativeLayout called ResizableLayout which we use to do that. It works ok with images, mind you, it's when we're loading a slide with a video, through a VideoView, that the issues pop.
This is how it looks from a design perspective. First we have it unshrunk, then we have it shrunk correctly, and last we have what happens whenever I try to load a slide with a VideoView inside it.
The snippet from the XML layout file:
<RelativeLayout
android:id="#+id/content_relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipChildren="false"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<br.com.i9algo.taxiadv.v2.views.widgets.ResizableLayout
android:id="#+id/slideshow_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:clipChildren="false"
android:orientation="horizontal"
android:scaleType="fitXY"
layout="#layout/slideshow_item_fragment">
<mypackage.widgets.UnswipableViewPager
android:id="#+id/playlist_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/slideshow_item_fragment"
android:clipChildren="false"
android:clickable="true"
android:scaleType="fitXY"
/>
</mypackage.widgets.ResizableLayout>
<FrameLayout
android:id="#+id/sidebar_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:clipChildren="false"
android:layout_toEndOf="#+id/slideshow_frame"
android:scaleType="fitXY" />
</RelativeLayout>
Our ResizableLayout class:
public class ResizableLayout extends RelativeLayout {
private int originalHeight = 0;
private int originalWidth = 0;
private int minWidth = 0;
private static final float SLIDE_TOP = 0f;
private static final float SLIDE_BOTTOM = 1f;
private boolean mMinimized = false;
public ResizableLayout(Context context) {
this(context, null, 0);
}
public ResizableLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ResizableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
if (!isInEditMode()) {
minWidth = getContext().getResources().getDimensionPixelSize(R.dimen.playlist_min_width);
}
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
public boolean smoothSlideTo(#NonNull float slideOffset) {
final int topBound = getPaddingTop();
int x = (int) (slideOffset * (getWidth() - getOriginalWidth()));
int y = (int) (topBound + slideOffset * getVerticalDragRange());
ViewCompat.postInvalidateOnAnimation(this);
return true;
}
public void minimize() {
if (isMinimized())
return;
mMinimized = true;
try {
ResizeAnimation resizeAnimation = new ResizeAnimation(this, minWidth, getOriginalHeight(), false);
resizeAnimation.setDuration(500);
resizeAnimation.setTopMargin(20);
setAnimation(resizeAnimation);
smoothSlideTo(SLIDE_BOTTOM);
requestLayout();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void maximize() {
if (isMaximized())
return;
mMinimized = false;
try {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
params.width = getOriginalWidth();
params.topMargin = 0;
setLayoutParams(params);
measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
smoothSlideTo(SLIDE_TOP);
requestLayout();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public int getOriginalHeight() {
if (originalHeight == 0) {
originalHeight = getMeasuredHeight();
}
return originalHeight;
}
public int getOriginalWidth() {
if (originalWidth == 0) {
originalWidth = getMeasuredWidth();
}
return originalWidth;
}
public boolean isMinimized() {
return mMinimized;
}
public boolean isMaximized() {
return !mMinimized;
}
private float getVerticalDragRange() {
return getHeight() - getOriginalHeight();
}
This is ResizeAnimation in case anybody is wondering
public class ResizeAnimation extends Animation {
private final int mOriginalWidth;
private final int mOriginalHeight;
private final int mTargetWidth;
private final int mTargetHeight;
private int topMargin, leftMargin, bottomMargin, rightMargin;
private boolean mDown;
private View mView;
public ResizeAnimation(View view, int targetWidth, int targetHeight, boolean down) {
this.mView = view;
this.mTargetWidth = targetWidth;
this.mTargetHeight = targetHeight;
mOriginalWidth = view.getWidth();
mOriginalHeight = view.getHeight();
this.mDown = down;
}
public void setTopMargin(int value) {
this.topMargin = value;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth = (int) (mOriginalWidth + (mTargetWidth - mOriginalWidth) * interpolatedTime);
int newHeight = (int) (mOriginalHeight + (mTargetHeight - mOriginalHeight) * interpolatedTime);
if (mDown) {
newWidth = mTargetWidth;
newHeight = mTargetHeight;
}
mView.getLayoutParams().width = newWidth;
mView.getLayoutParams().height = newHeight;
try {
((RelativeLayout.LayoutParams) mView.getLayoutParams()).topMargin = topMargin;
((RelativeLayout.LayoutParams) mView.getLayoutParams()).leftMargin = leftMargin;
((RelativeLayout.LayoutParams) mView.getLayoutParams()).bottomMargin = bottomMargin;
((RelativeLayout.LayoutParams) mView.getLayoutParams()).rightMargin = rightMargin;
} catch (Exception e) {
e.printStackTrace();
}
mView.requestLayout();
//mView.invalidate();
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds() {
return true;
}
}
And this is the method that handles the FragmentTransaction.
#Override
public void showSidebarFragment() {
resizableLayout.minimize();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.sidebar_frame, sidebarFragment, "sidebarFragment");
ft.commit();
mContentRelativeLayout.requestLayout();
sidebarframe.requestLayout();
}
Mind you that Sidebarframe is injected through Butterknife and sidebarFragment is injected through Dagger2 - we use the same instance of the fragment for everything.
I have no clue what's going on. I've tried several ways of bringing the Fragment to front but nothing seems to work. I'd love if anyone could give me a hand either on how to fix the issue or how to achieve the same effect through other means - whatever works.
I'm creating a custom linear layout to hold two image views.
I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. Then understood that the problem is the image views, they simply not showing.
Thanks in advance :-)
This is the class:
public class LoginIcons extends LinearLayout {
private ImageView mImageViewLogo;
private ImageView mImageViewIcons;
private View mView;
boolean test = false;
public LoginIcons(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoginIcons(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.VERTICAL);
mImageViewLogo = new ImageView(this.getContext());
mImageViewIcons = new ImageView(this.getContext());
mView = new View(getContext());
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mImageViewLogo.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
mImageViewIcons.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
mView.setLayoutParams(params);
mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);
mImageViewIcons.setImageResource(R.drawable.login_icons);
mImageViewIcons.setBackgroundColor(Color.BLACK);
mImageViewLogo.setBackgroundColor(Color.BLACK);
addView(mView);
addView(mImageViewLogo);
addView(mImageViewIcons);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setViewsDimensions();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
private void setViewsDimensions() {
LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();
int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());
// 0.375 percent
int imgDim = (int) ((double) screenWidth * 0.32);
int iconsWidth = (int) ((double) screenWidth * 0.5);
LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
.getLayoutParams();
params.width = imgDim;
params.height = imgDim;
mImageViewLogo.setLayoutParams(params);
params = (LayoutParams) mImageViewIcons.getLayoutParams();
params.width = iconsWidth;
mImageViewIcons.setLayoutParams(params);
}
}
And this 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"
android:background="#color/HeaderBackground"
android:orientation="vertical" >
<Button
android:id="#+id/button_ok"
style="#style/LoginOkButtonStyle"
android:background="#drawable/blue_button_background_selector"
android:text="#string/ok" />
<com.example.me.entities.views.LoginIcons
android:id="#+id/loginIcons"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.example.me.entities.views.LoginIcons>
</LinearLayout>
It seems that the View object acted like match_parent and had height of nearly 400 pixels. This caused the image views to drop outside the boundaries of the screen.
Once I removed the View the image views turned up.