Android crash on button.setEnabled(false) - java

Ok so I finally figured out the source of my problem. After a bunch of tests and trying out different things I noticed that after I put the codes
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
in onCreateView, it would crash upon startup. This is in my menu1_Fragment.java and is my first fragment. In my class I start off by defining my buttons.
Button button1;
Button button2;
Button button3;
Button button4;
Then the first thing I do in onCreateView is run the function setButton();
public void setButton() {
button1 = (Button) rootview.findViewById(R.id.upgbutton);
button2 = (Button) rootview.findViewById(R.id.upgbutton1);
button3 = (Button) rootview.findViewById(R.id.upgbutton2);
button4 = (Button) rootview.findViewById(R.id.upgbutton3);
}
What happens here is that these buttons are actually in my menu2_layout.xml which is my second fragment but my rootview allows me to find it. Then after I defined my buttons right below I set all my buttons to false as seen in the first bit of code because I don't want the user to be clicking the buttons yet. This is where I found that it crashes upon start. Here is the code for my buttons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state3_xml"
android:id="#+id/upgbutton1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state4_xml"
android:id="#+id/upgbutton2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state5_xml"
android:id="#+id/upgbutton3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I gave all my buttons their own selector_state because that is what initially I thought the problem was. Here is the code for my selector.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"></item>
<item android:drawable="#drawable/buttonclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/buttongray" android:state_enabled="false"></item>
</selector>
If you can please help I have been stuck on this issue for a really long time and made more posts then I need to, but I just can't seem to solve it. Also I cannot access my logs as I am having technical issues which basically freeze my android studio when I try to click android monitor and my app is running.
If all this isn't enough I also posted on github
https://github.com/BeniReydman/Slide_Menu-Slide_Menu
I will try to solve as much questions as you may have, but I am new and this is my first App. I haven't even been doing this for over a week so please bear with it.

What happens here is that these buttons are actually in my
menu2_layout.xml which is my second fragment but my rootview allows me
to find it.
I would usually say post the error but this has thrown me,
rootview is menu1_layou.xml, the buttons are in menu2_layout.xml.
So you need to call findViewById() on the View that contains the ID's you are looking for.
Yes the code will compile and your IDE will not throw any errors but that doesn't mean it will work.
If you are still having problems after you resolve this then please post the crash logs.

Related

Click arrow to reveal more content in application

i am trying to achieve a behavior where user click an arrow that can reveal more content such as more description abort something. It can a recycler view as well where more things can be added dynamically and the list will expand.Right now i do not have any idea how it can be achieved. I tried searching on the internet for solutions and saw a widget called spinner but i do not think it can help me achieve my desired behavior. YouTube does apply similar behavior as well
Below are the pictures which will make my question clear. Any help would be appreciated Thank You
Before clicking the arrow pic 1
After clicking the arrow pic 2
In your layout.xml include a nested layout that includes a Textview that holds the additional information and set android:visibility="gone". Use an OnClickListener to the button that is meant to expand the view. In the onClick method check if the view is visible or not. If it's not you make it visible, otherwise you set it to gone again.
layout:
...
<ImageView
android:id="#+id/chevron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/chevron"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your additional info here"
android:visibility="gone"/>
...
In your Activity:
ImageView yourView = findViewById(R.id.chevron);
..
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getVisibility() == View.Gone) {
view.setVisibility(View.Visible);
} else {
view.setVisibility(View.Gone);
}
}
});
I used for this purpose ExpandbleLayout from this github library
ExpandableLayout. In readMe of the github repo you can find example of using it, you can get similar experience with as in your example, without need to manually create View for arrow and handling the animation.
You can use it like this :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ael_expanded="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text goes here"
android:textSize="28sp" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
And in your java/kotlin code : do additional logic to expand/collapse call : expandableLayout.toggle();.
All the credit goes to the author of the library.
https://github.com/AAkira/ExpandableLayout

How add multiple TextView inside an ImageButton?

Is it possible to add multiple TextView inside one ImageButton with colour background ?
The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.
Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons :
How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.
Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?
I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.
TextView inside of ImageButton/ImageView XML - Android Dev
Obviously make sure each view has a unique id/name which you can assign as shown here on this link
Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.
I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.
an example of this would be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:background="#drawable/container_background"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
add a selector background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/text_pressed" />
<item android:color="#color/normal" />
</selector>
and the listener
findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Android - Button covering TextView in RelativeLayout only in version 5.0+

