Button is not disabling even it is in valid condition - java

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

Related

Facing problem in submit button. From verification OTP screen it is not navigating to dashboard screen

I have made three activities these are:
sendOTPone
verificationOTPtwo
dashboard
When I clicked on submit button from OTP verification screen then it will not let me go in dashboard screen my app closes automatically after this. Can anyone tell me where I did the mistake?
XML code for verificationOTPtwo
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:overScrollMode="never"
android:scrollbars="none"
tools:context=".verificationOTPtwo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:contentDescription="#string/app_name"
android:src="#drawable/recieved" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="OTP Verififcation"
android:textColor="#color/colorTextFirst"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="50dp"
android:gravity="center"
android:text="Please enter the OTP sent to you"
android:textColor="#color/colorTextSecond"
android:textSize="14sp" />
<TextView
android:id="#+id/textmobileshownumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="50dp"
android:gravity="center"
android:textColor="#color/colorTextFirst"
android:textSize="14sp"
android:textStyle="bold" />
//###############################################
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="#+id/inputotp1"
android:layout_width="40dp"
android:layout_height="46dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/inputotp2"
android:layout_width="40dp"
android:layout_height="46dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/inputotp3"
android:layout_width="40dp"
android:layout_height="46dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/inputotp4"
android:layout_width="40dp"
android:layout_height="46dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/inputotp5"
android:layout_width="40dp"
android:layout_height="46dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/inputotp6"
android:layout_width="40dp"
android:layout_height="46dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="#drawable/background"
android:gravity="center"
android:imeOptions="actionNext"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="1"
android:textColor="#color/colorTextFirst"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="LabelFor" />
</LinearLayout>
//################################################
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not received OTP ?"
android:textColor="#color/colorTextSecond"
android:textSize="14sp" />
<TextView
android:id="#+id/textresendotp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="RESEND OTP AGAIN"
android:textColor="#color/colorTextFirst"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/buttonotpsubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:background="#drawable/backgroundbutton"
android:text="Submit"
android:textColor="#color/white"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/progressbar_verify_otp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</ScrollView>
Java code for verificationOTPtwo
package com.example.animatedsplashdemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class verificationOTPtwo extends AppCompatActivity {
EditText inputnumber1, inputnumber2, inputnumber3, inputnumber4, inputnumber5, inputnumber6;
String getotpbackend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verification_otptwo);
final Button verifyButtonClick = findViewById(R.id.buttonotpsubmit);
inputnumber1 = findViewById(R.id.inputotp1);
inputnumber2 = findViewById(R.id.inputotp2);
inputnumber3 = findViewById(R.id.inputotp3);
inputnumber4 = findViewById(R.id.inputotp4);
inputnumber5 = findViewById(R.id.inputotp5);
inputnumber6 = findViewById(R.id.inputotp6);
TextView textView = findViewById(R.id.textmobileshownumber);
textView.setText(String.format(
"+91-%s", getIntent().getStringExtra("mobile")
));
getotpbackend = getIntent().getStringExtra("backendotp");
final ProgressBar progressBarverifyotp = findViewById(R.id.progressbar_verify_otp);
verifyButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!inputnumber1.getText().toString().trim().isEmpty() && !inputnumber2.getText().toString().trim().isEmpty() && !inputnumber3.getText().toString().trim().isEmpty() && !inputnumber4.getText().toString().trim().isEmpty() && !inputnumber5.getText().toString().trim().isEmpty() && !inputnumber6.getText().toString().trim().isEmpty()) {
String entercodeotp = inputnumber1.getText().toString() +
inputnumber2.getText().toString() +
inputnumber3.getText().toString() +
inputnumber4.getText().toString() +
inputnumber5.getText().toString() +
inputnumber6.getText().toString();
if (getotpbackend != null) {
progressBarverifyotp.setVisibility(View.VISIBLE);
verifyButtonClick.setVisibility(View.INVISIBLE);
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
getotpbackend, entercodeotp
);
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBarverifyotp.setVisibility(View.GONE);
verifyButtonClick.setVisibility(View.VISIBLE);
if (task.isSuccessful()) {
Intent intent = new Intent(getApplicationContext(), dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
Toast.makeText(verificationOTPtwo.this, "Enter the correct OTP", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(verificationOTPtwo.this, "Please check internet connection", Toast.LENGTH_SHORT).show();
}
// Toast.makeText(verificationOTPtwo.this, "OTP Verified", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(verificationOTPtwo.this, "Please enter the OTP", Toast.LENGTH_SHORT).show();
}
}
});
numberotpmove();
TextView resendlabel = findViewById(R.id.textresendotp);
resendlabel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91" + getIntent().getStringExtra("mobile"),
60,
TimeUnit.SECONDS,
verificationOTPtwo.this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
Toast.makeText(verificationOTPtwo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(#NonNull String newbackendotp, #NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
getotpbackend = newbackendotp;
Toast.makeText(verificationOTPtwo.this, "OTP sended successfully", Toast.LENGTH_SHORT).show();
}
}
);
}
});
}
private void numberotpmove() {
inputnumber1.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.toString().trim().isEmpty()) {
inputnumber2.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber2.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.toString().trim().isEmpty()) {
inputnumber3.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber3.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.toString().trim().isEmpty()) {
inputnumber4.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber4.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.toString().trim().isEmpty()) {
inputnumber5.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber5.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.toString().trim().isEmpty()) {
inputnumber6.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
dashboard.java :
package com.example.animatedsplashdemo;
import android.os.Bundle;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class dashboard extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
drawerLayout = findViewById(R.id.drawerlayout);
navigationView = findViewById(R.id.navigationview);
toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_open, R.string.navigation_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);
}
}
Logcat:
2022-04-03 01:27:14.483 21162-21162/com.example.animatedsplashdemo
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.animatedsplashdemo, PID: 21162
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.animatedsplashdemo/com.example.animatedsplashdemo.dashboard}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:581)
at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:183)
at com.example.animatedsplashdemo.dashboard.onCreate(dashboard.java:32)
at android.app.Activity.performCreate(Activity.java:8198)
at android.app.Activity.performCreate(Activity.java:8182)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:246) 
at android.app.ActivityThread.main(ActivityThread.java:8506) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
  
