I'm developing an app and use ViewPager inside it. It scrolls fragments, one of which contains the text input field. So the problem is the following: while the keyboard is showing I still can scroll fragments. have anybody met the same problem?
This is my Main Activity 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main"
tools:context=".activities.MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/navigation"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="#color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:menu="#menu/navigation" />
</RelativeLayout>
And this is the fragment code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.Login">
<ImageView
android:id="#+id/loginLogo"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#mipmap/logo_foreground"
tools:ignore="ContentDescription" />
<EditText
android:id="#+id/loginUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:layout_margin="10dp"
android:layout_below="#+id/loginLogo"
android:layout_centerHorizontal="true"/>
<EditText
android:id="#+id/loginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPassword"
android:layout_margin="10dp"
android:layout_below="#+id/loginUsername"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/loginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Войти" />
Well, the problem was solved the next way: I just created a little function to hide the keyboard
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
And then used the listener of ViewPager:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
hideKeyboard(activity);
}
#Override
public void onPageSelected(int position) {
if (prevMenuItem != null)
prevMenuItem.setChecked(false);
else
navigation.getMenu().getItem(0).setChecked(false);
navigation.getMenu().getItem(position).setChecked(true);
prevMenuItem = navigation.getMenu().getItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Related
I have an activity and framelayout container for replacing fragments inside the activity.Like below
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rootCo_Ordinator_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efefef"
android:orientation="vertical"
android:fitsSystemWindows="false"
tools:context=".DetailsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/scroll_view_horizontal" />
<HorizontalScrollView
android:id="#+id/scroll_view_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_above="#id/second_row">
<TextView
android:id="#+id/tv_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:padding="8dp"
android:scrollbars="horizontal"
android:singleLine="true"
android:textColor="#android:color/black"
android:textStyle="italic|bold"
tools:text="Sample Text" />
</HorizontalScrollView>
<LinearLayout
android:id="#+id/second_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000000"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:weightSum="5">
<Button
android:id="#+id/btn_content"
style="#style/DetailButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="#string/btn_desc"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_sub_cat"
style="#style/DetailButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="#string/btn_cat"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_text"
style="#style/DetailButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="#string/btn_cmntry"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_video"
style="#style/DetailButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/btn_videos"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_image"
style="#style/DetailButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="#string/btn_images"
android:textAllCaps="false" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<androidx.core.widget.ContentLoadingProgressBar
android:id="#+id/details_loading"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/transparent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And i have another fragment that dynamically creates youtubeplayerview ,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="false"
android:scrollbars="vertical">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:fitsSystemWindows="false"
tools:context=".fragments.IntroFragment">
<include
layout="#layout/empty_view"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
I have FullScreen Util class like below
public class FullScreenHelper {
private Activity context;
private View[] views;
public FullScreenHelper(Activity context, View ... views) {
this.context = context;
this.views = views;
}
public void enterFullScreen(View selectedView, View[] allViews) {
View decorView = context.getWindow().getDecorView();
hideSystemUi(decorView);
for(View view : views) {
if (!(view instanceof FrameLayout) && (view != selectedView) ) {
view.setVisibility(View.GONE);
view.invalidate();
}
}
for (View view:allViews){
if (view != null && (view != selectedView) ){
view.setVisibility(View.GONE);
view.invalidate();
}
}
}
public void exitFullScreen(View selectedView, View[] allViews) {
View decorView = context.getWindow().getDecorView();
showSystemUi(decorView);
for(View view : views) {
view.setVisibility(View.VISIBLE);
view.invalidate();
}
for (View view:allViews){
if (view != null && (view != selectedView) ){
view.setVisibility(View.VISIBLE);
view.invalidate();
}
}
}
private void hideSystemUi(View mDecorView) {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
private void showSystemUi(View mDecorView) {
mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
And i capture the toggle and make the changes in the Activity as such below,
FullScreenHelper fsHelper =
new FullScreenHelper(this,mAppBar,mPathTextView,mHorizontalScrollView,mSecondRow);
#Override
public void toLandScape(View selectedView, View... allViews) {
isLandscape = true;
this.selectedView = selectedView;
this.allViews = new View[allViews.length];
this.allViews = allViews;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getSupportActionBar().hide();
fsHelper.enterFullScreen(selectedView,allViews);
}
#Override
public void toPortrait(View selectedView, View... allViews) {
isLandscape = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getSupportActionBar().show();
fsHelper.exitFullScreen(selectedView,allViews);
}
There can be many youtubeplayerviews created dynamically in the fragment
I have added the respective listener when toggle screen is pressed.
When user presses the toggle in the current view , the youtube-player should fill the screen and should hide all other views,
and on pressing the toggle again should bring back the hidden views.
But i am not able to achieve this .
Currently all the views are getting hidden on landscape orientation changes,and show up all views on portrait.
Any help in how i go about doing this would be highly appreciated.!!!
go to any youtube video
https://www.youtube.com/watch?v=m8ZoxzX0hro #this is how the url looks
now in order to make it full screen and use it in your app you have to make a change in url saying
https://www.youtube.com/embed?v=m8ZoxzX0hro
watch has been replaced with embed
I'm trying to create a simple Bottom Sheet layout, One button shows and other collapse, but its not working. I was following this tutorial but still nothing.So I'm trying to pass the state inside of buttons and then pass into the callback method to hide or expand, and the bottom sheet can't be scrolled down!
This is the layout.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_botton_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:padding="16dp"
android:layout_margin="8dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_green_dark"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_margin="8dp"
android:text="Button 2"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_light"/>
<Button
android:id="#+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_margin="8dp"
android:text="Button 3"
android:textColor="#android:color/white"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
</ScrollView>
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:clipToPadding="true"
android:background="#android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="opaa"
android:padding="16dp"
android:textSize="16sp"/>
</android.support.v4.widget.NestedScrollView>
Activity :
private View bottomSheet;
private Button button1,button2,button3;
private BottomSheetBehavior mBottomSheetBehavior;
bottomSheet = findViewById( R.id.bottom_sheet );
button1 = (Button)findViewById(R.id.button_1);
button2 = (Button)findViewById(R.id.button_2);
button3 = (Button)findViewById(R.id.button_3);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
mBottomSheetBehavior.setPeekHeight(0);
}
if(newState == BottomSheetBehavior.STATE_EXPANDED){
mBottomSheetBehavior.setPeekHeight(500);
}
}
#Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
I ran into the same issue when trying that tutorial myself.
You have to set the BottomSheetBehavior to be hideable. Moreover, the COLLAPSED state may not be what you are looking for. To make the BottomSheet disappear completely, you have to set the state to HIDDEN. Like this:
mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Put TextView into LinearLayout
my current Zxing scanner screen looks like this,
I want to add a textview and couple of buttons on it, I had tried many procedures but nothing worked can anyone guide me, I will be humbly looking forward for your kind help
here's the Code
Java file:
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_qrresult);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
Toast.makeText(getApplicationContext(),"Scan QR code to Send
Money",Toast.LENGTH_LONG).show();
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
if(rawResult.getText().toString().equals("Test Ewallet Test")){
Toast.makeText(getApplicationContext(), rawResult.getText(),
Toast.LENGTH_SHORT).show();
Intent go = new Intent(qrresult.this,sendmoney.class);
startActivity(go);
}else {
Toast.makeText(getApplicationContext(), "Couldn't scan the
QRcode, Please Try again ", Toast.LENGTH_SHORT).show();
mScannerView.resumeCameraPreview(this);
}
// mScannerView.resumeCameraPreview(this);
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
}
heres xml file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tajveezrehman.applicationtest.yourqr">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/flashlight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="Button" />
<Button
android:id="#+id/open image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="Button" />
<TextView
android:id="#+id/camtxt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_weight="0.8"
android:text="#string/scanner"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
use RelativeLayout for it like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/zxscan"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView
android:id="#+id/btn_flash"
android:layout_marginBottom="15dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:src="#drawable/flash_100"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
its look like :
I want the code in the Java file to set the background color of whatever button was clicked, and keep it, even after you let go of the button. Thanks in advance
I don't really get the formatting on here. I hope it's clear what belongs where.
Java:
package mika.actual;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordian);
Button btnProfile = (Button) findViewById(R.id.btnProfile);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
If I understood correctly, what you want to do is, when clicking a button and expanding its sub-menu, change the background of that button until it is clicked again. You can do this with a Tag on the Button:
btnSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
//if button is not selected, change background and tag the View as selected
if(v.getTag() == null || v.getTag().equals("not_selected")) {
v.setBackgroundColor(Color.RED);
v.setTag("selected");
//show sub-menu
}
//if button is already selected, reset background and tag the View as not selected
else{
v.setBackgroundResource(android.R.drawable.btn_default);
v.setTag("not_selected");
//hide sub menu
}
}
});
Notice that v.setBackgroundResource(android.R.drawable.btn_default); is resetting the Button background to an android drawable. You may want to change this to a custom drawable or color.
You could have used an ExpandableListView to implement this kind of menu.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color));
}
return false;
}
});
I am trying to get swipe up/down gesture. Question is not about how to implement swipe up/down catch. I can not catch onTouch call.
Inside of Fragment layout, there is LinearLayout for which has been set onTouchListener. But somehow it is not working - can not get onTouch call.
This is that LinearLayout:
<LinearLayout
android:id="#+id/llContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_back"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/btn_forward"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent" />
</LinearLayout>
This is how Component tree for Fragment layout:
Inside Fragment's onCreateView, I am setting onTouchListener to that LinearLayout:
LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent);
llContent.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyLog("Touch");
return true;
}
});
I thought, this is because of child button and clickListeners. So I disabled them:
// view.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// mCurrentIndex = (mCurrentIndex > 0) ? mCurrentIndex - 1 : 0;
// update();
// }
// });
// view.findViewById(R.id.btn_forward).setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// mCurrentIndex = (mCurrentIndex < mPagination.size() - 1) ? mCurrentIndex + 1 : mPagination.size() - 1;
// update();
// }
// });
On StackOverflow, there are a lot of questions regarding this and there are solutions.
Try 1:
This answer says that I should return true from onTouch method. My onTouch method already returns true.
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyLog("Touch");
return true;
}
Try 2:
This answer says that I should override onTouchListener's onTouchEvent method. But when I implemented onTouchListener interface, I could not override onTouchEvent. Android Studio's intellisense could not find. When I written it manually, Android Studio shown onTouchEvent method as error.
Error: Method does not override method from its superclass. Same answer is here too.
Question: How to get onTouch call for child LinearLayout?
This is XML for whole fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FEFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="41dp"
android:background="#EFF0F1"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="57dp"
android:paddingRight="57dp">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2E2F30"
android:textSize="17sp" />
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#5E5F60"
android:textSize="17dp" />
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="8dp">
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="#1C1D1D"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#050505" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444546"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/llContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_back"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/btn_forward"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Try this:
LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent);
llContent.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch");
return true;
}
});
Button ff = (Button)view.findViewById(R.id.btn_forward);
ff.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.i(TAG, "onTouch");
return false;
}
});
ff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG, "onClick");
}
});
The btn_forward is above llContent, therefore you need to return false in onTouch() (btn_forward). Then the event will be called from bottom control (llContent).
Set android:clickable="false" and android:focusable="false" on your buttons.