i read an example of how to show the progres of a seekbar to the edittext this is the web page:
Simple Seekbar In Android
now my problem is how to change the seekbar if i introduce a number inside the edittext box? thanks for any help
if you can acces the web page i will also post the code:
main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="94dp" >
<requestFocus />
</EditText>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="38dp" />
</RelativeLayout>
and the java code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class SeekbarActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SeekBar sb=(SeekBar) findViewById(R.id.seekBar1);
final EditText et=(EditText) findViewById(R.id.editText1);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
//---change the font size of the EditText---
et.setText(String.valueOf(progress));
}
});
}
}
Use addTextChangedListener
A quick example :
//et and sk are class variables
et=(EditText)findViewById(R.id.editText);
sk = (SeekBar)findViewById(R.id.seekBar);
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
//Update Seekbar value after entering a number
sk.setProgress(Integer.parseInt(s.toString()));
} catch(Exception ex) {}
}
});
Related
I am creating a simple Android app, in which the user can drag a SeekBar to the left to reveal less of an image, and right to reveal more of an image. This is accomplished through attaching a listener to the SeekBar, in which the width of the ImageView is changed proportionally to the SeekBar's progress. The problem is that no matter what position the slider is in, the width of the ImageView remains the same, as shown in the screenshots below:
Progress = 0%:
Progress = 100%
This seems strange, considering that in the SeekBar's listener, when I output the value I am setting the ImageView width to, that value is correct.
Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="300px"
android:layout_width="300px"
android:orientation="vertical"
android:background="#0000FF">
<ImageView
android:layout_height="300px"
android:layout_width="300px"
android:src="#drawable/android"
android:scaleType="matrix"
android:id="#+id/iv"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = findViewById(R.id.iv);
SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 300 * progress/100;
System.out.println(300 * progress/100);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Note: The image is NOT supposed to be resized when the slider is moved.
This could be done with an additional View on top of the layout hierarchy with the same size as ImageView. We dynamically change the top View startMargin to have an image revealing/hiding effect.
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.iv);
final View dynamicView = ((View) findViewById(R.id.dynamicView));
final SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) dynamicView.getLayoutParams();
params.setMarginStart(imageView.getWidth() * progress / 100);
dynamicView.setLayoutParams(params);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="300dp"
android:layout_width="300dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="#drawable/owl"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/dynamicView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code: https://github.com/dautovicharis/sos_android/tree/q_68375499
Additional:
Instead of px use dp in layouts
My suggestion is to switch to Kotlin
ConstraintLayout is a better option if you want to have better performance and avoid nested views
EDIT:
I was not happy with the first solution and did a small research. A better way is to do it using ImageView overlay.
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.iv);
final SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
addOverlay(R.drawable.overlay, imageView, imageView.getWidth() * progress / 100);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// Initial overlay
imageView.post(() -> addOverlay(R.drawable.overlay, imageView, 0));
}
private void addOverlay(int resourceId, ImageView imageView, int startMargin) {
imageView.getOverlay().clear();
Drawable drawable = ResourcesCompat.getDrawable(getResources(), resourceId, null);
drawable.setBounds(new Rect(startMargin, 0, imageView.getWidth(), imageView.getHeight()));
imageView.getOverlay().add(drawable);
}
}
res->drawable-overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" />
</layer-list>
res->layout->activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="matrix"
android:src="#drawable/owl"
/>
</LinearLayout>
Code: https://github.com/dautovicharis/sos_android/commits/q_68375499_v2
EDIT: Final solution and what Adam actually wanted to achieve is:
Code: https://github.com/dautovicharis/sos_android/tree/q_68375499_v3
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 300 * progress/100;
iv.drawableStateChanged(); //<--- You need to redraw it after you change it.
System.out.println(300 * progress/100);
}
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 200 * progress;
iv.getLayoutParams().height= 200 * progress;
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I tried to combine seekBar and CountDownTimer in which I slide the seekBar to a 5 seconds and then release,it will then hit the CountDownTimer code to count from 5 second to 0 second. My problem is as soon as i release the seekBar slide,I want the slide also decreases (move to the left)(automatically) sync with both the timer and the slide reach 0. Help me thank you!!
package com.example.timerrr;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
CountDownTimer mCount;
SeekBar seekbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar= (SeekBar) findViewById(R.id.seekBarTimer);
seekbar.setMax(60000);
seekbar.setProgress(0);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mCount.start();
}
});
}
}
xml file
<?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="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="428dp"
android:layout_marginEnd="162dp"
android:layout_marginRight="162dp"
android:onClick="startButton11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="#+id/seekBarTimer"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try this:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
if(!fromUser)
return;
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
seekBar.setProgress((int) x);
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.cancel();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.start();
}
});
I don't know why I confuse with simple if/ else condition? In my app, phone verification part, Bydefault I set sendVerification button disable, and when I enter text then it should enable. but it is not enabling? What is wrong with my condition? even if I try with else part, same problem!
XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".PhoneAuthActivity">
<include
android:id="#+id/PhoneToolbar"
layout="#layout/app_bar">
</include>
<LinearLayout
android:id="#+id/DialLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/PhoneToolbar"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="#+id/dial"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/dial"
android:layout_weight="1"/>
<EditText
android:id="#+id/PhoneNumber"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:layout_weight="8"
android:ems="10"
android:inputType="phone"/>
<ProgressBar
android:id="#+id/PhoneProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/LockLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/DialLayout"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="#+id/lock"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/lock"
android:layout_weight="1"/>
<EditText
android:id="#+id/code"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Verification Code"
android:layout_weight="8"/>
<ProgressBar
android:id="#+id/CodeProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="#+id/VerificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:text="A verification code will be sent to your phone number" />
<Button
android:id="#+id/sendVerification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:backgroundTint="#FF0000"
android:text="Send Verification" />
</RelativeLayout>
And this is Activity
package com.jimmytrivedi.lapitchat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class PhoneAuthActivity extends AppCompatActivity {
private LinearLayout DialLayout, LockLayout;
private EditText PhoneNumber, code;
private ProgressBar PhoneProgress, CodeProgress;
private Button sendVerification;
private Toolbar PhoneToolbar;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private String number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_auth);
DialLayout = findViewById(R.id.DialLayout);
LockLayout = findViewById(R.id.LockLayout);
PhoneNumber = findViewById(R.id.PhoneNumber);
code = findViewById(R.id.code);
PhoneProgress = findViewById(R.id.PhoneProgress);
CodeProgress = findViewById(R.id.CodeProgress);
sendVerification = findViewById(R.id.sendVerification);
PhoneToolbar = findViewById(R.id.PhoneToolbar);
PhoneProgress.setVisibility(View.INVISIBLE);
CodeProgress.setVisibility(View.INVISIBLE);
sendVerification.setEnabled(false);
setSupportActionBar(PhoneToolbar);
getSupportActionBar().setTitle("Welcome to Phone Verification");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
sendVerification.setEnabled(true);
}
sendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PhoneNumber.setEnabled(false);
PhoneProgress.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
PhoneAuthActivity.this,
mCallbacks
);
}
});
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
#Override
public void onVerificationFailed(FirebaseException e) {
}
};
}
}
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
sendVerification.setEnabled(true);
}
This is all executed in onCreate before you get the chance to start typing. Once you start typing this code is not executed again. It is only executed once, when the activity is being created.
Solution: set up a TextWatcher on the phone number editText and enable/disable sendVerification in the TextWatcher#afterTextChanged method. See here for instructions on how to do it.
In your code
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
sendVerification.setEnabled(true);
}
is executed only once at onCreate. You'll have to add a text change listener and enable/disable the button accordingly
like,
PhoneNumber.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() > 0)
{
sendVerification.setEnabled(true);
}
}
});
You need a TextWatcher to do what you wish to do, so add a TextWatcher to your EditText/TextView like below:
PhoneNumber.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length()>0) {
sendVerification.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
The reason your code wasn't working is because the onCreate() method does not get called after you start typing, meaning the condition for enabling your view is checked only once so any changes are not checked again.
In my chat app, I'm adding Phone verification from user. So I want that when user will enter the number, after that sendVerification button should be setEnabled(true) and I set setEnabled(false) as default, but when I run the app, it is still showing as enabled without entering the digits.
activity_phone_auth.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".PhoneAuthActivity">
<include
android:id="#+id/PhoneToolbar"
layout="#layout/app_bar">
</include>
<LinearLayout
android:id="#+id/DialLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/PhoneToolbar"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="#+id/dial"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/dial"
android:layout_weight="1"/>
<EditText
android:id="#+id/PhoneNumber"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:layout_weight="8"
android:ems="10"
android:inputType="phone"/>
<ProgressBar
android:id="#+id/PhoneProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/LockLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/DialLayout"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="#+id/lock"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/lock"
android:layout_weight="1"/>
<EditText
android:id="#+id/code"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Verification Code"
android:layout_weight="8"/>
<ProgressBar
android:id="#+id/CodeProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="#+id/VerificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:text="A verification code will be sent to your phone number" />
<Button
android:id="#+id/sendVerification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:backgroundTint="#FF0000"
android:text="Send Verification" />
</RelativeLayout>
PhoneAuthActivity.java
package com.jimmytrivedi.lapitchat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class PhoneAuthActivity extends AppCompatActivity {
private LinearLayout DialLayout, LockLayout;
private EditText PhoneNumber, code;
private ProgressBar PhoneProgress, CodeProgress;
private Button sendVerification;
private Toolbar PhoneToolbar;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private String number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_auth);
DialLayout = findViewById(R.id.DialLayout);
LockLayout = findViewById(R.id.LockLayout);
PhoneNumber = findViewById(R.id.PhoneNumber);
code = findViewById(R.id.code);
PhoneProgress = findViewById(R.id.PhoneProgress);
CodeProgress = findViewById(R.id.CodeProgress);
sendVerification = findViewById(R.id.sendVerification);
PhoneToolbar = findViewById(R.id.PhoneToolbar);
PhoneProgress.setVisibility(View.INVISIBLE);
CodeProgress.setVisibility(View.INVISIBLE);
sendVerification.setEnabled(false);
setSupportActionBar(PhoneToolbar);
getSupportActionBar().setTitle("Welcome to Phone Verification");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
} else {
sendVerification.setEnabled(false);
}
sendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PhoneNumber.setEnabled(false);
PhoneProgress.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
PhoneAuthActivity.this,
mCallbacks
);
}
});
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
#Override
public void onVerificationFailed(FirebaseException e) {
}
};
}
}
Use a TextWatcher() to know when the user enters the text in edit text. Do like this:-
Replace this:-
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
}
else {
sendVerification.setEnabled(false);
}
with this:-
PhoneNumber.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = charSequence.toString();
if(!text.isEmpty()){
sendVerification.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
sendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!PhoneNumber.getText().toString().isEmpty()){
number = PhoneNumber.getText().toString();
PhoneNumber.setEnabled(false);
PhoneProgress.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
PhoneAuthActivity.this,
mCallbacks
);
}
else {
Toast.makeText(PhoneAuthActivity.this, "Empty Field", Toast.LENGTH_SHORT).show();
}
}
});
Replace
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
} else {
sendVerification.setEnabled(false);
}
with:
number = PhoneNumber.getText().toString();
if (number.isEmpty()) {
sendVerification.setEnabled(false);
} else {
sendVerification.setEnabled(true);
}
Set sendVerification.setEnabled(false) by default in onCreate(...) method and then apply TextChangedListener like this
PhoneNumber.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().equalsIgnoreCase("")) {
sendVerification.setEnabled(false);
} else {
sendVerification.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
It will automatically disable sendVerification Button whenever PhoneNumber is blank
I am using SeekBar to change the opacity level in drawing But I am getting Nullpointer exception in the lines
seekOpq.setMax(100);
and
seekTxt.setText(currLevel);
in the following code..
public void onClick(View view){
if(view.getId()==R.id.opacity_btn){
//launch opacity chooser
final Dialog seekDialog = new Dialog(this);
final TextView seekTxt = (TextView)seekDialog.findViewById(R.id.opq_txt);
final SeekBar seekOpq = (SeekBar)seekDialog.findViewById(R.id.seek);
Toast.makeText(getApplicationContext(),
"start.", Toast.LENGTH_SHORT).show();
seekDialog.setTitle("Opacity level:");
seekDialog.setContentView(R.layout.opacity_chooser);
seekOpq.setMax(100);
int currLevel = drawView.getPaintAlpha();
seekTxt.setText(currLevel);
seekOpq.setProgress(currLevel);
seekOpq.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekTxt.setText(Integer.toString(progress)+" % ");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar){}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
Button opqBtn = (Button)seekDialog.findViewById(R.id.opq_ok);
opqBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setPaintAlpha(seekOpq.getProgress());
Toast.makeText(getApplicationContext(),
"end.", Toast.LENGTH_SHORT).show();
seekDialog.dismiss();
}
});
}
I have tried giving numeric arguments manualy to these lines like
seekTxt.setText(50);
But the error still remains the same
This is my opacity_chooser.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/opq_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="100%"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<Button
android:id="#+id/opq_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
First set Content View after initialize seekbar & textview.
eg.
final Dialog seekDialog = new Dialog(this);
seekDialog.setContentView(R.layout.opacity_chooser);
final TextView seekTxt = (TextView)seekDialog.findViewById(R.id.opq_txt);
final SeekBar seekOpq = (SeekBar)seekDialog.findViewById(R.id.seek);
Toast.makeText(getApplicationContext(),
"start.", Toast.LENGTH_SHORT).show();
seekDialog.setTitle("Opacity level:");
seekOpq.setMax(100);
-------------------