I have two activities (TextActivity.java and GrammarActivity.java), on which one fragment (ChoixLangue.java) is a radio group with four buttons to select a language of translation.
The parent of GrammarActivity is TextActivity.
I can select the language I want either on TextActivity or GrammarActivity. And I can change it on whichever ativity I am as well.
But if I select a language on TextActivity, then go to GrammarActivity and change the language, and then kill GrammarActivity, which takes me back to TextActivity, it displays the wrong radio button checked - presumably because the display of TextActivity is not refreshed.
How to fix this bug? Here is a part of the code in TextActivity `
public class TextActivity extends AppCompatActivity {
public ImageButton monImageBouton;
public Button monBouton;
public TextView monTextView;
public static RadioButton monBoutonRadio;
public static String langueChoisie = "francais";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
// une des solutions pour rendre la zone trado scrollable
TextView myXmlContent = (TextView)findViewById(R.id.zone_trado_scrollable);
myXmlContent.setMovementMethod(new ScrollingMovementMethod());
verifLangue();
playText();
test("button8"); // indique la langue choisie
}
public void verifLangue(){
if(langueChoisie == "english"){
monBoutonRadio = (RadioButton)findViewById(R.id.english);
monBoutonRadio.setChecked(true);
}
else if(langueChoisie == "espanol"){
monBoutonRadio = (RadioButton)findViewById(R.id.espanol);
monBoutonRadio.setChecked(true);
}
else if(langueChoisie == "chinois"){
monBoutonRadio = (RadioButton)findViewById(R.id.chinois);
monBoutonRadio.setChecked(true);
}
else{
monBoutonRadio = (RadioButton)findViewById(R.id.francais);
monBoutonRadio.setChecked(true);
}
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.english:
if (checked)
langueChoisie = "english";
break;
case R.id.francais:
if (checked)
langueChoisie = "francais"; // checked et définie a priori
break;
case R.id.espanol:
if (checked)
langueChoisie = "espanol";
break;
case R.id.chinois:
if (checked)
langueChoisie = "chinois";
break;
}
}`
I'm not sure I'm fully understood your question but the best choice would be to use eventhandlers for radio buttons which will get the selected value and store it for both classes (Grammer and Text).
(I'm assuming you want the choice you made for Grammer activity should be kept for Text activity as well.)
Related
Here is the goal: 1 textview, 1 button. after pressing button text in textview is editing to new string. I tryed to realise it like this, but it
doesn`t work. Please help. There is 1 activity, 1 button, 1 tv and multiple pressings to edit text.
public class Second extends AppCompatActivity {
TextView tv;
Button btn;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
i = 1;
tv.setText(R.string.s1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (i=1;i<28;i++);{
switch (i){
case 1:tv.setText(R.string.s1);
break;
case 2:tv.setText(R.string.s2);
break;
case 3:tv.setText(R.string.s3);
break;
case 4:tv.setText(R.string.s4);
break;
case 5:tv.setText(R.string.s5);
break;
case 6:tv.setText(R.string.s6);
break;
case 7:tv.setBackgroundResource(R.drawable.tigs);
tv.setHeight(430);
tv.setWidth(350);
tv.setText(R.string.s6_1);
break;
default:break;
What i`m doing wrong?
At the end you will only see the value of R.string.s6 in your edit text and the background will be set as R.drawable.tigs. The for loop executes in a fraction of second and set value one by one. But due to its fast execution you will only see the last value. But if you want to see all the text one by one you can use a java thread and call its sleep method by passing the time in milliseconds.
I want the user to be able to rank a list of fruits in the order that they like to most.
Say for example, consider the list as "Mango, Apple, Banana, Pineapple".
If they like Apple, Mango, Pineapple, Banana in this order I want to use checkboxes that show the rank when the user clicks on them.
Is this possible?
Thanks
Why don't you try with a counter. It start's at 0, when user press a checkbox you just add 1 to it and you have an array for each checkbox where you will save the number pressed.
You have final int[] fruits = new int[4]; and final int counter = 0;
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_mango:
if (checked) {
counter++;
fruits[0] = counter;
} else {
counter--;
fruits[0] = 0;
}
break;
case R.id.checkbox_apple:
if (checked) {
counter++;
fruits[1] = counter;
} else {
counter--;
fruits[1] = 0;
}
break;
//OTHER CASES
}
}
Yes this is possible with Checkboxes, take a look here:
http://stackoverflow.com/questions/3149414/how-to-receive-a-event-on-android-checkbox-check-change
If I understood you right, you just have to set a variable when the user clicks on one of those checkboxes like:
var number1 = 'Mango'
You should achive this with the example you can find in the link above.
i have a problem in my application, i have a formulaire whish a user should fill information and save it to database,i have both Edit Text and Radio Button :
rm_1 = (EditText) findViewById(R.id.rm_1);
rm_2 = (EditText) findViewById(R.id.rm_2);
rm_3 = (EditText) findViewById(R.id.rm_3);
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_3);
i have a method Onclick whish associate each radio buton selected with a value :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((Checkable) view).isChecked();
switch (view.getId()) {
case R.id.rm_13_1:
if (checked)
a = 0;
break;
case R.id.rm_13_2:
if (checked)
a = 1;
break;
case R.id.rm_14_1:
if (checked)
b = 0;
break;
case R.id.rm_14_2:
if (checked)
b = 1;
break;
case R.id.rm_14_3:
if (checked)
b = 2;
break;
case R.id.rm_14_4:
if (checked)
b = 3;
break;
}
until now everything works fine, the user writes in the edit text and select the radio button , and in database i find the same information.
in order to save the data entered by the user i did used shared preferences, so the text writing by the user and radio button selected appear again when the user returns to the activity.
That's when the issue occurs when a user change the activity , so if he returns to the activity he finds the radio button already selected but when he click on save button the value he gets in database is zero, it is like the methode :
public void onRadioButtonClicked(View view)
is not working, i don't know why ?? the user need to click again on radio button to have the values assigned in the method onRadioButtonClicked(View view), so how to solve that ?
this is where i save data :
Button bton = (Button) findViewById(R.id.ajoutUn);
bton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ajouter(v);
}
public void ajouter(View v) {
db.open();
db.insertMENAGE(rm_1ts, rm_2ts, rm_3ts, rm_4ts, rm_5ts,
rm_6ts, rm_7ts, rm_8ts, rm_9ts, rm_10ts, rm_11ts,
a, b, rm_14_4_autrets, rm_15ts);}}
And the method in database is :
public long insertMENAGE(String Region, String Provence_prefecture , String Commune_Arrondissement ,String N_district, String N_M_district , String N_menage_logement, String Adresse_menage , String Nom_Enqueteur, String code_enquêteur , String Date_realisation_enquête, String Nom_controleur , String Date_controle, int echantillon_principal, int Statut_enquêté , String autre, String Observations ) {
ContentValues initialValues = new ContentValues();
initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement);
initialValues.put(col_N_district,N_district);
initialValues.put(col_N_M_district,N_M_district);
initialValues.put(col_N_menage_logement,N_menage_logement);
initialValues.put(col_Adresse_menage,Adresse_menage);
initialValues.put(col_Nom_Enqueteur,Nom_Enqueteur);
initialValues.put(col_code_enquêteur ,code_enquêteur);
initialValues.put(col_Date_realisation_enquête,Date_realisation_enquête);
initialValues.put(col_Nom_controleur,Nom_controleur);
initialValues.put(col_Date_controle,Date_controle);
initialValues.put(col_echantillon_principal,echantillon_principal);
initialValues.put(col_Statut_enquêté,Statut_enquêté);
initialValues.put(col_Observations,Observations);
return db.insertOrThrow(MENAGE,null, initialValues);
}
Check all your radio button values at your onResume() method like following, and assigning values to your variable.
if(rm_13_1.isChecked())
{
}
else if(rm_13_2.isChecked())
{
}
else if(rm_14_1.isChecked())
{
}
else if(rm_14_2.isChecked())
{
}
else if(rm_14_3.isChecked())
{
}
Hope it will work
First of all I'm posting this an answer because the text is too large.
Either there's some meaningful code that we're not seeing, or there's a problem understanding a few things.
The method onRadioButtonClicked(View view) is only called when the user clicks the radioButton. When the user comes back from another Activity the button is already selected. This is correct, and this is how it should behave. As far as I can see, this code only affects variables 'a' and 'b'.
On the other hand you say that you have a separate 'save' button. When the user comes back from another Acivity, variables 'a' and 'b' should preserve their values, and the results of your 'save' code should work.
I know this is not an answer, just a few insights that hopefully help you in some way.
I am working on a board game app (similar to chess). I have the activity GameBoardActivity which listens for clicks on a GridView, and at each click calls functions from a class Game to handle what should occur.
Within the class Game is the data about where pieces are and the method Move(int xFrom, int yFrom, int xTo, int yTo) where piece movement is handled.
For certain movements that a user may specify (e.g. that the piece at xFrom, yFrom should go to xTo, yTo) I want to provide them with a choice between two options. You can imagine that one choice is to go there normally, and the other is to go there as a transformed piece. To do this I want to display a custom dialog that presents the two choices for the user to click.
My custom Dialog class is given below:
public class CustomDialog extends Dialog implements View.OnClickListener{
Context mcontext;
Button button1;
Button button2;
int choice; //holds value of user's choice
public CustomDialog(Context context) {
super(context);
mcontext = context;
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
choice = 0; //no choice yet
}
public void setLayout(){
this.setContentView(R.layout.custom_dialog);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
choice = 1;
break;
case R.id.button2:
choice = 2;
break;
}
dismiss();
}
}
What I'm not clear on is how to pass the information about the user's choice back to the class Game.
Any help is greatly appreciated.
Save a reference to parent Activity via the constructor of the dialog:
private final MyActivity mCaller;
public CustomDialog(MyActivity caller) {
super(caller);
mCaller = caller;
//.......
}
Pass values to calling activity by invoking its methods:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
mCaller.setChoice(1);
break;
case R.id.button2:
mCaller.setChoice(2);
break;
}
dismiss();
}
Create a bean class for storing the button actions ....
While clicking the events store it in a bean
In a game class u access the action values for button from the bean class
I have a bunch of buttons to listen to.
Each button increments or decrements a value in an EditText.
Since every Button changes a different EditText I'm left with just switching.
There are quite a lot of buttons. Some buttons do the same thing except they are hidden according to user preference.
For every button in XML I did android:onClick="plusEditTexts", or android:onClick="minEditTexts" of course.
public void plusEditTexts(View v) {
switch(v.getId()) {
case R.id.firstButton1Inc:
case R.id.secondButton1Inc:
// EditText1++
break;
case R.id.firstButton2Dec:
case R.id.secondButton2Dec:
// EditText2++
break;
...
}
}
public void minEditTexts(View v) {
switch(v.getId()) {
case R.id.firstButton1Inc:
case R.id.secondButton1Inc:
// EditText1--
break;
case R.id.firstButton2Dec:
case R.id.secondButton2Dec:
// EditText2--
break;
...
}
}
I was wondering if this is the best way?
There are quite a lot of buttons. So the switch statement gets huge.
Cheers,
Daan
You could set attribute tagof your buttons with the id of the TextViewto be modified. Then, you can retrieve the int value of TextView resource id like this.-
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
Getting rid of the whole case sentences, so your code looked like.-
public void plusEditTexts(View v) {
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
EditText editText = (EditText) findViewById(textResId);
// Do your EditText stuff
}
public void minEditTexts(View v) {
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
EditText editText = (EditText) findViewById(textResId);
// Do your EditText stuff
}