Android bottom sheet does not collapse - java

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

Related

How to disable scrolling ViewPager while keyboard is shown?

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) {
}
});

Scroll entire page when particular EditText has focus?

I have registration page,in that four EditText exists,when I click on third EditText one LinearLayout(assume password guidelines) has to be visible and at the same time total layout should be scroll to view all guidelines.When other EditText (1,2 or 4) got focus this LinearLayout(password guidelines) should be closed. I tried lot and I searched lot , no solution worked for me,please help me. For understand I did small project ,please watch it.
public class MainActivity extends AppCompatActivity {
private EditText mEditText1,mEditText2,mEditText3,mEditText4;
private LinearLayout ll,root;
ScrollView mScrollView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout) findViewById(R.id.ll_linear_layout);
ll = (LinearLayout) findViewById(R.id.ll);
mEditText1 = (EditText) findViewById(R.id.et1);
mEditText2 = (EditText) findViewById(R.id.et2);
mEditText3 = (EditText) findViewById(R.id.et3);
mEditText4 = (EditText) findViewById(R.id.et4);
mEditText1.setOnFocusChangeListener(mListener1);
mEditText2.setOnFocusChangeListener(mListener2);
mEditText3.setOnFocusChangeListener(mLayoutExtract);
mEditText4.setOnFocusChangeListener(mListener4);
mScrollView = new ScrollView(this);
}
View.OnFocusChangeListener mListener1 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mListener2 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mLayoutExtract = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
ll.setVisibility(View.VISIBLE);
} else {
ll.setVisibility(View.GONE);
}
}
};
View.OnFocusChangeListener mListener4 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
}};
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
Try this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
</LinearLayout>
</ScrollView>
Try this use android:windowSoftInputMode="AdjustResize" in Your Manifest where your are decraing YOur Activity like below code
<activity
android:name=".YourActivity"
android:parentActivityName="XXX.XXX.XXXX"
android:windowSoftInputMode="AdjustResize" />

Android: How to display more views after clicking a button

SOLVED THE CODE BELOW WORKS
If I have a layout similar to the one below:
Those five temp EditTexts represent some info the user can enter (like the price of an item, the order number, etc.) If the user wants to add another item they would click on the Add button and I want another 5 textviews to appear on the screen right above the two buttons but right below the previous set of the 5 EditTexts. Can someone give me a starting point on how I would do this.
My layout of the fragment goes like this:
I have a top level linear layout (Vertical Orientation).
Then a scrollview.
Inside the scrollview I have another linear layout.
In that linear layout I have those five EditText objects and the two buttons
The view above is a fragment (defined in the file below) which I pass to my FragmentAdapter in my MainActivity file:
public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}
Here is my layout file for this fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />
<Button
android:id="#+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
edit_text_view_objects.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>
</LinearLayout>
in your XML take
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:id="#+id/layoutDynamic"a
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
and create one more XML for your Item and design accordingly (It will add dynamically when you click on ADD)
dynamic_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<TextView
android:id="#+id/etDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="Color"
android:layout_marginTop="10dp"/>
</LinearLayout>
so come to java Code
// take the reference of LinearLayout
linearLayout = (LinearLayout) romptView.findViewById(R.id.layoutDynamic);
// Take the reference of Add button
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View addView = layoutInflater1.inflate(R.layout.dynamic_row, null);
final TextView textView1 = (TextView) addView.findViewById(R.id.etDynamic);
textView1.setText(otheret.getText().toString().trim()); otheret.setText(""); linearLayout.addView(addView);
}
});
#and if you want to remove particular item
removeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addView.getParent()).removeView(addView);
}
});
You can add these EditText through code at runtime
we can solve this, using List/Recycler view efficiently/easily
when ever you clicking on ADD add the item to list and notify the Recycler view adapter
and and removing also easy you just delete the item from list based on recycler View position.
No need to create Dynamic View here recycler view will do the job
// In Main Layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView_Stops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layoutSaveBottom"
android:layout_below="#+id/ll1"
android:layout_marginTop="#dimen/pad_10dp"
app:layout_behavior="#string/bottom_sheet_behavior" />
<TextView
android:id="#+id/tvAddOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="#string/icon_plus"
android:textColor="#color/colorPrimary"
android:textSize="40sp"
app:customFontPath="#string/roboto_regular" />
// design your layout item
---------------------
and when clicking on Add set user entered details
addOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RouteElements routeElements = new RouteElements();
routeElements.setLatitude(latitude);
routeElements.setLongitude(longitude);
routeElements.setStopName(otheret.getText().toString().trim());
routeElementsArrayList.add(routeElements);
routeStopsAdapter.notifyDataSetChanged();
});
And in Your Adapter for removing the item
public void remove(int position) {
if (position < routeElementsArrayList.size()) {
routeElementsArrayList.remove(position);
notifyDataSetChanged();
}
}

Android background color for accordion menu

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;
}
});

Android - Problem with ViewFlipper

I am making a game that requires multiple views within an Activity, and i decided to use Viewflipper to do.
The thing is. I need to have 3 views in in the viewflipper, and the LAST view will transfer back to the first one.
My problem is that the buttons are acting weird, and they are either not going to the next, or skipping the third view. I tried to put vf.setDisplayedChild(R.Id.gamescreen1); at the last view but then the whole thing breaks down.
Thanks for all answers in advance, i have been stuck with this problem for DAYS! and yes, i know that i am a noob :(
[SOURCECODE]
public class GameActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_game);
final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
//SCREEN 1
Button btnSTART = (Button) findViewById(R.id.btnSTART);
//SCREEN 2
Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);
//SCREEN 3
Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);
//screen 1
btnSTART.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 2 // Either button will go to view 3
btnALT1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 3 // Either button will go back to view 1
btnALT3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
}
}
[XML]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
<ListView android:layout_width="fill_parent" android:id="#+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="#+id/btnSTART" android:layout_width="200dp" android:text="#string/btnstart"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/list2"></ListView>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT1"></Button>
<Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT2"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT3"></Button>
<Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT4"></Button>
</LinearLayout>
</LinearLayout>
try this
flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);
if(flipper.getCurrentView() == 3)
{
flipper.setDisplayedChild(1);
}
setDisplayChild takes an integer parameter that is the zero based index of the child to display NOT the id of the child to display. A bit confusing I know.

Categories