How to delete individual elements from textview on Android studio - java

I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....

Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity

Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}

Related

Calling a button from a second xml file from the main .java file

I'm trying to make a text adventure game in android studio using buttons to progress through the map. I'm trying to call a button that is in the second page of the game. I know the problem is that it can't find the button since its on the second page and the page the .java file is looking at is page one. Is there a way to have the .java file look at all .xml page at the same time?
public class textAdven extends AppCompatActivity {
boolean male;
int gender;
private Button boyButt;
private Button girlButt;
private TextView genderBox1;
private Button shedButt, fieldButt, yardButt, lookAround1, cabButt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_adven);
boyButt =(Button) findViewById(R.id.boyButt);
girlButt =(Button) findViewById(R.id.girlButt);
shedButt=(Button) findViewById(R.id.shedButt);
cabButt =(Button) findViewById(R.id.cabButt);
lookAround1 =(Button) findViewById(R.id.lookAround1);
fieldButt=(Button) findViewById(R.id.fieldButt);
yardButt =(Button) findViewById(R.id.yardButt);
boyButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = true;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("boy");
}
});
girlButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = false;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("girl");
}
});
shedButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven3);
}
});
yardButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
}
});
}
}

Database is saved on click but not going to the next class when clicked

public class JobSignup extends AppCompatActivity {
private Button btn;
private ImageView imageview;
private static final String IMAGE_DIRECTORY = "/demonuts";
private int GALLERY = 1, CAMERA = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_signup);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReferenceFromUrl("https://rozgarinepal100.firebaseio.com/");
final TextView userName = (TextView) findViewById(R.id.userName);
final EditText writeName = (EditText) findViewById(R.id.writeName);
ImageButton sendDataToFirebase = (ImageButton) findViewById(R.id.sendDataToFirebase);
final TextView dateOfBirth = (TextView) findViewById(R.id.dateOfBirth);
final EditText enterDateOfBirth = (EditText) findViewById(R.id.enterDateOfBirth);
if ( writeName.getText().toString().length() == 0)
{
writeName.setError("Username Compulsory");
}
// if( TextUtils.isEmpty(writeName.getText())) {
// /**
// * You can Toast a message here that the Username is Empty
// **/
//
// writeName.setError("First name is required!");
//
// }
sendDataToFirebase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRef.child("0").child(userName.getText().toString()).setValue(writeName.getText().toString());
//myRef.child("NAME:").setValue("Kathmandu University");
myRef.child("0").child(dateOfBirth.getText().toString()).setValue(enterDateOfBirth.getText().toString());
}
});
}
public void onClick(View v) {
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
}
}
My code is running fine and database is also saved on click but on click it is not going to the next class using intent.Here in the onclick listener function i have added link for database when the button is clicked on android.Database is running fine on click operation but i am not able to go the next class which i have created.I have the only problem to go the next class when the same button is clicked.I am trying to enable the database and goto the next class using same button.
Try this, It will work...
sendDataToFirebase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRef.child("0").child(userName.getText().toString()).setValue(writeName.getText().toString());
//myRef.child("NAME:").setValue("Kathmandu University");
myRef.child("0").child(dateOfBirth.getText().toString()).setValue(enterDateOfBirth.getText().toString());
Intent i = new Intent(JobSignup.this, HomeActivity.class);
startActivity(i);
}
});

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

Disabling edittext by using a checkbox error

I have a problem here. I want to disable a edittext by using a checkbox. If checkbox is checked, edittext isa available, if not, it is disabled. But I'm having problems. Here is my code: Can anyone check on this. Accdng to eclipse, no error. But when I run it on my phone, the enabling/disabling is not functioning.
public class Order extends Activity {
private Button button1;
private EditText txtbox1,txtbox2;
private TextView tv;
CheckBox check1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
}
public void addListenerOncheck1() {
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
txtbox1.setFocusable(true);
txtbox1.setFocusableInTouchMode(true);
} else {
txtbox1.setFocusable(false);
txtbox1.setFocusableInTouchMode(false);
}
}
});
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
}
}
you are nowhere adding your Listener by calling ur method addListenerOncheck1();
so put addListenerOncheck1(); add the end of you OnCreate method.
But I highly recommend you not to use all these additional self-made Classes for just adding listeners. Use the following code instead and add it into your OnCreate Method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
txtbox1.setEnabled(false);
} else {
txtbox1.setEnabled(true);
}
}
});
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
});
// This will make sure the user can only type numbers into the EditTexts:
txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER);
txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER);
// Code to disable editTexts at start once:
txtbox1.setEnabled(false);
txtbox2.setEnabled(false);
}

How can I pass data from one view to another in android?

I have two views named: first_view and second_view.
The first view consists of a button and an editable text view. The second view consists of a single text view.
In my first view, I want to put a number in the datable text view. As I click the button, it should display the number in the second view.
How can I code Java classes for the two views?
I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.
Here is what you have to do.
public class MyActivity extends Activity {
Button btn;
EditText et;
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.yourbtnID);
et = (EditText) findViewById(R.id.yourEtID);
tv = (TextView) findViewById(R.id.yourtvID);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myText = et.getText().toString();
tv.setText(myText);
}
});
}
}
If you want to pass text between two Activities then use Intent.
In your current Activity do this.
Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);
Then in next Activity do this..
Bundle extras = getIntent().getExtras();
String myEtText;
if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}
if Two Views in the same Activity , you can do that
Button btn;
EditText txtInput;
TextView txtShow;
//btn=firstView.findViewWithTag(tag)
btn=firstView.findViewById(R.id.**);
txtInput=firstView.findViewById(R.id.**);
txtShow=secondView.findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
txtShow.setText(input);
}
});
if you have Two Activity :
Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{
btn=(Button)findViewById(R.id.**);
txtInput=(Button)findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
Intent intent=new Intent(this, AnotherActivity.Class);
intent.putExtra(VALUE_KEY, input);
}
});
}
On the AnotherActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
String value=intent.getStringExtra(VALUE_KEY);
TextView view=(TextView)findViewById(R.id.txt);
view.setText(value);
}
Try this,
put your value in String: String et_str = EditText.getText().toString();
When you call the other intent,
Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);
In The Second View, get this value using getExtra()

Categories