I am a beginner in android.
My Scenario
I have a screen A which has 2 buttons
Button A
Button B.
When I open my application screen A opens up with the above 2 buttons, when I click on Button B a textview and Edittext is displayed.
What I want ?
When the back button is pressed the textview and edittext should hide and when I press back again, I should exit out of Screen A.
What have I tried so far ?
Is my below code correct for what I want ?
Main Activity.xml
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView title;
EditText userinput;
Button buttonA,buttonB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
userinput = (EditText)findViewById(R.id.userinput);
title = (TextView)findViewById(R.id.title);
buttonA = (Button)findViewById(R.id.buttonA);
buttonB = (Button)findViewById(R.id.buttonB);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
}
#Override
public void onBackPressed() {
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.buttonA:
break;
case R.id.buttonB:
title.setVisibility(View.VISIBLE);
userinput.setVisibility(View.VISIBLE);
break;
}
}
}
I referred this and this link but did not understand. If some one could help me in achieving me want I want 1
When the back button is pressed the textview and edittext should hide
and when I press back again,
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE) {
super.onBackPressed();
return;
}
title.setText(null);
userinput.setText(null);
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
Do like this.
#Override
public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
{
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
else
{
finish();
}
}
Hop it will do what you want.
Change the code to below
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
super.onBackPressed();
}
Related
I am new to coding.I want to code
in order to go to second activity using a button,
an editText and a pasdword in case it maches.
But it does n`t work.Please help me.Following is my code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eText= (EditText)findViewById(R.id.mainEditText1);
String myCode=eText.getText().toString();
Button nextPage= (Button)findViewById(R.id.mainButton1);
if(myCode.equals("Titan")){
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
startActivity(new Intent(MainActivity.
this,actor.class));
}
});
}
}
}
Move your condition and getText() call in listener
nextPage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan")){
startActivity(new Intent(MainActivity.
this,actor.class));
}
}
}
So, here's what's happening:
You have an EditText field in your app, where the user types their password.
You capture that EditText in your code using EditText eText = ...
You then check to see if eText.equals("Titan");
If it matches Titan, you see the onClick listener.
But, when this code is ran, the EditText is always empty, so you never set the onClick listener!
The fix is fairly straightforward:
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
String myCode=eText.getText().toString();
if(myCode.equals("Titan")) {
startActivity(new Intent(MainActivity.this,actor.class));
}
}
});
Now, when the button is clicked:
You capture the value of eText as myCode.
You check if myCode is equal to Titan
If true (ie it matches), you then run startActivity(new Intent(MainActivity.this,actor.class));
You have written class name actor.class starting with a smaller case. Make it starts with uppercase Actor.class. Bellow is the answer to your questions.
Button nextPage = (Button)findViewById(R.id.mainButton1);
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan"))
{
startActivity(new Intent(MainActivity.
this,Actor.class));
}else{
//password not matching
}
});
It's just a simple text with two buttons that creates toasts and change the text. I'm very new to this AS so I really don't know how to even attempt to fix this..
Any help is very appreciated...
MainActivity.java:
package com.example.david.davidisawesome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Started.");
final TextView firstText = (TextView) findViewById(R.id.firstText);
Button firstButton = (Button) findViewById(R.id.firstBtn);
Button secondButton = (Button) findViewById(R.id.secondBtn);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: First Button Clicked.");
toastMessage("You Clicked the first button");
firstText.setText("Nice Job.");
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Second Button Clicked.");
toastMessage("You Clicked the second button");
firstText.setText("Good Effort.");
}
});
}
private void toastMessage(String message){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
In picture:
activity_main.xml:
Error:
It is not finding your xml elements. The error shows that you have a null exception on setting the onclick interface to the button. So start by confirming that you are inflating the proper xml and finding the proper id.
More specifically your secondBtn is not found.
I used below code to close app after back button pressed. Some time ago it worked but I tried to use it again and have:
Error:(88, 13) error: class, interface, or enum expected.
If I remove this code app can be build, I don't see where is problem?
Here is the MainActivity where onBackPressed is implemented:
package com.example.chab.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.a);
ImageView image1 = (ImageView) findViewById(R.id.b);
ImageView image2 = (ImageView) findViewById(R.id.c);
ImageView image3 = (ImageView) findViewById(R.id.d);
ImageView image4 = (ImageView) findViewById(R.id.e);
ImageView image5 = (ImageView) findViewById(R.id.f);
ImageView image6 = (ImageView) findViewById(R.id.g);
Picasso.with(this).load("http:/1.jpeg").into(image);
Picasso.with(this).load("http://1.jpeg").into(image1);
Picasso.with(this).load("http://1.jpeg").into(image2);
Picasso.with(this).load("http://1.jpeg").into(image3);
Picasso.with(this).load("http://1.jpeg").into(image4);
Picasso.with(this).load("http://1.jpeg").into(image5);
Picasso.with(this).load("http://1.jpeg").into(image6);
Button btnOne = (Button) findViewById(R.id.Btn);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Activitydwa.class);
startActivity(intent);
}
});
}
} //THIS BRACKET MUST BE MOVED TO THE END OF CODE!
private Boolean exit = false;
#Override
private void super.onBackPressed() {
if (exit) {
this.finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
EDIT:
Solved.
Bracket before Boollean must be moved to the end of code. Then all works. Thank you.
Your first problem is that you have your method implemented outside of the class. In java, methods need to belong to a class, interface, or enum. Double check your brackets and move your method to the inside of your class brackets. Your second problem is that you have the wrong signature for the onBackPressed method. See the code below:
This is what you have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
}
private Boolean exit = false;
#Override
private void super.onBackPressed() {
// ...
}
This is what you need:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
private Boolean exit = false;
#Override
public void onBackPressed() {
// ...
}
}
Try to put
super.onBackPressed()
before finish()
Try this.finish() OR
Try super.onBackPressed(); OR
call NavUtils.navigateUpFromSameTask(this); to get back to previous screen/ activity
Please Give Me some Solution to add back button in quiz app
What I want to create is back button in my quiz app, the questions come from database. I want button to go to previous question without messing up.I am new to android can any one help me to solve the problem..
package in.gtarena.myquizapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import in.gtarena.myquizapp.db.DBAdapter;
import in.gtarena.myquizapp.model.Question;
public class ComputerActivity extends AppCompatActivity {
private List<Question> questionsList;
private Question currentQuestion;
private TextView txtQuestion,tvNoOfQs;
private RadioButton rbtnA, rbtnB, rbtnC,rbtnD;
private Button btnNext,btnBack;
private RadioGroup grp;
private int obtainedScore=0;
private int questionId=0;
private int answeredQsNo=0;
ArrayList<String> myAnsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_concept);
init();
btnBack = (Button) findViewById(R.id.btnBack);
grp=(RadioGroup)findViewById(R.id.radioGroup1);
//Initialize the database
final DBAdapter dbAdapter =new DBAdapter(this);
questionsList= dbAdapter.getQuestions();
currentQuestion =questionsList.get(questionId);
//Set question
setQuestionsView();
//Check and Next
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.e("Answer ID", "Selected Positioned value - "+grp.getCheckedRadioButtonId());
if(answer!=null){
Log.e("Answer", currentQuestion.getANSWER() + " -- " + answer.getText());
//Add answer to the list
myAnsList.add(""+answer.getText());
if(currentQuestion.getANSWER().equals(answer.getText())){
obtainedScore++;
Log.e("comments", "Correct Answer");
Log.d("score", "Obtained score " + obtainedScore);
}else{
Log.e("comments", "Wrong Answer");
}
if(questionId < dbAdapter.rowCount()){
currentQuestion =questionsList.get(questionId);
setQuestionsView();
}else{
Intent intent = new Intent(ComputerActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", obtainedScore);
b.putInt("totalQs", questionsList.size());
b.putStringArrayList("myAnsList", myAnsList);
intent.putExtras(b);
startActivity(intent);
finish();
}
}else{
Log.e("comments", "No Answer");
}
//Need to clear the checked item id
grp.clearCheck();
}//end onClick Method
});
//Check and Back
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT SHOULD I DO HERE FOR GO BACK TO PREVIOUS QUESTION//
}
});
}
public void init(){
tvNoOfQs=(TextView)findViewById(R.id.tvNumberOfQuestions);
txtQuestion=(TextView)findViewById(R.id.tvQuestion);
rbtnA=(RadioButton)findViewById(R.id.radio0);
rbtnB=(RadioButton)findViewById(R.id.radio1);
rbtnC=(RadioButton)findViewById(R.id.radio2);
rbtnD=(RadioButton)findViewById(R.id.radio3);
btnNext=(Button)findViewById(R.id.btnNext);
myAnsList = new ArrayList<String>();
}
private void setQuestionsView()
{
rbtnA.setChecked(false);
rbtnB.setChecked(false);
rbtnC.setChecked(false);
rbtnD.setChecked(false);
answeredQsNo=questionId+1;
tvNoOfQs.setText("Questions "+answeredQsNo+" of "+questionsList.size());
txtQuestion.setText(currentQuestion.getQUESTION());
rbtnA.setText(currentQuestion.getOptionA());
rbtnB.setText(currentQuestion.getOptionB());
rbtnC.setText(currentQuestion.getOptionC());
rbtnD.setText(currentQuestion.getOptionD());
questionId++;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Are you using separate activity for every question?
If yes, then you can use onBackPressed method.
It would be easy for you to go back to question if you provide a id(ex.1,2,3..) for every question in database and take a variable named id and increment id every time you click the next button and fetch the question matching to the id and when you press back button the decrement id and fetch the question again...
Just decrement questionid in back button listener and call the questions again
You can do this by two approaches :
Create a button and than link that button to the previous activity
Create a method onBackPressed like this code :
#Override
public void onBackPressed() {
Intent intent = new Intent(OrderList.this, MainActivity.class);
startActivity(intent);
}
You can simply use the second code in any activity to lead you back to previous activity.
Hope this helps !!
am new in android and facing a problem in edit text view need your help. I just want to check EditText before going on next intent. If EditText is filled by at least 1 String then it will go to next intent. May be this is simple for you to implement but this is to difficult for me and I wasted half day on this. Is anyone here which can tell me where is the mistake in this code.
Thank you in advance
package com.example.tricknearn;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Posttittle extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posttittle);
final EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Button postTittleButton = (Button) findViewById(R.id.postTittleButton);
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().length() == 0) {
tittleInput.setError("Please enter some text");
}
}
});
}
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().equals("")){
}else{
startActivity(i);
}
}
}
Try this code instead...
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().isEmpty()){
// Here you can place code while edit text is empty
}else{
startActivity(i);
}
}
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().toString().isEmptry()) {
//edittext is empty
tittleInput.setError("Please enter some text");
} else {
//Edittext is not empty
//start new Activity
}
}
});
And remove postTittleClick method
Replace if (tittleInput.getText().length() == 0) with if (tittleInput.getText().toString.equals(""))