This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Closed 5 years ago.
I want to know if I have a text like "hello world".
How can I set the preference of this text view that user can change the font size to medium, large and small like 20sp, 25sp, 30sp.
If you have an activity, which layout have a
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="20sp"
/>
so, you need an user input like buttons and in the activity you can changue the size of the textview element detecting an event relate to the user input, in this example the "onclick" method.
here is the code to archieve that.
layout:
<?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">
<TextView
android:id="#+id/txt_hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HelloWorld"
android:textColor="#android:color/black"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txt_hello_world"
android:orientation="horizontal"
>
<Button
android:id="#+id/btn_20sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="20sp"
/>
<Button
android:id="#+id/btn_30sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="30sp"
/>
<Button
android:id="#+id/btn_40sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="40sp"
/>
</LinearLayout>
Activity
class MainActivity : AppCompatActivity(), View.OnClickListener {
lateinit var helloText : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
helloText = findViewById(R.id.txt_hello_world)
var btn20sp: Button = findViewById(R.id.btn_20sp)
btn20sp.setOnClickListener(this)
var btn30sp: Button = findViewById(R.id.btn_30sp)
btn30sp.setOnClickListener(this)
var btn40sp: Button = findViewById(R.id.btn_40sp)
btn40sp.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id){
R.id.btn_20sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,20f)
R.id.btn_30sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,30f)
R.id.btn_40sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,40f)
}
}
}
The code is in kotlin but the logic is the samen in java
Related
I have a bottomsheet implemented in my app. I used inflater to show the bottomsheet dialog, here is my code in NewSignUpActivity to show the bottomsheet dialog.
class NewSignUpActivity : AppCompatActivity() {
private lateinit var btnBottomSheet: RelativeLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_rider_or_bike)
btnBottomSheet = findViewById(R.id.riderBtn)
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback(){
override fun onStateChanged(bottomSheet: View, newState: Int) {}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
}
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(bottomSheetView)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback)
btnBottomSheet.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.show()
}
}
}
Here's how it looks like in view.
As you can see, there's button on the bottomsheet I want to navigate to another layout activity once I clicked the YES button, how can I do that? I am very new to android development using Kotlin.
EDITED: Added my XML file for bottomsheet dialog please see below._ name of file is layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutBtmSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/rectangle"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_15sdp"
android:layout_marginBottom="#dimen/_30sdp"
android:src="#drawable/close_black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:background="#drawable/bgwhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/ll_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/outline_sports_motorsports_black_48"
tools:layout_editor_absoluteX="3dp"
tools:layout_editor_absoluteY="3dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#+id/ll_one"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_there_partner_rider"
android:textSize="18sp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/_5sdp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_100sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3"
app:layout_constraintVertical_bias="0.678">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/noBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/bgbtn1"
android:radius="#dimen/_50sdp"
android:text="No" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/yellowBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#color/white"
android:background="#drawable/bgbtno"
android:text="#string/yes" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#color/reply_black_800"
android:text="#string/are_you_using_your_own_motorcycle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.074" />
</androidx.constraintlayout.widget.ConstraintLayout>
Already the BottomSheetView layout is inflated with:
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
And this layout hosts the YES button, so you can just use findViewById(). Assuming that the id of the YES button is btn_yes:
val btnYes = bottomSheetView.findViewById<Button>(R.id.btn_yes)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}
UPDATE:
As you provided the id of the YES button which is yellowBtn, then you can use it:
val btnYes = bottomSheetView.findViewById<Button>(R.id.yellowBtn)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}
The following error occurs when I try to execute the code and app also terminates.
(Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button)
package com.tisu.role
import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
#SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener {
Toast.makeText(this, "button clicked", Toast.LENGTH_LONG).show()
}
}
}
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"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/roll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/roll"
/>
</LinearLayout>
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button)
in your layout the roll_button is a TextView and in your acivity your trying doing findViewById as Button that's why you are getting ClassCastException
First solution is
Use this
val rollButton: TextView = findViewById(R.id.roll_button)
instead of this
val rollButton: Button = findViewById(R.id.roll_button)
Second solution is
assign roll_button id to your button in your layout file
<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"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:layout_width="wrap_content"
android:id="#+id/roll_button"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/roll"
/>
</LinearLayout>
Bonus
Also read this
no need to findViewById in kotlin
Kotlin Android Extensions: Say goodbye to findViewById
Goodbye findViewById, say hello to Synthetic Binding
you are trying to set TextView id to a button . change your layout code to this
<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"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_01"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<Button android:id="#+id/roll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/roll"
/>
you have issue on line
val rollButton: Button = findViewById(R.id.roll_button)
You are tried to convert text view as Button so you have to use like below
val rollButton: TextView = findViewById(R.id.roll_button)
Set id to Button in place of TextView
<Button android:id="#+id/roll_button"
I see that Android released in Oreo a new attribute for textView:
android:autoSizeTextType
This adapts the layout of the textView based on the string of text that it is showing.
How would I be able to use this with a textSwitcher?
I don't know about the ones above but here's an solution with minimal coding. You can change the main layout height to see the autosizing in action, or you can tap the upper switcher. A cool feature is that the styling of the second text field carries over when tapped (i.e. text is white).
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun switchText(v: View) {
(v as? TextSwitcher)?.let{
it.tag = (((it.tag as? Int) ?: 0) + 1) % it.childCount
it.setText((it.children.toList()[it.tag as Int] as TextView).text)
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="350dp"
tools:context=".MainActivity">
<TextSwitcher
android:id="#+id/a"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/purple_200"
app:layout_constraintBottom_toTopOf="#id/b"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:onClick="switchText"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/a1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="936sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
<TextView
android:id="#+id/a2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1000000 / 10000000"
android:textColor="#color/white"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
<TextSwitcher
android:id="#+id/b"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/a"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
private ViewFactory viewFactory = new ViewFactory() {
public View makeView() {
LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);
TextView textView = (TextView) inflater.inflate(R.layout.textView, null);
return textView;
}
};
altitudeSwitcher = (TextSwitcher) findViewById(R.id.altitude);
altitudeSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
TextView t = new TextView(getApplicationContext());
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
return t;
}
});
I need to allow user to CLICK anywhere on the screen, so I've implemented onClick listener for mainLayout and it worked great.
But the problem is I've got to add a scrollview inside mainLayout to support small screen sizes and after adding scrollview, it blocks the parentLayout (in my case mainLayout) CLICK EVENT.
Just to exaplin this, a sample layout is as follows.
<!-- MAIN LAYOUT WHERE I NEED
THE CLICK EVENT-->
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
android:onClick="onClick_MainLayout">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
I searched for this and found various ideas like disableing onClick manually in all other views inside scrollview etc but non of them works.
NOTE: I tried both implementing onClick listener manually in java and adding android:onClick="onClick_MainLayout" in to layout. Both the same.
EDITS ----------------------
All the changes I've done as you (#IntelliJ Amiya) mentioned is as below. Please let me know anything is missing.
<RelativeLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"/>
<Button
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"/>
<TextView
android:id="#+id/textview1"
android:layout_below="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And in on create
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("MyLogs", "Click Event Fires...");
}
});
Your Scroll view overlaps the linear layout i.e #mainLayout,so
set its height to wrap content
then you can perform onclick for both scroll view buttons and mainlayout.
Pls tell me if i wrong.
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_Button1(View v)
{
Toast.makeText(this, "1ST Button Click", Toast.LENGTH_SHORT).show();
}
public void onClick_Button2(View v)
{
Toast.makeText(this, "2ND Button Click", Toast.LENGTH_SHORT).show();
}
}
In have a progress bar spinner that appears once the login button is clicked, I would like the elements behind the spinner to dim or darken while the spinner is visible on the screen. My layout file looks like this currently:
<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"
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="apps.kusasa.biz.MainActivity" >
<EditText
android:id="#+id/EditText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:ems="10"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/loginBtn"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/EditText01"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/EditText01"
android:layout_marginBottom="67dp"
android:text="Login" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/EditText01"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/pics_logo" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
</RelativeLayout>
My button click method:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginBtn:
spinner.setVisibility(View.VISIBLE);
// etc
}
}
How do I achieve this? Thanks!
This will be tough when your spinner is in the same layout as the elements that you would like to dim. I would move the spinner to a separately (parent) layout and then apply a background color with an alpha channel to the relativelayout.
For example #99000000
What I did was creating a custom dialog, which has only the progress bar (instead of a complete dialog as view):
class ProgressBarDialog(context: Context) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.progress_bar_dialog)
setCancelable(false)
this.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
override fun onBackPressed() {
// do nothing
}
}
The view (progress_bar_dialog.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
That's it. The dimmed background is given (by the Dialog() it extends) and by setting setCancelable(false) and overriding the onBackPressed() method the user can't dismiss it until waiting is over.
You just need to use it in your activity:
var mMyProgressBarDialog = ProgressBarDialog()
and then call mMyProgressBarDialog.show() when you need it to appear and mMyProgressBarDialog.dismiss() when you need it to disappear.
Hope this helps.