Scrolling scrollview programmatically when keyboard is shown - java

I have been searching for a long time, but none of the answers were helpful.
I'm making a chat app. When the user clicks on input edit text the keyboard shows up, and I want to automatically scroll to the bottom of scroll view, but it does not work as expected. It looks like it thinks that the scrollview has different dimensions than it really has. (probably doesn't realize that keyboard is taking some space)
When keyboard is hidden, programitacally scrolling to the bottom works as expected.
This is what i have now:
In manifest:
<activity
android:name=".ChatActivity"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait" />
In activity:
messageEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
HideKeyboard(v);
// ScrollToBottom ();
}
else
{
ScrollToBottom();
messagesScrollView.post(new Runnable() {
#Override
public void run() {
messageEditText.requestFocus();
}
});
ScrollToBottom();
}
}
});
private void ScrollToBottom ()
{
messagesLinearLayout.requestLayout();
messagesLinearLayout.post(new Runnable() {
#Override
public void run() {
messagesScrollView.scrollTo(0, messagesScrollView.getBottom());
}
});
}
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#color/black"
android:clickable="true"
android:focusableInTouchMode="true"
tools:context=".ChatActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.LetsGOFriends.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="66dp"
android:background="#242A2F"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetEnd="0dp"
app:buttonGravity="center_vertical"
app:popupTheme="#style/Theme.LetsGOFriends.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|start">
<TextView
android:id="#+id/trainerNameChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/profilePictureImageView"
android:layout_marginStart="10dp"
android:layout_marginTop="-2dp"
android:layout_toEndOf="#+id/profilePictureImageView"
android:maxLines="1"
android:textSize="14sp"
android:text="LoriMontana"
android:textColor="#color/white" />
<TextView
android:id="#+id/trainerCodeChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/profilePictureImageView"
android:layout_alignStart="#+id/trainerNameChat"
android:layout_marginBottom="-2dp"
android:layout_marginStart="0dp"
android:maxLines="1"
android:textSize="14sp"
android:text="0467 0321 3849"
android:textColor="#color/white" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/profilePictureImageView"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="15dp"
android:layout_marginStart="0dp"
android:layout_marginTop="15dp"
android:padding="1dp"
app:shapeAppearanceOverlay="#style/roundedImageViewRounded"
app:srcCompat="#drawable/ic_default_profile_picture"
app:strokeColor="#484848"
app:strokeWidth="1dp" />
<ImageView
android:id="#+id/languageFlagChatImageView"
android:layout_width="18dp"
android:layout_height="16dp"
android:layout_alignBottom="#+id/trainerNameChat"
android:layout_alignTop="#+id/trainerNameChat"
android:layout_marginBottom="4dp"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/trainerNameChat"
tools:srcCompat="#tools:sample/avatars" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:id="#+id/messagesScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="-15dp"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toTopOf="#+id/cardView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout">
<LinearLayout
android:id="#+id/messagesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusableInTouchMode="true"
android:paddingBottom="20dp"
android:paddingTop="5dp"
android:orientation="vertical"
android:focusable="true" />
</ScrollView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout2"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
app:boxBackgroundColor="#242A2F"
app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp"
app:boxStrokeColor="#color/teal_700"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:endIconContentDescription="Send"
app:endIconDrawable="#drawable/ic_baseline_send_24"
app:endIconMode="custom"
app:endIconTint="#color/white"
app:hintEnabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Enter your message.."
android:maxLength="250"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="#color/white"
android:textColorHint="#D3D3D3" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
I think it's something wrong with layout file.

Try this code for scrolling down:
messagesScrollView.fullScroll(ScrollView.FOCUS_DOWN);

Related

Button setonclick listener is not working correctly

