The below is the xml that iam inflating within my customList
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<exp.viswanath.dropper.CustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#e1e1e1"
android:padding="4dp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/custom_view" />
</RelativeLayout>
</LinearLayout>
And my custom view Layout is
<?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:orientation="vertical" >
<exp.viswanath.dropper.CustomList
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Iam getting the height of Layout with id parent and listview as -1 and -2 . But iam getting the correct height for element with id custom_view as 105. Iam inflating this within the constructor of my CustomList
CODE
View rowView = LayoutInflater.from(context).inflate(R.layout.activity_dropper, null);
parentLayout = (RelativeLayout)rowView.findViewById(R.id.parent);
Log.d("", "DEBUG:pl:" + parentLayout.getLayoutParams().height);
myView = (CustomView)parentLayout.findViewById(R.id.custom_view);
_viewHeight = myView.getLayoutParams().height;
initList();
_height = (-1) * _viewHeight;
Log.d("","height:" + _height);
listView = (ListView)parentLayout.findViewById(R.id.listView);
Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
Does any one knows the problem or experienced the same? The layout is not visible at all
PROBLEM
MY VIEW IS NOT VISIBLE
The problem is that myView.getLayoutParams().height; will not return you the height as it has not been measured yet. In tghis case it returns you the constant associated with the view such as FILL_PARENT, WRAP_CONTENT etc. See documentation here:
Information about how tall the view wants to be. Can be one of the
constants FILL_PARENT (replaced by MATCH_PARENT , in API Level 8) or
WRAP_CONTENT. or an exact size.
To get the size of a layout, your should be using getWidth() or getMeasuredWidth() methods. However, these methods won't give you a meaningful answer until the view has been measured. Read about how android draws views here.
You can get the correct size, by overriding the onWindowFocusChanged() as mentioned in this thread.
Alternatively, you could do this:
myView.post(new Runnable() {
#Override
public void run() {
Log.d("", "DEBUG:lv:" + myView.getMeasuredHeight());
}
});
Try this way
listView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
}
});
Related
I've got a CoordinatorLayout, including a LinearLayout as my BottomSheet. There is also a FrameLayout (direct child of the CoordinatorLayout as well).
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">
<FrameLayout
android:id="#+id/sketchViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="#drawable/rounded_top"
android:backgroundTint="#color/colorBottomSheetBackground"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="#dimen/bottomSheetPeekHeight"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/bottomSheetPeekHeight"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="#+id/fixedPointsSeekBar"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:max="10"
android:progress="10" />
</LinearLayout>
</LinearLayout>
I use the Processing.jar libraries to draw stuff on the FrameLayout.
In order to do this I created a Fragment (so called PFragement in Processing) and added it to the FrameLayout via the SupportFragmentManager. The PFragment takes a PApplet as a constructor. In the PApplet the canvas drawing takes place and also the width and the height get defined. Furthermore I sized the PApplet to fill my FrameLayout.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sketch = Sketch();
sketchViewGroup.post {
sketch.sketchWidth = sketchViewGroup.width
sketch.sketchHeight = sketchViewGroup.height
// this works:
// sketch.sketchWidth = sketchViewGroup.width -1
// sketch.sketchHeight = sketchViewGroup.height -1
supportFragmentManager.beginTransaction().add(sketchViewGroup.id, PFragment(sketch)).commit();
}
}
}
and
public class Sketch extends PApplet {
int sketchWidth = 100;
int sketchHeight = 100;
public void settings() {
size(sketchWidth, sketchHeight);
}
public void setup(){
background(0);
}
}
Now, wired things are happening: When I size the Sketch to exactly the size of my FrameLayout my BottomSheet does not expand properly anymore. But if I subtract one of the size of my sketch everything works as expected. Can someone explain this and tell how to fix this?
sketch.sketchWidth = sketchViewGroup.width
sketch.sketchHeight = sketchViewGroup.height
sketch.sketchWidth = sketchViewGroup.width -1
sketch.sketchHeight = sketchViewGroup.height -1
It looks like the height of your view group is MATCH_PARENT which is -1. When you subtract 1 from that you get -2 which equals WRAP_CONTENT. So I think you are just setting the height of the sketch object to be WRAP_CONTENT?
i have a main LinearLayout and two RecyclerView as child and need to get height size of second RecyclerView
i tried by rv_show_adv.getHeight() and rv_show_adv.getLayoutParams().height but get me 0 but when i test the app in a real device i could to see i have height and width on both but why in java code i always got 0 pixel !
/layout/activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/mainlinear"
android:orientation="vertical"
tools:context=".main.MainActivity"
android:background="#f3cdcd"
android:weightSum="1"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="55dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_selected_adv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_show_adv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#b9f1ce"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
really confused because when i have a View (like as TextView) i could to get height by View.getHeight() but why not worked when i had a ViewGroup
i also try to get height of second child of main LinearLayout by LinearLayout.getChildAt(1).getHeight() but again get me 0
also try to define RecyclerView variable in onCreate() and wants to get height in onResume() lifecycle but not different!
i think i miss somethings in my code what do you suggestion ?
Because of View.getHeight() on onCreate is 0, view need onMeasure() when onCreateView so onMeasure() maybe finished after onCreated, you should do this:
int mWidth , mHeight ;
rv_show_adv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rv_show_adv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
rv_show_adv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
mWidth = rv_show_adv.getWidth();
mHeight = rv_show_adv.getHeight();
}
});
I'm trying to find the center coordinates of a RelativeLayout that for the moment, is empty. I need to find the center before I can put anything into the Relative Layout. The Relative Layout is not the whole screen, so I can't use metrics to find the screen dimensions. Since my minimum API is 10, I can't use rLayout.getX() or rLayout.getY(). So what I've tried is:
int xScreenInt = gBoard_RL.getWidth()/2;
int yScreenInt = gBoard_RL.getHeight()/2;
int xInt = gBoard_RL.getLeft() + xScreenInt;
int yInt = gBoard_RL.getTop() + yScreenInt;
and all I get is 0,0. Does anyone have an any ideas how I can get this done? Here's my layout and the Activity so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/gb1_root_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".GB1" >
<RelativeLayout
android:id="#+id/gb1_topInfo_RL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/blu_grade_bg"
android:orientation="vertical"
android:paddingBottom="15dp" >
<TextView
android:id="#+id/gb1_scValue_TV"
style="#style/mainScoreValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/gb1_score_TV"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/gb1_timeValue_TV"
style="#style/mainTimeValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/gb1_time_TV"
android:layout_marginRight="10dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/gb1_gf_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/gb1_topInfo_RL"
android:padding="10dp" >
</RelativeLayout>
the Relative Layout I need to find the center of is: android:id="#+id/gb1_gf_RL"
public class GB1 extends Activity {
TextView GScore_TV;
RelativeLayout gBoard_RL;
int xScreenInt, yScreenInt, xInt, yInt;
String xStr, yStr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
setContentView(R.layout.gb_layout);
GScore_TV = (TextView) findViewById(R.id.G1_scoreValue_TV);
gBoard_RL = (RelativeLayout) findViewById(R.id.gb1_gf_RL);
gBoard_RL.setBackgroundColor(Color.MAGENTA);
xScreenInt = gBoard_RL.getWidth()/2;
yScreenInt = gBoard_RL.getHeight()/2;
xInt = gBoard_RL.getLeft() + xScreenInt;
yInt = gBoard_RL.getTop() + yScreenInt;
xStr = String.valueOf(xInt);
yStr = String.valueOf(yInt);
GScore_TV.setText(xStr + ", " + yStr);
}
}
Try putting your code in a onGlobalLayout listener:
myRelativelayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//Your code here
}
});
EDIT:
Since, as #xorgate pointed out, the layout isnt drawn yet in the onCreate method. The onGlobalLayout method will be called when the layout is drawn and calculations such as yours should be done in there.
onCreate is called before a layout pass is done. All Views will have no size yet.
Try setting a global layout listener, and do the positioning code there.
I have a VerticalViewPager and I have some images in it. When I rotate my device in landscape mode my ImageView doesn't scale to width of my screen. It fits the height if image instead. I used AspectRatioImageView. It fits the width but VerticalViewPager doesn't scroll down.
Thank you.
activity_main.xml:
<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"
tools:context=".MainActivity" >
<HackyViewPager
android:id="#+id/vvp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
>
</HackyViewPager>
</RelativeLayout>
Here is rowitemview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >
<AspectRatioImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/m4_3" />
</LinearLayout>
and here is my instaniateItem of my ImagePageAdapter that extends PagerAdapter:
#Override
public Object instantiateItem(ViewGroup container, int position)
{
Context context = MainActivity.this;
container.requestLayout();
AspectRatioImageView imageView = new AspectRatioImageView(context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.m4_3));
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnMatrixChangeListener(new OnMatrixChangedListener() {
#Override
public void onMatrixChanged(RectF rect) {
if((int)rect.width()>_width)
viewPager.setLocked(true);
else
viewPager.setLocked(false);
}
});
P.S : The issue is with viewPager height. When I scroll down the image it just goes to the other page instead of scrolling the scaled image.
Here's what you need.
This is a good solution I found out.
Android Crop Center of Bitmap
This allows to set the height and your width according to your orientation.
To change the image's scale to full screen, you generally use center crop like this:
yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Hope this information was useful..:)
Use android:scalType = "fitxy" in activity code like this imageView.setScaleType(ImageView.ScaleType.FIT_XY);
As title says, my listview won't get filled.
I know that the adapter is getting filled with 3 items, debugged my way through it. But my xml isnt showing anything at all:
The adapter is a global variable if that should do anything.
Code:
private void listChange(CustomCursor c){
List<SimpleListItem> itemsList = new ArrayList<SimpleListItem>();
while (c.moveToNext()) {
String picture = c.getString("picture");
String text = c.getString("name");
itemsList.add(new SimpleListItem(text, picture));
}
productListAdapter = new ProductAdapter(getBaseContext(),R.layout.product_item, itemsList);
setListAdapter(productListAdapter);
productListAdapter.notifyDataSetChanged();
}
XML of the activity:
<?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:orientation="horizontal" >
<dk.foo.views.PageHeaderView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</dk.foo.views.PageHeaderView>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Modify the orientation of your LinearLayout to vertical if you want to see the ListView.
Right now with the orientation of your parent LinearLayout set to horizontal and the width of your custom view PageHeaderView set to FILL_PARENT, your ListView is pushed out of the screen.