I'm working on a fragment that contains three toggle buttons at the moment. In the fragments layout I have:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/corkRdToggle"
android:layout_below="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_marginRight="15dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/collegeStToggle"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/allRoomsToggle"
android:layout_below="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:layout_marginLeft="15dp"
android:textSize="12sp" />
In my fragments onViewCreated() method I'm finding the toggle buttons by id, setting both the 'on' and 'off' text, and setting one button to be active:
corkRdToggle = (ToggleButton) view.findViewById(R.id.corkRdToggle);
collegeStToggle = (ToggleButton) view.findViewById(R.id.collegeStToggle);
allRoomsToggle = (ToggleButton) view.findViewById(R.id.allRoomsToggle);
corkRdToggle.setTextOn("Cork Rd.");
collegeStToggle.setTextOn("College St.");
allRoomsToggle.setTextOn("All Rooms");
collegeStToggle.setTextOff("College St.");
allRoomsToggle.setTextOff("All Rooms");
corkRdToggle.setTextOff("Cork Rd.");
allRoomsToggle.setChecked(true);
And attaching listeners to them:
corkRdToggle.setOnClickListener(this);
collegeStToggle.setOnClickListener(this);
allRoomsToggle.setOnClickListener(this);
And in my onClick():
case R.id.corkRdToggle:
if(allRoomsToggle.isChecked() || collegeStToggle.isChecked()){
allRoomsToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
corkRdToggle.setChecked(true);
break;
case R.id.collegeStToggle:
if(allRoomsToggle.isChecked() || corkRdToggle.isChecked()){
allRoomsToggle.setChecked(false);
corkRdToggle.setChecked(false);
}
collegeStToggle.setChecked(true);
break;
case R.id.allRoomsToggle:
if(corkRdToggle.isChecked() || collegeStToggle.isChecked()){
corkRdToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
allRoomsToggle.setChecked(true);
break;
I have the three toggle buttons, they're linked to the buttons in the layout, the text has been set for both on and off states, and the 'All Rooms' button has been set to be active by default when the screen is created. Whichever button is pressed will become active, while deactivating both others. Everything is working fine.....except for the initial state. When the screen is created the 'All Rooms' button is indeed activated, and shows the text, but the other two toggle buttons only show 'OFF':
When screen is created
Pressing either of them will deactivate the 'All Rooms' button and the proper text will display in all buttons:
Proper text showing
This remains the case for as long as the screen is being used (due to my rep I can only post 2 links, take my word that it's working).
However the problem is back when I recreate the screen. Done a good bit of looking into this but there doesn't seem to be any info relating to this specifically, and my novice skills are exhausted. If anything jumps out at you I'd really appreciate the pointer, thanks!
Rather than setOnClickListener(), try setOnCheckChangeListener()
corkRdToggle.setOnCheckChangeListener(this);
collegeStToggle.setOnCheckChangeListener(this);
allRoomsToggle.setOnCheckChangeListener(this);
I tend to only use onClickListener for toggles when i want to specifically control if the toggle should indeed be toggled on click (eg. i want to show a warning first or something)
You need to set setChecked(false) to the other buttons to set the initial state OFF text.
Related
I am attempting to make a textview clickable by adding an email autolink attribute to it with android:autoLink="email"but as soon as I add it, the text just disappears. What's interesting is that other combinations of text appear just fine. When typing in an email, as soon as I type the "o" in sample#gmail.com, the text just disappears. Am I doing something wrong?
My TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:text="sample#gmail.com"
android:clickable="true"
android:autoLink="email"
android:textColor="#54ACE6"
android:textSize="13dp" />
Try to set android:textColorLink="#color/blue" or any other color different than your background.
I'm trying to create a bottomsheet that's either completely expanded or completely out of view - I don't want it to be anywhere in the middle or peeking.
Here's the xml:
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_bright"
android:clipToPadding="true"
android:orientation="vertical"
android:elevation="10dp"
app:behavior_peekHeight="0dp"
app:behavior_hideable="true"
app:behavior_skipCollapsed="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/cinza3"
android:text="clear/delete Q"/>
</LinearLayout>
In my code I have the following methods:
private void showHideBottomSheet() {
if (mBSBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
showBottomSheet();
} else {
hideBottomSheet();
}
}
private void showBottomSheet() {
mBSBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private void hideBottomSheet() {
mBSBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
And in my layout there's a button that calls showHideBottomSheet() when clicked.
When I click the button, everything works fine and the bottomsheet is shown/hidden. But if it's EXPANDED and I click on a textview elsewhere in the code (outside the bottomsheet), for example, the bottomsheet moves down a little, but not completely - it's top half is visible, but if I log it's state, it's STATE_EXPANDED.
what's the difference between STATE_HIDDEN and STATE_COLLAPSED? I've searched everywhere for a visual explanation but couldn't find it. Is this 'intermediate' state the collapsed state? Even if I set peekHeight="0" in the xml and skipCollapsed="true"?
what does peekHeight and skipCollapsed in the xml actually do?
how can I make it to be fully visible or fully hidden at all times and avoid this 'intermediate' state?
EDIT: There's a TextView inside the BottomSheet, and and OnClickListener on it. When I click it, the BottomSheet goes to that 'intermediate' state too, even though the OnclickListener does not call setState or anything related to the BottomSheet.
Updated my support:design library to 25.3.1 and it started working as expected.
Background
Development Tool: Android Studio 2.1.3
Device: Android OS Ver. 4.4.2
I have an Activity with multiple Views. I wanted to focus a certain view (editText1 in this case) programatically based on user's previous actions. So I employed View.requestFocus() for this. Before this, I have set focusable and focusableInThouchMode of editText1 to true XML design file:
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".15"
android:inputType="numberDecimal"
android:maxLength="3"
android:text="1"
android:focusableInTouchMode="true"
android:focusable="true"
android:enabled="true" />
Ideally the scenario would be: If user has checked a certain myCheckBox before current action, move focus to dditText1, if else, return.
if(myCheckBox.isChecked()){
editText1.selectAll();
if(adet.requestFocusFromTouch()) {
Log.i(General.LOG_TAG, "editText1 has focus");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT);
}
return;
}
But In reality, some other View snatches focus instantly from editText1. I can even see that editText1 got focus and have selected a text inside it for an instant. Also, I can see in log that editText1 got focus.
My activity contains a LinearLayout and all other Views(CheckBox, EditText, ListView etc.) are inside it. Also I have set focusable and focusableInThouchMode to false for other views than editText1
Question
How can I prevent other views than my editText from getting/snatching focus in my scenario?
Is there an alternative approach for what I am trying to do here?
You could try to set android:focusable="false" for the components that should not get focused. Not sure I got your question right.
I am working on an application . It uses a main layout..
In it I use a relative layout and i have 4 buttons .
Button 1 , Button 2, Button 3 and Button 4
So based on user credentials and capabilities, i hide some buttons. e.g
Admin can view all the buttons
Button 2 and 3 is hidden for user account .So when user login, i show only Button 1 and Button 4.
But i want to rearrange the UI look.
Currently, when Button 2 and 3 are hidden , there is a big space between button 1 and 4 , when viewing on the screen.
When Button 2 and 3 are hidden, i want to move Button 1 at the place of Button 3 (nearer to 4 on the UI ) . so that it looks good on viewing the screen.
I dont know how to achieve this. Please let me know how to rearrange the buttons in the layout when one button is hidden
<RelativeLayout
android:id="#+id/Info_RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip" >
<Button
android:id="#+id/Camera_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/division_LinearLayout"
android:background="#drawable/camera_selector" />
<Button
android:id="#+id/Favorite_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/GotoCamera_Button"
android:background="#drawable/favorite_btn_selector" />
<Button
android:id="#+id/Profile_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/Favorite_Button"
android:background="#drawable/profile_list_btn_selector" />
<Button
android:id="#+id/Info_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ProfileList_Button"
android:background="#drawable/camerainfo_selector" />
</RelativeLayout>
Try using setVisibility(View.GONE) on your buttons instead of just making them invisible. When they're gone, they don't take up space for layout purposes, unlike when they are just INVISIBLE.
Try remove the buttons like this :
ViewGroup layout = (ViewGroup) btnToRemove.getParent();
if(null!=layout) layout.removeView(btnToRemove);
I have an app that I have almost finished which is a scoreboard app. When the user does a LongClick on the score, currently a dialog box appears which shows an EditText field with an OK and CANCEL button. When the user clicks the EditText field a keypad appears (numbers only) in which the user types in the new score, clicks DONE and then the EditText field shows the new value in the dialog box and when the user clicks OK it saves the entered value to the TextView showing the current score.
The problem is that I feel it is cumbersome. Here is what I would like to happen:
User does a LongClick on score
Keypad appears
User enters new score
User clicks OK
Keypad disappears and value is saved to the TextView updating the current score.
In order to accomplish this, I thought of two things:
I could create a new class extending View -- I'm too new to android to really understand this option.
I could replace the EditText in the dialog with a new XML file which is designed like a keypad.
Option 2: I figured this would probably be the easiest so I have designed the XML Layout and called it keypad.xml. Now when the user does a LongClick the keypad.xml file inflates in a dialog with OK and CANCEL. So far so good. However, I don't know where I should put the code for the button actions.
At first thought, I figured that I could setup some onClick listener which is for ALL BUTTONS in the layout. I figured that all buttons are going to perform the exact same function. Basically, get the text from the button (in this case 0-9) and append it to the TextView which is just above the keypad. Then when the user clicks OK save the TextView in keypad.xml to the current score textview.
Can anyone help point me in the right direction to accomplish this please?
Here is the screenshot of keypad.xml (there is an empty TextView above the numbers):
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:columnCount="3" >
<TextView
android:id="#+id/textView1"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:gravity="right|center_vertical"
android:textSize="24sp" />
<Button android:text="1" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:gravity="center"
android:text="0" />
This Question has not seemed to prompt a response. In case anyone is looking for an answer to this, I have come up with another solution which isn't exactly the same but works none the less.
Please see this question.