countdown with editText - java

I would like to create a countdown where the initial number is given by the user. I created this application but when the application starts shut down. I do not understand what the mistake is because there are no errors during the compilation. What am I doing wrong?
this is the code:
public class Main2Activity extends AppCompatActivity {
Button bstart, bStopReset;
EditText editTimer;
CountDownTimer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editTimer = (EditText) findViewById(R.id.contatore);
bstart = (Button) findViewById(R.id.start);
bStopReset = (Button) findViewById(R.id.stop_reset);
}
String valore = editTimer.getText().toString();
int ValoreIntero = Integer.parseInt(valore);
public void startOnClick(View view) {
timer = new CountDownTimer(ValoreIntero, 1000) {
#Override
public void onTick(final long millSecondsLeftToFinish) {
String time = String.valueOf(millSecondsLeftToFinish / 1000);
editTimer.setText(time);
}
#Override
public void onFinish() {
editTimer.setText("Done!");
}
};
timer.start();
}
public void stopOnClick(View view) {
timer.cancel();
editTimer.setText("0");
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:weightSum="1"
tools:context="com.example.gabrypacor.orologio.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.39"
android:gravity="center"
android:text="countdown"
android:textAlignment="center"
android:textSize="36sp" />
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.27"
android:gravity="center"
android:text="inserisci il tempo del countdown nel riquadro blu"
android:textAlignment="center"
android:textSize="20sp"
tools:textAlignment="center" />
<EditText
android:id="#+id/contatore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.21"
android:background="#android:color/holo_blue_light"
android:ems="10"
android:inputType="numberDecimal"
android:text="0"
android:textAlignment="center"
android:textColorLink="?android:attr/textColorPrimaryDisableOnly"
android:textSize="60sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:onClick="startOnClick"
android:text="inizio" />
<Button
android:id="#+id/stop_reset"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:onClick="stopOnClick"
android:text="stop/reset" />
</LinearLayout>

You are setting these two values when your activity is initialized rather than when the user clicks the start button:
String valore = editTimer.getText().toString();
int ValoreIntero = Integer.parseInt(valore);
Move them into your startOnClick method and that should fix the exception you are getting.

Related

Why won't the textinputlayout in my xml show up?

The textinputlayout I created is for a login screen, but whenever the activity is launched, everything else shows except for that. Clicking on the space where it should be also does nothing (the keyboard does not come up) so it seems like its visibility has been set to "gone" even though it has not.
Layout file:
<?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:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.thelatinschool.canvasgrades.SplashScreenActivity">
<ImageView
android:id="#+id/logo"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:contentDescription="#null"
android:src="#mipmap/ic_launcher_round"
android:visibility="visible" />
<ProgressBar
android:id="#+id/loadingProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-4dp"
android:foregroundGravity="bottom"
android:indeterminate="true"
android:padding="0dp"
android:theme="#style/ProgressBarStyle"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/afterAnimationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="130dp"
android:layout_marginEnd="20dp"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="#+id/WelcomeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Highlands Latin School"
android:textColor="#color/colorPrimary"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/readItTogetherTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/WelcomeTextView"
android:layout_marginTop="10dp"
android:text="Canvas Grades"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/loginButton"
android:layout_below="#+id/readItTogetherTextView"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Email"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxBackgroundMode="outline" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="25dp"
android:hint="Password"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxStrokeColor="#000000" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:background="#FFFFFF"
android:text="Forgot Password?"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
<Button
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_above="#+id/skipTextView"
android:layout_marginBottom="5dp"
android:background="#drawable/button_drawable"
android:text="Login"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="#+id/skipTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="12dp"
android:text="Incorrect username or password!"
android:textColor="#B53737"
android:textSize="15sp"
android:visibility="invisible" />
</RelativeLayout>
</RelativeLayout>
And the corresponding java:
public class SplashScreenActivity extends AppCompatActivity {
public String theme1;
private ProgressDialog progressDialog;
private boolean animationStarted = false;
private ImageView bookIconImageView;
private TextView bookITextView;
private ProgressBar loadingProgressBar;
int progress;
private RelativeLayout rootView, afterAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
bookIconImageView = findViewById(R.id.logo);
loadingProgressBar = (ProgressBar)findViewById(R.id.loadingProgressBar);
rootView = findViewById(R.id.rootView);
afterAnimationView = findViewById(R.id.afterAnimationView);
Thread thred = new Thread(new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
public void doWork() {
for (progress=10;progress<100;progress=progress+10){
try {
Thread.sleep(350);
loadingProgressBar.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}} }
public void startApp(){
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingProgressBar.setVisibility(GONE);
rootView.setBackgroundColor(ContextCompat.getColor(SplashScreenActivity.this, R.color.splashscreen));
bookIconImageView.setImageResource(R.mipmap.ic_launcher_round);
}
});
startAnimation();
};
});
thred.start();
}
private void startAnimation() {
ViewPropertyAnimator viewPropertyAnimator = bookIconImageView.animate();
viewPropertyAnimator.x(50f);
viewPropertyAnimator.y(100f);
viewPropertyAnimator.setDuration(1000);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
afterAnimationView.setVisibility(VISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
// Intent myIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
// SplashScreenActivity.this.startActivity(myIntent);
}
I have an animation for the splash screen that displays right before the login screen, I haven't touched anything to do with the text input box in java but maybe I made a mistake somewhere there.
Most probably you miss adding app level material design dependency
// material Design support library - androidx
implementation 'com.google.android.material:material:1.0.0'
// material Design support library - support library
implementation 'com.android.support:design:28.0.0'
UPDATE
You're missing adding TextInputEditText within TextInputLayout, so you won't expect to see something, also move android:hint, android:textSize, android:textColorHint & android:textColor attributes into TextInputEditText instead of TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
app:boxBackgroundMode="outline">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp" />
</com.google.android.material.textfield.TextInputLayout>

Unable to use components of different layout in android main activity

In my activity I am displaying splash(splash.xml) screen for 5 sec and then I am changing content view to my actual layout of main activity(activity_main). But when I am trying to use the components in the my main activity layout(activity_main) I am getting null pointer exception.
Main Activity is
public class MainActivity extends ActionBarActivity {
ProgressBar progressBar2;
EditText userName,password;
static String url;
JSONObject reader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new CountDownTimer(5000,1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
progressBar2=(ProgressBar)findViewById(R.id.progressBar2);
progressBar2.setVisibility(View.GONE);
userName=(EditText)findViewById(R.id.userName);
password=(EditText)findViewById(R.id.password);
}
}
when I use any of this components I am getting null pointer exception.
My splash Screen layout file splash.xml
<RelativeLayout
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_web">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="111dp"
/>
</RelativeLayout>
and the main layout(activity_main.xml is)
<LinearLayout
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:hint="Username"
android:layout_height="wrap_content"
android:id="#+id/userName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password"
android:hint="Password"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:id="#+id/checkBox"
android:checked="false"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button"
android:onClick="login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:id="#+id/button3"
android:onClick="signUp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"/>
</LinearLayout>
</LinearLayout>
Because your
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
will be executed after 5 seconds.
Set your layout
setContentView(R.layout.activity_main);
before progressBar2=(ProgressBar)findViewById(R.id.progressBar2);

Resolve java.lang.NumberFormatException: Invalid double: "" without losing placeholder text

Initializing the EditTexts on onCreate is returning a "" which cannot be cast into valid double hence this error. Using an explicit value might help but then I will lose the android:hint text.
How can I resolve this issue without losing the android:hint text ?
Code
public class MainActivity extends Activity {
private double quant, problem, verbal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText quantScore = (EditText) findViewById(R.id.quant);
EditText problemScore = (EditText) findViewById(R.id.problem);
EditText verbalScore = (EditText) findViewById(R.id.verbal);
Button calculate = (Button) findViewById(R.id.button);
quant = Double.valueOf(quantScore.getText().toString());
problem = Double.valueOf(problemScore.getText().toString());
verbal = Double.valueOf(verbalScore.getText().toString());
final double total = quant + problem + verbal;
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "The total is " +total, Toast.LENGTH_LONG).show();
}
});
}
Layout
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score To Percentile"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/quant"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:hint="Quantitative Ability"
android:gravity="center"
android:textStyle="italic" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/problem"
android:layout_marginTop="50dp"
android:layout_below="#+id/quant"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Problem Solving"
android:gravity="center"
android:textColorLink="#ffffdbeb"
android:textStyle="italic" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/verbal"
android:layout_marginTop="50dp"
android:hint="Verbal Ability"
android:gravity="center"
android:textColorLink="#ff48f3ff"
android:layout_below="#+id/problem"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="italic" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/button"
android:layout_below="#+id/verbal"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp"
android:textStyle="italic"
android:typeface="monospace" />
You should catch the NumberFormatException and provide the desired default value.
try {
quant = Double.valueOf(quantScore.getText().toString());
} catch(NumberFormatException e) {
quant = 0;
}
This will work for any text that can't be parsed to Double not only when the EditText is empty.