when I press the save button does not work after pressing it several time button is working sometimes first-time press is working sometimes after pressing moreover 10 times button is not working.Also i have toast messege to determine it is working or not the toast messege appear after pressing several time to the button.
binding.savebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Settringsactivity.this,"ok",Toast.LENGTH_LONG).show();
String status=binding.etStatus.getText().toString();
String username=binding.etUsername.getText().toString();
HashMap<String, Object> obj=new HashMap<>();
Toast.makeText(Settringsactivity.this,username,Toast.LENGTH_LONG).show();
obj.put("username",username);
obj.put("about",status);
if(name!=null) {
Toast.makeText(Settringsactivity.this,"Updated",Toast.LENGTH_LONG).show();
database.getReference().child("Users").child(name).updateChildren(obj);
}
}
});
Here is the xml code
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/savebtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#color/white"
android:foregroundTint="#B11B1B"
tools:context=".Settringsactivity">
<ImageView
android:id="#+id/settingsbackarraow"
android:layout_width="30dp"
android:layout_height="30dp"
android:backgroundTint="#FFFFFF"
app:srcCompat="#android:drawable/checkbox_on_background" />
<LinearLayout
android:id="#+id/flinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/settingsbackarraow"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profileimage"
android:layout_width="120dp"
android:layout_height="120dp"
android:backgroundTint="#FFFFFF"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp"
android:src="#drawable/img" />
<ImageView
android:id="#+id/plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/btn_plus" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#100606" />
<EditText
android:id="#+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#color/black"
android:hint="Enter your name"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#100606" />
<EditText
android:id="#+id/etStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="About"
android:textColor="#color/black"
android:inputType="textPersonName" />
</LinearLayout>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:elevation="15sp"
android:background="#1966FB"
android:layout_gravity="right"
android:text="Save" />
</LinearLayout>
</RelativeLayout>

Markers title and Traffic view, googlemap android

