Android FrameLayout position using Fragment - android

I am adding a fragment with:getSupportFragmentManager().beginTransaction().replace(R.id.fragment_host, TutorialFragment_.builder().build(), "tutorial").commit();
the FrameLayout is inside a RelativeLayout using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible">
<ImageView
android:id="#+id/phone_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="-100dp"
android:adjustViewBounds="true"
android:src="#drawable/phone_tutorial"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="60dp"
android:visibility="invisible"
android:src="#drawable/a_capacity" />
<ImageView
android:id="#+id/tuto_image_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:src="#drawable/a_loan_simulation"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="100dp"
android:src="#drawable/a_checklist"
android:visibility="invisible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="#drawable/add_btn_circle_center"
android:visibility="gone" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/mButtons"
android:layout_marginBottom="20dp" />
<LinearLayout
android:id="#+id/mButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical"
android:padding="15dp"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/next_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="right"
android:text="#string/Generic_Button_Next"
android:textColor="#color/processStepCategoryTextColor" />
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/skip_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="left"
android:text="#string/Generic_Button_Cancel"
android:textColor="#color/processStepCategoryTextColor" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The TutorialFragment object uses this layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
No matter what I try, this FrameLayout is always aligned on top of the screen, and I need it to be at the bottom of the screen.

Try by change your layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible" >
.....
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:layout_marginBottom="20dp" />
Now view pager will come below 'relLayout' and above 'mButtons'

maybe your RelativeLayout is not full screen,so the FrameLayout will never aligned the bottom of the screen

You can try this,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_alignParentBottom="true"
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

you can use like this also:
public class MyViewPager extends ViewPager {
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// find the first child view
View view = getChildAt(0);
if (view != null) {
// measure the first child view with the specified measure spec
view.measure(widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}
private int measureHeight(int measureSpec, View view) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
// set the height from the base view if available
if (view != null) {
result = view.getMeasuredHeight();
}
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
in xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:background="#0000ff" >
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>

Related

I want the entire activity to be scrollable but only listview is scrollable. How do I do that?

I pull data into my listview from servers. The listview is inside an activity. The activity has buttons above it.
When the data is received and when I scroll data, only listview is scrolled. The buttons remain fixed at the top.
I want the listview to take the required vertical height it needs so that I would need to scroll the activity instead of the listview.
My XML code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_c_name"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:text="text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_motto" />
<TextView
android:text="text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_motto"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_details" />
<TextView
android:text="text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_address"
android:layout_below="#+id/view_c_details"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_act_text"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="71dp" />
<Button
android:text="Button 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="button 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:layout_toEndOf="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button2" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view_c_act_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/my_listview"
android:scrollbars="none"
/>
</RelativeLayout>
The perfect code should be;
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Inside the below layout your buttons will come. Modify it accordingly-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--Your buttons will come here-->
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
As shown above, Nested Scroll View should be used instead of Scroll View because
there is a need of scrolling view (in this case List View) inside another scrolling view. The system is unable to decide which view to scroll and this is where Nested Scroll View comes in.
Wrap your existing layout in a ScrollView to scroll the entire thing, not just the ListView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your existing layout here, change your top container's height from match_parent to wrap_content -->
</ScrollView>
Try this one
<ScrollView
android:id="#+id/scrollViewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent">
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
"your existing layout here, change your top container's height from match_parent or wrap_content "
the best case secanrio is to use scroll View inside of an relative layout and all the existing code inside of the scrollView
I had similar problem then I created a class
public class ListViewExpanded extends ListView {
public ListViewExpanded(Context context, AttributeSet attrs) {
super(context, attrs);
setDividerHeight(0);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
}
}
then I used in xml as try it out it will help for sure
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/text_goal_scored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<example.ListViewExpanded
android:id="#+id/list_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</example.ListViewExpanded>
</android.support.v4.widget.NestedScrollView>
Go through this Example. Make ScroolView as Parent. It Work Form me.
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/maintool"
layout="#layout/toolbar"></include>
<RelativeLayout
android:id="#+id/re1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/re2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Email template"
android:textSize="18sp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:paddingTop="10dp" />
</RelativeLayout>
</LinearLayout>
</ScroolView>
this is how you can do your task.this is Scroll disabled listview so you can scroll your activity easily.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* Created by sudesh Regmi on 4/20/2017.
*/
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
setListViewHeightBasedOnChildren(your_listview);
add this line in your activity
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
add scrollview at parent in your xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/view_c_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text1"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/view_c_motto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_name"
android:text="text4" />
<TextView
android:id="#+id/view_c_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_motto"
android:text="text5" />
<TextView
android:id="#+id/view_c_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_details"
android:text="text2" />
<TextView
android:id="#+id/view_c_act_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginTop="71dp"
android:text="text3" />
<Button
android:id="#+id/view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Button 1" />
<Button
android:id="#+id/view_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:text="button 2" />
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_act_text"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>

How to hide widget when keyboard pops?

I have two ViewPager on the same view with weight= 70/30 . The problem is when the keyboard pops, my both view keep the same weight and do not display my EditText.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context="com.suez.memboard.fragments.formWaterQuality">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="30">
<com.suez.memboard.controls.TextView
android:id="#+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="#style/FreeTextBold" />
<LinearLayout
android:id="#+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forms.label.intervention_date"
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" : "
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.DateTimeText
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:drawableRight="#drawable/ic_date_range_black_24dp"
android:focusable="false"
android:hint="forms.label.intervention_date"
fab:type="date" />
</LinearLayout>
<Spinner
android:id="#+id/spin_location"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
<ImageButton
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
android:src="#drawable/ic_add_box_white_48dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="70">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<FAB ....>
I think my last solution is to make disappear my picker at the top when my keyboard pops. How can I do this?
Hope this help you :
public class YourActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
attachKeyboardListeners();
}
#Override
protected void onShowKeyboard(int keyboardHeight) {
// when keyboard is shown
yourWidget.setVisibility(View.GONE);
}
#Override
protected void onHideKeyboard() {
// when keyboard is hidden
yourWidget.setVisibility(View.VISIBLE);
}
}
Try to use ScrollView in parent view, for example-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<ScrollView
android:id="#+id/scroll_view_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
You can set listeners when keyboard is opened and closed.
// ContentView is the root view of the layout of this activity/fragment
contentView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.d(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
} else {
// keyboard is closed
}
}
});

