-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.
Related
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) {
}
});
I made an Activity in Android Studio and added a button in there, added the constraints so the error would go away. When I run an App from Android Studio to my phone, it works fine. When I use an AAR file in Unity and call the activity from there, the buttons jump back to 0,0 like the error said if I did not add constraints, which I did. I'm not getting any errors either as to why it's not able to constraint the button.
Here is how I made the button in my activity.
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginBottom="431dp"
android:layout_marginEnd="130dp"
android:layout_marginStart="168dp"
android:layout_marginTop="32dp"
android:text="#string/StringName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I've never really worked with Android Studio UI until now, so I might have forgotten something important here, just hoping one of you guys knows what could be the problem, thanks in advance.
EDIT
The problem is with "app:", everywhere you use it should be replaced with "android:". However android:layout_constraintBottom_toEndOf="parent" doesn't work. Instead you have to use: android:layout_alignParentEnd="true". Also replace "android.support.constraint.ConstraintLayout" with "RelativeLayout".
Unity doesn't work with "app:" for some reason so replacing with "android:" is always necessary. Thanks Soon Santos for telling me about RelativeLayout, I looked at this: https://developer.android.com/guide/topics/ui/layout/relative.html and remembered I had gotten an app: problem before and fixed by changing to android:.
activity_main.xml
<?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.Company.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="25dp"
android:text="#string/StringName"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false"/>
</RelativeLayout>
You can actually leave out android:layout_alignParentEnd/Start. With RelativeLayout it seems to not jump back to 0,0 at all, so basically always use android: and RelativeLayout when using Unity (it seems like that at least).
Nothing wrong with the code, it works for me too. Maybe it is because your other way to run the code does not support constraintLayout, you can try RelativeLayout instead and see if it'll work.
I'm working on an Android App which has a few (Next, Back, ... etc) Buttons which are all round and transparent and I use ImageButton for those Buttons, however the problem is there is always a white border around the Button (I use black background so it is very ugly) and the button never appears to be round is appears as some sort of a square-ish shape.
Here is what I tried so far :
Setting the background of the ImageButton in the activity_mypage.xml file to
android:background="#android:color/transparent"
and to
android:background="?android:selectableItemBackground"
and to
android:background="?android:selectableItemBackgroundBorderless"
even to
android:background="#null"
but nothing seems to work on the .xml side.
and I tried the following on the MyPage.java file :
myBtn.setBackgroundResource(0);
and also
myBtn.setBackground(null);
Nothing seems to work anything I do keeps resulting the same (although it removes a gray border from around the Button but none of them makes the Button completely transparent)
This is a screenshot of the button before applying any of above :
And this is a screenshot of the same Button after applying any of those attributes (doesn't really matter if I applied it only to the xml or only to the java file or to both because all results are the same):
This is my xml code for one of my Buttons in xml :
<ImageButton
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#mipmap/my_btn"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1"
android:background="#android:color/transparent"
/>
And here is the code for it in the .java file :
ImageButton myBtn= (ImageButton) findViewById(R.id.my_btn);
myBtn.setBackground(null);
myBtn.setBackgroundResource(0);
I'm really lost at this every result I found for this query has suggested one of the above methods but none of these seems to work for me..
Just change
android:background="#android:color/transparent"
to
android:background="#null"
Try with this
android:src="#mipmap/yourimage"
instead of
app:srcCompat="#mipmap/my_btn"
add this to your button
android:background="#drawable/template"
and create a file name it template in your drawable folder
and add this code to it
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/btn2imagePressed"></item>
<item android:drawable="#drawable/btn1imageNotPressed"></item> </selector>
You can use ImageView instead of ImageButton, so background issue will not occur
<ImageView
android:id="#+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:src="#mipmap/my_btn"
/>
So after reading some of the posted answers and playing around with the suggestions in my code I noticed that the icon was incorrectly loaded into my mipmap directory instead of drawables and that is because when I imported the icons I selected New -> Image Asset instead of Vector Asset which caused my 'icon' to lose its transparency ...
Thanks to everyone for their replies and answers.
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.
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.