i am facing some problems with anable and disable traffic TYPE on the google map when OnMapReady.I have tow differents MAP_TYPE_NORMAL and MAP_TYPE_HYBRID.
what i want is to setOnclickListener when button is clicked.
my button code:
maptrafic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(map.getMapType() == GoogleMap.MAP_TYPE_NORMAL || map.getMapType() == GoogleMap.MAP_TYPE_HYBRID){
map.setTrafficEnabled(true);
maptrafic.setImageResource(R.drawable.trafficmap_on);
}
else {
map.setTrafficEnabled(false);
maptrafic.setImageResource(R.drawable.trafficmap_off);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
});
also, i am working on googlemap using REST API i have successfuly create windowsInfoMarker but what i need is when map ready is to see the marker name only not all information like windowsInfoMarker but only Markers with name.
my MapActivity code shared from drive
MapActivity.java
my activity_map.xml
<RelativeLayout 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:orientation="vertical"
android:id="#+id/drawer"
android:layout_height="match_parent"
tools:context=".activities.MapActivity">
<RelativeLayout
android:id="#+id/mapfloating"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--inclusion de la carte-->
<include layout="#layout/include_main_map" />
<!--fin inclusion de la carte-->
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/mainFloatingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_margin="25dp"
app:fab_labelStyle="#style/custom_floating_buttons"
android:layout_alignParentBottom="true"
app:fab_addButtonColorNormal="#color/blue">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/evennements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_icon="#drawable/icon_events"
app:fab_title="Evennements"
android:background="#drawable/cerclebackground"
app:fab_colorNormal="#color/purple_500"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/historiques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_icon="#drawable/icon_history"
app:fab_title="Historique"
android:background="#drawable/cerclebackground"
app:fab_colorNormal="#color/purple_500"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/reglages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_icon="#drawable/icon_setup"
app:fab_title="Reglages"
android:background="#drawable/cerclebackground"
app:fab_colorNormal="#color/purple_500"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/aide_support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_icon="#drawable/icon_support"
app:fab_title="Aide/Support"
android:background="#drawable/cerclebackground"
app:fab_colorNormal="#color/purple_500"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingPopUpLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:contentDescription="#string/app_name"
app:fab_colorDisabled="#color/white"
app:fab_colorNormal="#color/white"
app:fab_colorPressed="#color/blue"
app:fab_icon="#drawable/ic_menu_24" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingPopUpRight"
style="#style/custom_floating_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:contentDescription="#string/app_name"
app:fab_colorDisabled="#color/white"
app:fab_colorNormal="#color/white"
app:fab_colorPressed="#color/blue"
app:fab_icon="#drawable/ic_baseline_notifications_active_24" />
</RelativeLayout>
</RelativeLayout>
my include_mean_map
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/content_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true" />
<LinearLayout
android:id="#+id/zoomio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="#+id/fonctio"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="290dp"
android:layout_marginStart="9dp">
<ImageButton
android:id="#+id/zoom_in"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/zoom_in_selector"
android:layout_marginStart="4dp"
tools:ignore="ContentDescription" />
<ImageButton
android:id="#+id/zoom_out"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="4dp"
android:layout_marginTop="5dp"
android:src="#drawable/zoom_out_selector"
tools:ignore="ContentDescription,TouchTargetSizeCheck,SpeakableTextPresentCheck" />
</LinearLayout>
<LinearLayout
android:id="#+id/fonctio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="70dp"
android:layout_marginStart="9dp">
<ImageButton
android:id="#+id/showtails"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:src="#drawable/tail_active"
tools:ignore="ContentDescription,TouchTargetSizeCheck" />
<ImageButton
android:id="#+id/autozoom"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/icon2022"
android:layout_marginTop="5dp"
tools:ignore="ContentDescription" />
<ImageButton
android:id="#+id/geofences"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/geofence_active"
android:layout_marginTop="5dp"
tools:ignore="ContentDescription" />
<ImageButton
android:id="#+id/map_layer"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:src="#drawable/map_layer_change_icon_inactive"
tools:ignore="ContentDescription,TouchTargetSizeCheck" />
<ImageButton
android:id="#+id/map_trafic"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/grey_round_rect"
android:layout_marginTop="5dp"
android:src="#drawable/trafficmap_on"
tools:ignore="ContentDescription" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/loading_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:visibility="gone"
android:gravity="center">
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:indeterminate="true"
style="#style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/nodata_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:visibility="gone"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/noMapData"/>
</RelativeLayout>
</RelativeLayout>
if you have any idea please don't ready only i need help. any idea are welcome.
if you need more details juste ask i am online 24/7 each second.

Automatically go to furthest right (last horizontal child) then back to furthest left(first child) in a horizontal scroll view

I have cards in a horizontal LinearLayout which is then in a HorizontalScrollView that I would like to rotate about left to right and back like an image slideshow.
Here is my XML, it contains 5 cards which are in horizontal alignment
<HorizontalScrollView
android:id="#+id/horizontal_scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 1"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 2"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 3"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 4"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 5"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
I have tried using tags such as <ViewFlipper with that users cannot scroll themselves within the layout.
Use a timer for continuous scrolling, and using canScrollHorizontally() to check wether it reached the furthest right then start scrolling left,
canScrollHorizontally() takes positive int for right direction, negative int for left direction
private HorizontalScrollView scrollView;
private boolean scrollingLeft;
int pace = 50;
private Timer timer;
timer = new Timer("horizontalScrollViewTimer");
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (scrollView.canScrollHorizontally(1)&&!scrollingLeft ) {
Log.d(TAG, "Scrolling right");
scrollView.smoothScrollBy(pace, 0);
}
else {
Log.d(TAG, "Scrolling left");
scrollingLeft =true;
scrollView.smoothScrollBy(-pace, 0);
if(!scrollView.canScrollHorizontally(-1)) // reached the furthest left turn of scrolling left
scrollingLeft = false;
}
}
});
}
}, 3000, 50);

Prevent Reload WebView onResume()