Motion event error on ImageButton click

public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.

Unable to click ImageButton

I have a following layout in main_view.xml which contains a ImageButton in it, button when i click/touch the button the ButtonListener function never gets called.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/mapArea"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/locate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="#string/findingLocation"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageButton
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginLeft="#dimen/rightLeftMargin"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/button1text"
android:src="#drawable/ic_feed_active" />
<ImageButton
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button2"
android:src="#drawable/ic_button2" />
<ImageButton
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginRight="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button3View"
android:src="#drawable/ic_settings_active" />
</RelativeLayout>
And following code in MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User usrObj = new User();
if (usrObj.authenticateUser())
setContentView(R.layout.main_view);
else
setContentView(R.layout.login);
}
public void ButtonListener() {
ImageButton oggleButton = (ImageButton) findViewById(R.id.Button1);
ToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "ImageButton is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
}
please help.
you forget to call ButtonListener method when usrObj.authenticateUser() is true. do it as:
if (usrObj.authenticateUser())
{
setContentView(R.layout.main_view);
ButtonListener (); << call here
}
else
setContentView(R.layout.login);
and also attach setOnClickListener to oggleButton instead of ToggleButton
apart from above suggestions from ρяσѕρєя K, here is one more thing you have to take care is.
instead of this below code.
oggleButton.setOnClickListener(new OnClickListener() {
use this one
oggleButton.setOnClickListener(new View.OnClickListener() { // use View.oncli......

Categories

Resources