The target I need to achieve: 2 lines of text and 1 straight line in between should always be on top of Button. So in such a case, setting text in Button's attribute is not applicable according to my knowledge. Therefore I use TextView and View aligning on top of Button to achieve it.
Problem: A strange UI appearance I have come across of Android 5.0+ is that when the Button is enabled, it covers TextView and View even it comes before TextView in RelativeLayout xml file. (according to my knowledge, this means that TextView should on top of Button) The following is my xml code:
<RelativeLayout
android:id="#+id/rl_daily_check_in_button_container"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginBottom="4dp"
android:layout_centerHorizontal="true"
android:layout_above="#+id/textview_check_in_days_in_row">
<Button
android:id="#+id/button_daily_check_in"
android:layout_width="140dp"
android:layout_height="140dp"
android:background="#drawable/daily_checkin_button" />
<TextView
android:id="#+id/textview_check_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/view_line"
android:layout_marginBottom="4dp"
android:text="#string/title_not_yet_daily_check_in"
android:textColor="#color/text_gray"
android:textStyle="bold"
android:textSize="36sp" />
<View
android:id="#+id/view_line"
android:layout_width="94dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_above="#+id/textview_check_in_points"
android:layout_marginBottom="6dp"
android:background="#color/text_gray" />
<TextView
android:id="#+id/textview_check_in_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/points_can_get_daily_check_in_placeholder"
android:textColor="#color/text_gray"
android:textSize="14sp" />
</RelativeLayout>
But when the app is in Android 5.0-, say 4.4.2, the UI is exactly what I expect. I have tried to set android:elevation="xxdp" attribute, it success until I set the background of Button programatically. Can anyone explain the reason and how the Andoird 5.0+ render its UI? Many Thanks!
Button on Andoird 5.0+:
Button on Android 5.0-:
From Android Lollipop (5.0), the Button has a default StateListAnimator. It has set the default android:elevation and android:translationZ attributes to the Button. Since the Button has default elevation set to it, it appears above the other Views that doesn't have elevation set (Views that has elevation less than the Button, to be precise). i.e. The Views that has higher elevation set to it will appear above the Views that has lower elevation set.
So that's why, when you changed the Button to ImageView, the problem is solved, because ImageView has no default elevation set.

Android Studio: ToggleButton - How to start/awake with true instead of false?

-Using Android Studio
In my activity_main.xml,
android:checked="false"
would android:checked="" be the reason why my ToggleButton starts (or awakens) with false when my application compiles and runs? Changing to true seems to not work. My goal is to start with the ON button (or True) whenever my application boots instead of the OFF (or false) button but I have no idea how make this happen. Any ideas? Doesn't matter if it's through java or xml.
Also I am using,
android:background="#drawable/toggle_sound"
in order to import the images to the ToggleButton, which I successfully managed to do so.
In my toggle_sound.xml:
<item android:drawable="#drawable/sound_on" android:state_checked="true"></item>
<item android:drawable="#drawable/sound_off" android:state_checked="false"></item>
Additionally, this is my entire method of ToggleButton inside activity_main.xml:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toggleSound"
android:background="#drawable/toggle_sound"
android:textOff=""
android:textOn=""
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:checked="false" />
Again, any solution either through java or xml will be much appreciated!
If you're willing to use a Java solution, then it's easy!
In your onCreate method, just set it to false!
ToggleButton mToggle;
protected void onCreate(Bundle b) {
super.onCreate(b);
mToggle = (ToggleButton) findViewById(R.id.my_toggle_button);
mToggle.setChecked(false);
}
This work each and every time!
android:checked="true"
You can go with java too, as depicted by Alek K. But if you want your application to always start will true(togglebutton ON) then you can go with XML one.

Android- Cannot Cast from View to Button

Okay, I'm new to both java and android programming, but I've come across the most frustrating error ever and I can't seem to be able to find the solution. here is where the error is at.
final Button button = (Button) findViewById(R.id.activity_button);
And here is the XML of the button itself.
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
I'm not seeing the problem, I imported import com.awesometech.uselessbutton.R; which didn't help. Any suggestions? I'm using Eclipse.
Change to
final Button button = (Button) findViewById(R.id.button_main);
You have
<Button
android:id="#+id/button_main" // id is button_main not activity_button
Probably activity_button id belongs to another view and you are casting it to a Button.
In your main Activity, in the findViewbyId() method, you pass the ID of your Button which you declared in you xml.
So your button code is :-
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
here , id is button_main
so in you Class, you need to do
final Button button = (Button) findViewById(R.id.button_main);//Button ID goes here

Categories

Resources