how to set full screen video when rotating android device

Am using Video view to play the MP4 video files from external card. when i rotate my android device video not showing full screen. am working on android 4.4 normal including the ui output screen below
when i rotate phone
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/video"
android:orientation="vertical"
android:background="#drawable/planebackground">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="200dp"
>
<VideoView
android:id="#+id/VideoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="#+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="#+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="#+id/aboutcuz"
android:layout_alignLeft="#+id/aboutcuz"
android:layout_alignStart="#+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="#+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/aboutcuz"
android:layout_alignStart="#+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="#android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray"/>
</RelativeLayout>
</LinearLayout>
is there any layout issue in my code
Please try this. I hope this may result you desired results.
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/video"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<VideoView
android:id="#+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="#+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="#+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="#+id/aboutcuz"
android:layout_alignLeft="#+id/aboutcuz"
android:layout_alignStart="#+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="#+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/aboutcuz"
android:layout_alignStart="#+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="#android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray"/>
</RelativeLayout>
Make your custom VideoView class
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
public void setFixedVideoSize(int width, int height)
{
getHolder().setFixedSize(width, height);
}
}
and set in your Layout insted of VideoView
<yourpackage.FullScreenVideoView
android:id="#+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Do something like this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Logger.debug(TAG, "onConfigChanged");
updateLayout(newConfig.orientation);
}
/**
* Update the current layout params according to current device orientation.
*/
private void updateLayout(int orientation) {
LinearLayout.LayoutParams favParams = (LinearLayout.LayoutParams) mFavoriteLayout.getLayoutParams();
LinearLayout.LayoutParams historyParams = (LinearLayout.LayoutParams) mHistoryLayout.getLayoutParams();
LinearLayout.LayoutParams colParams = (LinearLayout.LayoutParams) mColumnLayout.getLayoutParams();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mColumnLayout.setOrientation(LinearLayout.VERTICAL);
historyParams.width = favParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.height = favParams.height = 0;
colParams.weight = 1.0f;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
historyParams.height = favParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.width = favParams.width = 0;
colParams.weight = 2.0f;
}
// Set back updated params and redraw
mFavoriteLayout.setLayoutParams(favParams);
mHistoryLayout.setLayoutParams(historyParams);
mColumnLayout.setLayoutParams(colParams);
mColumnLayout.invalidate();
}
When you change orientation change weight and you can achieve what you want.
It's quite simple.You can find an example when you create a new "full screen activity".
It's something like this:
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