Set windowActionBar false in your AppTheme.
<item name="windowActionBar">false</item>
Hope this will help you.

Cant find Exact Error Line Number in the Console

here console show ClassCastException but not show error line Number? Any one help me here?
if any one has idea about to find exact errorline number then suggest me plz?
here xml where i used ListView.
i have tried
1) removed Params which set by programatically
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adlayout"
android:background="#color/md_white_0">
<com.os.folder.and.file.locker.files.widget.BGridView
android:id="#+id/hide_view_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/hide_btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="#color/file_add"
android:onClick="onClick"
android:src="#drawable/ic_action_new" />
<LinearLayout
android:id="#+id/file_bottom_layout_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/unrecord" />
<TextView
android:id="#+id/file_bottom_txt_tips"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center"
android:text=""
android:textColor="#color/md_black_2"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/pic_hide_btn_edit"
android:layout_width="match_parent"
android:layout_height="#dimen/title_height"
android:layout_above="#+id/adlayout"
android:layout_alignParentRight="true"
android:background="#color/md_black_1"
android:visibility="visible"
android:weightSum="4">
<LinearLayout
android:id="#+id/pic_hide_img_cancel"
android:layout_width="0dp"
android:layout_height="#dimen/title_height"
android:layout_weight="1"
android:gravity="center"
android:onClick="onClick">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="#dimen/title_height"
android:layout_weight="1"
android:gravity="center">
<CheckBox
android:id="#+id/item_file_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
</LinearLayout>
<LinearLayout
android:id="#+id/pic_hide_img_recovery"
android:layout_width="0dp"
android:layout_height="#dimen/title_height"
android:layout_weight="1"
android:gravity="center"
android:onClick="onClick">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/unlock" />
</LinearLayout>
<LinearLayout
android:id="#+id/pic_hide_img_del"
android:layout_width="0dp"
android:layout_height="#dimen/title_height"
android:layout_weight="1"
android:gravity="center"
android:onClick="onClick">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/delete" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/adlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" />
</RelativeLayout>
here Java file, i have implemented from here listView
package com.os.folder.and.file.locker.files.activity;
import android.content.Intent;
import com.os.folder.and.file.locker.R;
import com.os.folder.and.file.locker.data.GroupImage;
import com.os.folder.and.file.locker.data.HideImage;
import com.os.folder.and.file.locker.files.adapter.BaseHideAdapter.OnListener;
import com.os.folder.and.file.locker.files.adapter.PicHideAdapter;
import com.os.folder.and.file.locker.files.entity.HideImageExt;
import com.os.folder.and.file.locker.files.widget.BGridView;
import com.os.folder.and.file.locker.service.GroupImageService;
import com.os.folder.and.file.locker.service.ImageService;
import java.util.List;
public class PicHideActivity extends BaseHideActivity implements OnListener {
protected static final String TAG = "PicHideActivity";
private int itemSize;
protected GroupImageService mGroupImageService;
protected ImageService mImageService;
protected void initUI() {
setContentView((int) R.layout.activity_file_hide_group);
setUI();
setTitleRID(R.string.pic_preview_title, R.string.pic_preview_title_edit);
setGridView();
this.mFile_bottom_txt_tips.setText(R.string.file_hide_txt_add_pic);
this.rid_string_type = R.string.pic_preview;
}
private void setGridView() {
this.itemSize = ((BGridView) findViewById(R.id.hide_view_list)).setGridView(getWindowManager(), 4, 4);
}
void initAdapter() {
BGridView adapterView = findViewById(R.id.hide_view_list);
this.mGroupImageService = new GroupImageService(this);
this.mImageService = new ImageService(this);
this.mBaseHideAdapter = new PicHideAdapter(this, this, this.itemSize);
adapterView.setAdapter(this.mBaseHideAdapter);
}
boolean delFolder() {
return false;
}
void addFolder() {}
public void addFile() {
Intent intent = new Intent(this, PicPreViewActivity.class);
intent.putExtra("beyondGroupId", this.mBaseHideAdapter.getGruopID());
startActivity(intent);
}
protected void recoveryFiles() {
for (Object imageModelView: this.mBaseHideAdapter.getHitFiles()) {
this.mImageService.unHideImage((HideImageExt) imageModelView);
}
}
protected void delFiles() {
for (Object hideImageExt: this.mBaseHideAdapter.getHitFiles()) {
this.mImageService.deleteAudioByPath((HideImageExt)
hideImageExt);
}
}
protected void openHolder(int groupID) {
List < GroupImage > groupList = this.mGroupImageService.getGroupFiles(groupID);
List < HideImage > list = this.mImageService.getHideImages(groupID);
this.mBaseHideAdapter.setHitFiles(groupList, list, groupID);
setHasData(groupList, list);
}
public void openHolder(Object object) {
GroupImage data = (GroupImage) object;
int groupID = -1;
if (data != null) {
groupID = data.getId().intValue();
}
openHolder(groupID);
}
}
and here is adapter code,
package com.os.folder.and.file.locker.files.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
//import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import
com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.os.folder.and.file.locker.R;
import com.os.folder.and.file.locker.data.GroupImage;
import com.os.folder.and.file.locker.data.HideImage;
import com.os.folder.and.file.locker.files.activity.PhotoPreViewActivity;
import com.os.folder.and.file.locker.files.entity.GroupImageExt;
import com.os.folder.and.file.locker.files.entity.HideImageExt;
import java.util.ArrayList;
import java.util.List;
public class PicHideAdapter extends BaseHideAdapter {
private static final String TAG = "PicHideAdapter";
protected ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new Builder().showStubImage(R.drawable.default_picture).showImageForEmptyUri((int) R.drawable.default_picture).showImageOnFail((int) R.drawable.default_picture).cacheInMemory(true).cacheOnDisc(true).build();
private ViewGroup.LayoutParams params;
class PicHolder {
Object mData;
ImageView mImg_pre_preview;
View mItem_file_ok;
View mItem_file_pic;
PicHolder() {}
}
public PicHideAdapter(Context context, OnListener onListern, int itemSize) {
super(context, onListern);
this.params = new ViewGroup.LayoutParams(itemSize, itemSize);
}
public void clear() {
super.clear();
if (this.imageLoader != null) {
this.imageLoader.stop();
this.imageLoader = null;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.item_file_hide_pic, null);
PicHolder picHolder = new PicHolder();
picHolder.mItem_file_pic = convertView.findViewById(R.id.item_file_pic);
picHolder.mItem_file_ok = convertView.findViewById(R.id.item_file_ok);
picHolder.mImg_pre_preview = (ImageView)
convertView.findViewById(R.id.img_pre_preview);
convertView.setTag(picHolder);
// convertView.setLayoutParams(this.params);
}
initView(convertView, position);
return convertView;
}
protected void initView(View view, final int position) {
int i = 8;
final PicHolder fileHolder = (PicHolder) view.getTag();
fileHolder.mImg_pre_preview.setImageBitmap(null);
Object data = getItem(position);
fileHolder.mData = data;
if (data instanceof HideImageExt) {
final HideImageExt hideImageView = (HideImageExt) data;
this.imageLoader.displayImage(Scheme.THUMBNAIL.wrap(hideImageView.getNewPathUrl()), fileHolder.mImg_pre_preview, this.options);
if (this.edit) {
View view2 = fileHolder.mItem_file_ok;
if (hideImageView.isEnable()) {
i = 0;
}
view2.setVisibility(i);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean z;
int i = 0;
HideImageExt hideImageExt = hideImageView;
if (hideImageView.isEnable()) {
z = false;
} else {
z = true;
}
hideImageExt.setEnable(z);
View view = fileHolder.mItem_file_ok;
if (!hideImageView.isEnable()) {
i = 8;
}
view.setVisibility(i);
PicHideAdapter.this.updateSelect();
}
});
view.setOnLongClickListener(null);
return;
}
fileHolder.mItem_file_ok.setVisibility(8);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(PicHideAdapter.this.context, PhotoPreViewActivity.class);
intent.putParcelableArrayListExtra("list", (ArrayList)
PicHideAdapter.this.mList_HideFile);
intent.putExtra("id", position);
PicHideAdapter.this.context.startActivity(intent);
}
});
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
PicHideAdapter.this.doVibrator(PicHideAdapter.this.context);
PicHideAdapter.this.mOnListern.onLongClick(hideImageView);
return false;
}
});
} else if (data instanceof GroupImageExt) {
final GroupImageExt groupImageView = (GroupImageExt) data;
fileHolder.mItem_file_ok.setVisibility(8);
fileHolder.mImg_pre_preview.setImageResource(R.drawable.folder);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (PicHideAdapter.this.edit) {
PicHolder fileHolder = (PicHolder) v.getTag();
groupImageView.setEnable(!groupImageView.isEnable());
} else if (PicHideAdapter.this.mOnListern != null) {
PicHideAdapter.this.mOnListern.openHolder(groupImageView);
}
}
});
}
}
public void setHitFiles(List < ?>listGroup, List < ?>listFile, int groupID) {
this.mList_Group = GroupImageExt.transList((List < GroupImage > ) listGroup);
this.mList_HideFile = HideImageExt.transList((List < HideImage > ) listFile);
setGroup(groupID);
notifyDataSetChanged();
}
}
LayoutParams lp = new LayoutParams(-2, -2);
lp.setMargins(margin, margin, margin, margin);
setLayoutParams(lp);
resolved..by removing this param