After lock my screen and unlock pressing the turn off button my webview is reloaded.
I have found that problem is with onResume()
#Override
protected void onResume() {
super.onResume();
mWebView.onResume();
mWebView.resumeTimers();
}
Reload is happen only with webview, as i can see navigation bar at the bottom is untouched. So how to prevent reloading webview also i will need onResume() for future use so removing is not solution.
Here is Activity layout
<androidx.constraintlayout.widget.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:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:windowTranslucentNavigation="false"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefresh"
android:layout_width="0dp"
android:layout_height="0dp"
android:overScrollMode="never"
android:soundEffectsEnabled="true"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#id/BottomLayout"
app:layout_constraintEnd_toEndOf="#id/BottomLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:scrollbars="none"
android:visibility="gone" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<include
android:id="#+id/netCon"
layout="#layout/network_connection" />
<include
android:id="#+id/srvCon"
layout="#layout/server_connection" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/BottomLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:animateLayoutChanges="true"
android:background="#drawable/border_top_bottom_menu"
android:clipToPadding="false"
android:duplicateParentState="false"
android:fitsSystemWindows="false"
android:focusableInTouchMode="false"
android:theme="#style/bottom_navigation_menu"
android:visibility="visible"
app:barrierAllowsGoneWidgets="true"
app:itemHorizontalTranslationEnabled="false"
app:itemIconSize="22dp"
app:itemIconTint="#color/drawer_item"
app:itemTextColor="#color/drawer_item"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toTopOf="#id/Notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/bottom_navigation_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<LinearLayout
android:id="#+id/Notification"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/notification_offline"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical"
android:weightSum="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/notificationText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="normal" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center|center_vertical"
android:indeterminate="false"
android:theme="#style/progressWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try saving the state of the webview and only loading it when savedstate is not null
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}

Unable to achieve animation effect in ViewAnimator when performing view switching

When performing view switching in ViewAnimator, I first thought I can easily achieve animation effect, by using
viewAnimator.setDisplayedChild(1);
or
viewAnimator.showNext();
The view is switched successfully. However, there are no animation being shown.
I expect there will be slide in/ slide out effect as I had setup the animation during initialization.
Animation slideInLeftFast = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_in_left_fast);
Animation slideOutRightSlow = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_out_right_slow);
this.viewAnimator.setInAnimation(slideInLeftFast);
this.viewAnimator.setOutAnimation(slideOutRightSlow);
slideInLeftFast.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
android.util.Log.i("CHEOK", "animation end");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
android.util.Log.i("CHEOK", "animation repeat");
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
android.util.Log.i("CHEOK", "animation start");
}
});
My complete layout file is as following. There are 2 different views in the ViewAnimator
trading_sign_in_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/view_animator"
android:animateFirstView="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/sign_in_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:layout_above="#+id/sign_in_bottom_nav_bar">
<android.support.design.widget.TextInputLayout
android:id="#+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="#+id/username_edit_text"
android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
app:passwordToggleEnabled="true"
android:id="#+id/password_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="#+id/password_edit_text"
android:inputType="textPassword"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/forgot_password_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="false"
android:text="Forgot password"
android:textSize="16sp"
android:layout_above="#+id/sign_in_bottom_nav_bar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:paddingLeft="32dp"
android:paddingRight="32dp" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/sign_in_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"
android:id="#+id/sign_in_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="Log in" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/forgot_password_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:layout_above="#+id/forgot_password_bottom_nav_bar">
<TextView
android:id="#+id/primary_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/forgot_password_username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="#+id/forgot_password_username_edit_text"
android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/forgot_password_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"
android:id="#+id/forgot_password_forgot_password_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="Forgot password" />
</LinearLayout>
</RelativeLayout>
</ViewAnimator>
May I know what's wrong with my code. Has I missed out something? The complete minimal code can be found at https://github.com/yccheok/MyProject
If you just specify initially displayed child after initialization of viewAnimator, the animation will kick in as expected.
viewAnimator = (ViewAnimator) v.findViewById(R.id.view_animator);
viewAnimator.setDisplayedChild(0);

Categories

Resources