reseting color of button in kotlin - android-studio

I am new in android studio. I am making Tic Tac toe and when I am clicking on button, it's changes its background color and text.
clickedView.text = "x"
clickedView.setBackgroundColor(Color.CYAN)
this is button in .XML file:
<Button
android:id="#+id/button1"
android:layout_height="45pt"
android:layout_width="45pt"
android:background="#color/colorPrimary"
android:layout_margin="2dp"
android:textSize="45sp"
android:textColor="#color/white"
/>
so, now i am making reset button and i dont know how to change button's background color on default. can you help me with that?

You can get the color from hex code
yourView.setBackgroundColor(Color.parseColor("#000000"));
Or get it from the resources
yourView.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
Or from default colors
yourView.setBackgroundColor(getResources().getColor(R.color.black));

Related

The color does not show correct in the design view of android studio

The background color in design view of android studio is not correct
This is my color.xml
The color of the button "=" is Orange and I do not set any color to the rest of buttons, but all button show purple in Design view
Did you change the theme? You can make your own theme (just a suggestion).
Or use AppCompatButton, example:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/yourButton"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_marginTop="15dp"
android:background="#drawable/rounded_corners"
android:backgroundTint="#color/white"
android:contextClickable="false"
android:linksClickable="false"
android:text="Your text"
android:textStyle="bold"
android:textColor="#color/black"
app:layout_constraintCircleRadius="20dp"
app:rippleColor="#FFFFFF" />

How to make Horizontal Scrollable Text View with Automated Scrolling?

I am a just a beginner to Android Studio and have started as a hobby, and to learn it's basics I have tried to make a simple calculator in Android Studio(Kotlin).
The problem is that if the answer is to long to be diplayed in the text view box it cuts out...So to solve this problem I made the text view Horizontally Scrollable using "Horizontal Scroll View" in the xml File,
But now the problem is when the user types the text after the view is full, it does not automatically scroll to the last digit he has typed, rather he/she has to manually scroll it to the end. Here is the code for the text box which displays the result:
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:id="#+id/tvExpression"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1C1C1C"
android:textColor="#FFFFFF"
android:textSize="27dp"
</HorizontalScrollView>`
Could you please tell a way so that each time a new value is added to the box, it automatically scrolls to the right most part of the box, so that the users can see his/her last inputted value?
Please Help me!
Thanks,
Shaurya
Preferably delete a tag of HorizontalScrollView Because this makes textview More flexible to contain the result of the calculator.
You can also select the max line of the textView by using this attribute:
android:maxLines="select int number"
If you want a way to make the scroll of horizontalScrollView from the end
you can use this code in your activity that contents the horizontalScrollView:
horizontalScrollView.postDelayed(
Runnable {
horizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) },
100L
)

Android Studio Changing Button Background Color

I have started learning Android Studio and I am trying to change my button's background, but it is just not working.
Here is my code:
<Button
android:layout_width="300dp"
android:layout_height="200dp"
android:text="salam"
android:textSize="30sp"
android:textColor="#ffccee"
android:background="#ffffff"
/>
The background color stays the same after I change it.
So actually for some reason now Button only follows the colour which is in the theme of your activity but instead of going and messing with theme use AppCompatButton
<androidx.appcompat.widget.AppCompatButton
android:layout_width="300dp"
android:layout_height="200dp"
android:text="salam"
android:textSize="30sp"
android:textColor="#ffccee"
android:background="#ffffff">
</androidx.appcompat.widget.AppCompatButton>
Keep in mind even in future I would recommend using AppCompatButton.

Why Is My UI Not Showing When I Drop Items Into It From The Editor?

So I just downloaded Android Studio onto this compute. When the Layout Editor interface loads and I try to drag the XML items onto the screen, they do show up in the constraintView but not in the editor.
I get a render error that states "Couldn't resolve resource #string/helegrtsrewfrtsaw"
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="132dp"
android:layout_marginStart="132dp"
android:text="#string/helegrtsrewfrtsaw"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Because you are using ConstraintLayout as parent layout, you need to to remove android:layout_width and android_layout:height then define constraints of your Switch in the Attributes tab on the right hand side.
It will allow system to arrange your View width/height and position base on parent layout (instead of using specific predefined values).
P.s: If you click on the exclamation mark !, it will tell you to add the constraints or the View will jump to position (0,0).

How I do to roll the text to the bottom?

I'm programming on ADK and I got one screen with a text at a huge font so the whole text can't appear on the screen. I want to know to do slide the finger up and see the text below, sliding it as we do with normal texts on Android.
Put your TextView inside ScrollView in XML like:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="txt2"/>
</ScrollView>
OR If You don't need to use a ScrollView actually.
Just set the
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
properties of your TextView in your layout's xml file.
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
in your code.
It scrolls automatically without any issues.
Create a custom listview! Like so : http://www.vogella.com/articles/AndroidListView/article.html#androidlists_overview

Resources