How to set the Grid view and View pager inside the scroll view in android?

In my XML i set the entire screen as scrollable, I created the scroll view inside that set the Framelayout for View pager and Grid view. Here Grid view only scrolled but it doesn't scroll the entire screen.
Below is my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="#+id/framelayoutViewpager">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/btnImagePrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous" />
<Button
android:id="#+id/btnImageNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
</FrameLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/framelayoutViewpager"
android:numColumns="3"
android:stretchMode="columnWidth"
android:paddingTop="7dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/gridView" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
NonScrollGridView will do the trick here..
NonScrollGridView
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class NonScrollGridView extends GridViewWithHeaderAndFooter {
public NonScrollGridView(Context context) {
super(context);
}
public NonScrollGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
If ready to use AppCompact support lib then try NestedScrollView and CoordinatorLayout.
sample layout code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/btnImagePrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous" />
<Button
android:id="#+id/btnImageNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/framelayoutViewpager"
android:numColumns="3"
android:stretchMode="columnWidth"
android:paddingTop="7dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/gridView" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Slide In View Animation

I want a relative layout to slide into a screen.
At the moment I set the layout to be below a layout that takes up the whole screen
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ghostIv"
android:src="#drawable/results_ghost"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="#+id/ghostShadowIv"
android:visibility="gone"
android:src="#drawable/results_ghost_shadow"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ghostIv"/>
<TextView
android:id="#+id/noFilterTv"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textColor="#color/LightTextGrey"
android:visibility="gone"
android:text="#string/no_filters"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/ghostShadowIv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<com.allryder.android.ui.views.ScheduleView
android:visibility="invisible"
android:background="#color/White"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scheduleView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
<View
android:id="#+id/shadowV"
android:visibility="gone"
android:layout_above="#+id/ratingRl"
android:layout_height="4dp"
android:layout_width="match_parent"
android:background="#drawable/bottom_shadow"/>
<RelativeLayout
android:id="#+id/ratingRl"
android:layout_below="#+id/scheduleView"
android:background="#color/White"
android:clickable="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:background="#drawable/time_view_selector"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/ratePromptTv"
android:layout_toLeftOf="#+id/closeRatingIv"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center|left"
android:text="#string/rate_prompt"/>
<ImageView
android:background="#drawable/time_view_selector"
android:padding="10dp"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ratePromptTv"
android:layout_alignBottom="#+id/ratePromptTv"
android:id="#+id/closeRatingIv"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/search_clear"/>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<fragment android:id="#+id/filters_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:name="com.allryder.android.ui.fragments.FiltersDrawerFragment"/>
Then make this call
ratingRl.animate().translationY(- ratingRl.getHeight());
But it doesn't slide into the screen. If the view is in the screen to start off with, the view does animate. What is the problem?
ratingRl.animate().translationY(- ratingRl.getHeight()).setDuration(300).start();
I have done similar task in my recent project.
background = (RelativeLayout) findViewById(R.id.slidingBackground);
positionOffScreen(background);
private void positionOffScreen(View v){
View myView = v;
int amountOffscreen = (int)(getScreenWidth());
boolean offscreen = true;
int xOffset = (offscreen) ? amountOffscreen : 0;
RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams)myView.getLayoutParams();
rlParams.setMargins(-1*xOffset, 0, xOffset, 0);
myView.setLayoutParams(rlParams);
}
protected float getScreenWidth() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return (float) displaymetrics.widthPixels;
}
When you want to start animation call this method
private void startAnimation(int duration){
TranslateAnimation a = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,getScreenWidth(),Animation.ABSOLUTE,0,Animation.ABSOLUTE,0);
a.setDuration(duration);
a.setFillAfter(false);
background.startAnimation(a);
}
When you want to interrupt animation
background.clearAnimation();
Here is layout example. The sliding view has to be placed on RelativeLayout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<RelativeLayout
android:id="#+id/slidingBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#64000000"
android:orientation="vertical" >
</RelativeLayout>
...
And this is the app link where I use this sliding view.

Categories

Resources