I use Android Studio.
I have two editText named: E1, E2 and three buttons named: btn1, btn2, btn3
When I press button, it would insert some word in editText.
For example: When I press btn1, it would insert "cat" in edittext.
But now, I don't know which edittext does student want to insert. How I can detect cursor?
I hope when I detect cursor, I know student which edittext will be insert
this is my code:
private Button.OnClickListener btn=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
s=s+"cat";
E1.setText(s);
E2.setText(s);
//I dont know whether the student want to insert E1 or E2
// how can I do,thank;
break;
case R.id.btn2:
s=s+"apple";
E1.setText(s);
E2.setText(s);
//same problem .....
break;
case R.id.btn3:
s=s+"dog";
E1.setText(s);
break;
}
}
};
Thank.
check the focus of EditText:
if(EditText1.isFocused()){
//EditText1 is focused
}else if(EditText2.isFocused()){
//EditText2 is focused
}
Related
I want visible button has removed it text from TextView
i am making keyboard typing in text view I have 4 buttons b1,b2,b3,backspace.
when I clicking on b1 typing in textview "A" and b1 invisible
then when I clicking backspace button its remove last char but does not appear last button has clicked
my project
https://youtu.be/_pLEUevM8aA
java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//my buttons
Button b1,b2,b3,backspace;
//my text view
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txt);
b1 = (Button)findViewById(R.id.b1);
b2 = (Button)findViewById(R.id.b2);
b3 = (Button)findViewById(R.id.b3);
backspace = (Button)findViewById(R.id.backspace);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
backspace.setOnClickListener(this);
}
//on clike listener
#Override
public void onClick(View view) {
switch (view.getId()){
//b1 on click
case R.id.b1:
txt.setText(txt.getText().toString() + b1.getText());
b1.setVisibility(View.INVISIBLE);
break;
//b2 on click
case R.id.b2:
txt.setText(txt.getText().toString() + b2.getText());
b2.setVisibility(View.INVISIBLE);
break;
//b3 on click
case R.id.b3:
txt.setText(txt.getText().toString() + b3.getText());
b3.setVisibility(View.INVISIBLE);
break;
//here is my problem last char has deleted button of that char must be get visible
//backspace on click
case R.id.backspace:
//I want visible button has removed it text from TextView
//appear last button has clicked
txt.setText(txt.getText().toString().substring(0,txt.getText().toString().length()-1));
break;
}//end switch
} //I hope get answer in this website
}
//thank for ALL helping
You could create a Stack of pressed buttons, so that you would know in which order they are pressed:
public class MainActivity extends ... implements ...{
Stack<View> pressedButtons = new Stack<>();
...
#Override
public void onClick(View view) {
// If it is a number, not a backspace key - remember that we pressed it
if(view.getId() != R.id.backspace) {
pressedButtons.push(view);
txt.setText(txt.getText().toString() + ((Button) view).getText());
view.setVisibility(View.INVISIBLE);
}
else { // backspace key
String text = txt.getText().toString();
if(text.isEmpty())
return; // do this to prevent crash
txt.setText(text.substring(0, text.length() - 1));
// make the button visible again
View lastPressedButton = pressedButtons.pop();
lastPressedButton.setVisibility(View.VISIBLE);
}
}
}
You can try this:
case R.id.backspace:
//try like this
if(string.substring(string.length() - 1).equals(b3.getText()))
b3.setVisibility(View.VISIBLE);
...
txt.setText(txt.getText().toString().substring(0,txt.getText().toString().length()-1));
I am creating a small quiz app. As the question suggests, how do I prevent a second click without disabling the button? When I click twice on a radio button it adds 2 points instead of 1. Very much appreciated!
Here's a picture of my app:
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
scoreForRadioButtons += 1;
}
case R.id.question1_8:
if (checked) {
break;
}
case R.id.question1_7:
if (checked) {
break;
}
}
}
SOLVED
This is what I did: just add 'break;' on the correct answer.
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
pointForQ1 = 1;
break;
}
case R.id.question1_8:
if (checked) {
pointForQ1 = 0;
}
case R.id.question1_7:
if (checked) {
pointForQ1 = 0;
}
}
}
Short Answer:
You can set the clickable attribute to false for the RadioButton after it is clicked if you really want to using the following:
myRadioButton.setClickable(false);
Long Answer:
Don't use radio buttons for this. Users should be able to click a radio button as many times as they want without anything happening like incrementing the score in your case. It's a standard convention of a radio button that once selected, no new code is executed if it is selected again and again. Note this is only if the state of the radio button hasn't changed during selections (state meaning whether it's selected or not).
The way your app is using (or wants to use) RadioButtons is not correct. I'd recommend using buttons for this, where you could do the following:
myButton.setEnabled(false);
Edit:
If you want to make it visible to the user that they clicked a button, you can do a few things (change it's text, change the background colour). For this example, you can change the background colour with this:
myButton.setBackgroundColor(Color.GREEN); // or whatever colour you choose
If you want to do this add the following to your imports:
import android.graphics.Color
If you just don't want to click that radio button two times, After radio button is clicked,
add view.setEnabled(false);or
view.setClickable(false); to prevent it click again
To prevent the second click on a checked radio button, make that radio disabled
radioButton.setEnabled(false);
Disable a clicked button and enable it when another is checked
public void question1 (View view) {
//will enable all buttons
Integer[] buttons = {R.id.question1_9, R.id.question1_8, R.id.question1_7};
for (int i = 0; i < buttons.length; i++) {
findViewById(buttons[i]).setEnabled(true);
}
RadioButton button = (RadioButton) view;
if (button.isChecked()){
switch (view.getId()) {
case R.id.question1_9:
scoreForRadioButtons ++;
//will disable clicked button
button.setEnabled(false);
break;
case R.id.question1_8:
break;
case R.id.question1_7:
break;
}
}
I have 4 textviews, out of 4 textviews, I have to select anyone from the 4 textviews, and also I have to change the background color(green) of the textview at the same time to highlight it.
Every textview uses custom drawable background.
This is textview xml:
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="#dimen/_55sdp"
style="#style/ChoosePlan"
android:background="#drawable/choose_plan_bg"
android:text="#string/part_time_monthly"
/>
That means
setBackground(getResources().getDrawable(R.color.green));
will not work.
So I tried like this:
private void changeColor()
{
Drawable tempDrawable = getResources().getDrawable(R.drawable.choose_plan_bg);
LayerDrawable bubble = (LayerDrawable) tempDrawable; //(cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(getResources().getColor(R.color.green));
textview1.setBackground(tempDrawable);
textview1.setTextColor(getResources().getColor(R.color.white));
}
and onClick Event I call the above method:
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_partTimeMonthly:
changeColor();
checkDoneBtnVisibile();
break;
case R.id.tv_partTimeAnnually:
checkDoneBtnVisibile();
break;
case R.id.tv_fullTimeMonthly:
checkDoneBtnVisibile();
break;
case R.id.tv_fullTimeAnually:
checkDoneBtnVisibile();
break;
case R.id.iv_back_button:
finish();
break;
}
}
P.S: Have to use Textview(No ImageView)
I got stuck to it. Can you please help me out to move me forward.
Thanks in Advance
Sharing the Screenshot:
You are only updating textview1 in your changeColour() method. If you just want to highlight 'only' one textView that is pressed at any time, then you may try something like the following.
First, I made a method that takes a textView as its argument that changes its background back to choose_plan_bg. Specify wherever your background is located in drawable.
private void changeColorBack(TextView textView)
{
textView.setBackground(getResources().getDrawable(R.drawable.choose_plan_bg))
}
Similarly, the changeColour method:
private void changeColor(TextView textView)
{
Drawable tempDrawable = getResources().getDrawable(R.drawable.choose_plan_bg);
LayerDrawable bubble = (LayerDrawable) tempDrawable; //(cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(getResources().getColor(R.color.green));
textView.setBackground(tempDrawable);
textView.setTextColor(getResources().getColor(R.color.white));
}
Next, create a distinct OnClickListener and set the textViews to this listener:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
changeColorBack(textView1);
changeColorBack(textView2);
changeColorBack(textView3);
changeColorBack(textView4);
switch (v.getId()) {
case R.id.textView1:
changeColor(textView1);
break;
case R.id.textView2:
changeColor(textView2);
break;
case R.id.textView3:
changeColor(textView3);
break;
case R.id.textView4:
changeColor(textView4);
}
}
};
textView1.setOnClickListener(listener);
textView2.setOnClickListener(listener);
textView3.setOnClickListener(listener);
textView4.setOnClickListener(listener);
This selects only the textView that is pressed and unselects anyone other textView.
i am developing/designing a quiz application in android, on the testActivity i have
a TextView which loads the questions and four answer(alternatives) buttons.
btnAnswer1, btnAnswer2, btnAnswer3, and btnAnswer4.
Problem:When i click on btnAnser1 as my answer, it should remain in selected state, however it should also be possible to unselect(normal state) it if i doubt the answer. and lastly if its selected, and i decide to go for btnAnswer2 whiles btnAnswer1 is selected, immediately i select btnAnswer2, btnAnswer1 should be unselected(normal) whiles btnAnswer2 is selected.
i hope my question is clear and it sounds quiet easy but am still new in android programming so will appreciate the help.
thanks.
optionone = (Button) findViewById(R.id.optionone);
optionone.setTag(1);
optionone.setId(i);
//optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
optionone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setSelected(true);
checkAnswer();
final int change = (Integer) v.getTag();
if (change == 1) {
optionone.setBackgroundColor(Color.parseColor("#B2B2B2"));
back.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
v.setTag(0);
} else {
optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
back.setVisibility(View.GONE);
next.setVisibility(View.GONE);
v.setTag(1);
}
}
});
Try to use toggle buttons instead of buttons, or just change background color of buttons when they pressed, not state.
I have some buttons and I have some text set ot them and I need that text to be visible when activity starts. After that I set some text to all my buttons from sqlite database, but I don't want that text to be visible until user clicks of a button.
The simplest solution would be to use button.setText(c.getString(int)); and set that text from db when a button is clicked, but my c cursor is not in activity scope so I can't use it in my onClick method of my buttons. I tried to place everything inside of my nextQuestion() method in activity scope, but that gives me errors.
So I was thinking I should hide all text set from db until a button is pressed, that unhide it. How to do that? Or if first idea is possible somehow it's even better.
Here's my game class:
public class Game extends Activity implements View.OnClickListener{
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
MediaPlayer buttonClicks;
public boolean music;
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
addListenerOnButton();
nextQuestion();
}
private void addListenerOnButton() {
buttonClicks = MediaPlayer.create(this, R.raw.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
private void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
b1.setText(c.getString(2));
b2.setText(c.getString(3));
b3.setText(c.getString(4));
b4.setText(c.getString(5));
b5.setText(c.getString(6));
b6.setText(c.getString(7));
b7.setText(c.getString(8));
b8.setText(c.getString(9));
b9.setText(c.getString(10));
}
finally{
mDbHelper.close();
}
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
if(music == true){
buttonClicks.start();
}
b1.setText(c.getString(2));// this is a good option, but can't use cursor
b2.setEnabled(true);
break;
case R.id.button2:
if(music == true){
buttonClicks.start();
}
b3.setEnabled(true);
break;
case R.id.button3:
if(music == true){
buttonClicks.start();
}
break;
case R.id.button4:
if(music == true){
buttonClicks.start();
}
}
}
}
There is no builtin method to hide only the text on a button, but you could simply store the values from the database in a bunch of variables and not set those against the buttons until they're pressed.
That means you should change:
b1.setText(c.getString(2));
b2.setText(c.getString(3));
...
b9.setText(c.getString(10));
To something like:
String mButtonText1, mButtonText2, ... mButtonText9; // <-- class members
mButtonText1 = c.getString(2);
mButtonText2 = c.getString(3);
...
mButtonText9 = c.getString(10);
(alternatively, you could put these values in an array)
And then in onClick():
b1.setText(mButtonText1);
To 'hide' the text on the button, simply call setText() with "" or null.
You may be able to achieve the same effect by setting the button's text color to 'transparent', but I haven't tried that.
How about passing the String-s to some intermediate storage in some variables and then, at appropriate time, setting the Buttons' texts from those strings?
You're trying to find a solution for a symptom of a larger, underlying problem. If you're having difficulties sharing data between activities, you certainly shouldn't be heading in this direction.
That being said, to answer your specific question, you could set textColor to android.R.color.transparent.
I think the best way is:
Setting text null or empty and in your java implementation, you set text.
Like this:
teste.xml:
<Button android:id="#+button/testebutton" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Teste.java:
Button testeButton = (Button) findViewById(R.button.testebutton);
testeButton.setText("NEW TITLE");
How about making the cursor an instance variable (of the class) instead of method-local variable? And maybe using http://en.wikipedia.org/wiki/Lazy_initialization ...