Confuse with simple condition

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.

How do I set a wallpaper based on the Image selected?

I'm making a wallpaper app similar in functionality to Google's new wallpaper app. There is a Horizontal Scroll View at the bottom with a list of Image Buttons that when clicked will show a preview of the wallpaper in the app. How do I tell the app to set the wallpaper from the one being previewed?
My xml:
<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:id="#+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
tools:context="biz.bigtooth.wallpapers.MainActivity">
<Button
android:id="#+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="16dp"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:background="#color/button"
android:gravity="start|center_vertical"
android:textColor="#android:color/white"
android:text="Set Wallpaper" />
<HorizontalScrollView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/bottom">
<ImageButton android:id="#+id/dsc18"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0018" />
<ImageButton android:id="#+id/dsc65"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0065" />
<ImageButton android:id="#+id/dsc131"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0131" />
<ImageButton android:id="#+id/dsc175"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0175" />
<ImageButton android:id="#+id/dsc246"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0246" />
<ImageButton android:id="#+id/dsc274"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_margin="8dp"
android:focusable="true"
android:src="#drawable/_dsc0274" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
My Java:
import android.app.WallpaperManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.FocusFinder;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
RelativeLayout layout;
HorizontalScrollView view;
Button set;
ImageButton dsc18;
ImageButton dsc65;
ImageButton dsc131;
ImageButton dsc175;
ImageButton dsc246;
ImageButton dsc274;
int oneeight = R.drawable._dsc0018pr;
int sixfive = R.drawable._dsc0065pr;
int onethreeone = R.drawable._dsc0131pr;
int onesevenfive = R.drawable._dsc0175pr;
int twofoursix = R.drawable._dsc0246pr;
int twosevenfour = R.drawable._dsc0274pr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout)findViewById(R.id.layout_main);
layout.setBackgroundResource(oneeight);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view.setVisibility(view.isShown()
? View.GONE
: View.VISIBLE );
}
});
view = (HorizontalScrollView)findViewById(R.id.view);
set = (Button)findViewById(R.id.set);
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setWallpaper(onesevenfive);
}
});
dsc18 = (ImageButton)findViewById(R.id.dsc18);
dsc65 = (ImageButton)findViewById(R.id.dsc65);
dsc131 = (ImageButton)findViewById(R.id.dsc131);
dsc175 = (ImageButton)findViewById(R.id.dsc175);
dsc246 = (ImageButton)findViewById(R.id.dsc246);
dsc274 = (ImageButton)findViewById(R.id.dsc274);
dsc18.hasFocus();
dsc18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(oneeight);
dsc18.hasFocus();
}
});
dsc65.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(sixfive);
dsc65.hasFocus();
}
});
dsc131.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(onethreeone);
dsc131.hasFocus();
}
});
dsc175.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(onesevenfive);
dsc175.hasFocus();
}
});
dsc246.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(twofoursix);
dsc246.hasFocus();
}
});
dsc274.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundResource(twosevenfour);
dsc274.hasFocus();
}
});
}
public void setWallpaper(int id) {
int wall = getBackground;
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(wall);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks for any and all help in advance!
First , you need to set the required permission
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
and then :
dsc18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(onethreeone);
} catch (IOException e) {
e.printStackTrace();
}
dsc18.hasFocus();
}
});

how to update seekbar using a edittext

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) {}
}
});

Categories