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?
Related
This is my xml .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/secondView"
android:orientation="vertical">
<FrameLayout
android:id="#+id/videoFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.videolan.libvlc.util.VLCVideoLayout
android:id="#+id/videoLayout"
android:fitsSystemWindows="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/secondView"
android:layout_weight="1"
android:background="#android:color/holo_orange_light"
android:orientation="vertical">
</LinearLayout>
This is my class :
public class WifiActivity extends AppCompatActivity {
private BroadcastReceiver MyReceiver = null;
private static final String url = " rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";
private LibVLC libVlc;
private MediaPlayer mediaPlayer;
private VLCVideoLayout videoLayout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
libVlc = new LibVLC(this);
mediaPlayer = new MediaPlayer(libVlc);
videoLayout = findViewById(R.id.videoLayout);
}
#Override
protected void onStart() {
super.onStart();
mediaPlayer.attachViews(videoLayout, null, false, false);
Media media = new Media(libVlc, Uri.parse(url));
media.setHWDecoderEnabled(true, false);
media.addOption(":network-caching=600");
mediaPlayer.setMedia(media);
media.release();
mediaPlayer.play();
}
}
after running this code I am able to show live streaming using the given URL look like this.
I am trying to remove the black bar which is showing in the given image I want to keep it full screen please help me how to achieve this I am unable to find any solution for this.
you are setting layout_height="match_parent" for VLCVideoLayout, so currently it behaves properly, according to written code
if you want different height for this view, then you should calculate it and set for VLCVideoLayout. here is fun fact: this class will re-set its width and height at runtime (check out onAttachedToWindow method), so even when you set some fixed values in XML - these will be overwritten. its made for "some reason" by library authors, so let's don't touch this class, but now we know that VLCVideoLayout will always stretch/fit to parent
so now I would create some parent class for this video view only
<LinearLayout
android:id="#+id/firstLayout"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/secondView"
android:orientation="vertical">
<FrameLayout
android:id="#+id/videoFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.videolan.libvlc.util.VLCVideoLayout
android:id="#+id/videoLayout"
android:fitsSystemWindows="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
now as we know that frame/video will match_parent width then we can measure it by measuring screen. if this video would be packed into some more complicated layout, e.g. some small video frame in corner - you should measure size of this area (which oftenly have fixed width/size in such case)
knowing videoFrame width you can calculate easily height with just multiplying by ratio of video
int scrWidth = getScreenWidth();
FrameLayout frameLayout = findViewById(R.id.videoFrame);
double ratio = 16d/9d; // 16:9
frameLayout.getLayoutParams().height = (int)(scrWidth / ratio);
I have two views on parent Layout those are top and bottom screens and bottom screen having fixed height and its align to parent-bottom and top screen based on content it must be extend and collapse on parent Layout when i tapped on it and bottom screen automatically come down from top screen and i can able to scroll total content on parent Layout can some one help me please
activity_main.layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_view"
android:background="#3143ff" />
<View
android:id="#+id/bottom_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary" />
</RelativeLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View view = findViewById(R.id.view);
final View contentView = findViewById(R.id.content_frame);
view.setOnClickListener(v -> {
final int screenHeight = contentView.getHeight();
ValueAnimator heightAnimator = ValueAnimator.ofInt(view.getHeight(), screenHeight);
heightAnimator.setDuration(1500);
heightAnimator.addUpdateListener(animation -> {
view.getLayoutParams().height = (int) animation.getAnimatedValue();
view.requestLayout();
});
heightAnimator.start();
});
}
}
See FoldingCell for Android for more help.
In your build.gradle()
dependencies{
implementation 'com.ramotion.foldingcell:folding-cell:1.2.2'
}
In you XML layout
<com.ramotion.foldingcell.FoldingCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folding_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Layout One-->
<!--Layout Two-->
</com.ramotion.foldingcell.FoldingCell>
Almost done! Two steps remain! For correct animation, you need to set up two properties on the root element(s) of your Folding Cell:
android:clipChildren="false"
android:clipToPadding="false"
In your Java class
// get our folding cell
final FoldingCell fc = (FoldingCell) findViewById(R.id.folding_cell);
// attach click listener to folding cell
fc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fc.toggle(false);
}
});
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.
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);
}
});