How to realise multiple text editing from one button? - android

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.

Related

Refresh the display of an activity when it is visible again

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.)

Android 1 function for 3 buttons

I have 1 function to print the text of the button, and i use that function for the 3 button, how do i know in what button the user click? (sorry but my english is low)
Example:
public void function(View view) {
TextView text = (TextView) findViewById(R.id.textView);
Button button1= (Button) findViewById(R.id.button3);
Button button1= (Button) findViewById(R.id.button4);
Button button1= (Button) findViewById(R.id.button5);
text.setText(button <?> .getText()); }
is the number of the button the user click
Its any way to know in which button the user click?
Please help, Thanks.
you can define one click listener and set this listener for all buttons. For example:
private OnClickListener genericListerner = new OnClickListener() {
public void Click(View v) {
switch(v.getId())
{
case R.id.button3:
text.setText(button3.getText().toString());
break;
case R.id.button4:
text.setText(button4.getText().toString());
break;
case R.id.button5:
text.setText(button5.getText().toString());
break;
default:
//do something
}
};
and you set the listeners like the following:
button3.setOnClickListener(genericListener);
button4.setOnClickListener(genericListener);
button5.setOnClickListener(genericListener);
I am not sure whether this is what you are asking, but what this code basically does is that, if you click button 3 it sets the textview's text to button 3's text, if you click button 4 it sets the textview's text to button 4's text and if you click button 5, it sets the textview's text to button 5's text.
Hope this helps.
The view that is passed in is the one that was clicked, therefore it has the id.
view.getId() == R.id.button3
If you still need help, share more of your code.
Well you will require some onclick listeners for the button, you can then set an id or name in the onclick e.g
final Button button1 = (Button) findViewById(R.id.button_id);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.settext("Button1 Clicked")
}
});

Dynamically populate a button value android

I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}

android multiply button case switch

I have been looking at some posts and still I cannot get mine code to work (I am a beginner) .. I am just tring to use the toast with my two buttons with a case switch .. When compiling it just crashes .. one something has an idea ?
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
TextView et = (TextView) findViewById(R.id.txtHeader);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnDis = (Button) findViewById(R.id.btnDisplay);
btnAdd.setOnClickListener((OnClickListener) this);
btnDis.setOnClickListener((OnClickListener) this);
}
public void OnClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
// Toast msg = Toast.makeText(getBaseContext(), "Torben", Toast.LENGTH_LONG);
// msg.show();
break;
case R.id.btnDisplay:
// Toast msg1 = Toast.makeText(getBaseContext(), "Henriksen", Toast.LENGTH_LONG);
// msg1.show();
break;
default:
break;
}
}
I see two main problems:
((OnClickListener) this
Make sure your class implements OnClickListener because you never need to cast if you are actually implementing the interface.'
The declaration of the class should be something like:
public class MyActivity extends Activity implements OnClickListener
Then change OnClick to lowercase o.
#Override
public void onClick(View v) {
some log output would be helpful!
a wild guess is that your activiy does not implement OnClickListener, why else would you cast this to OnClickListener?
just check in your layout manifest that whether the buttons id are correct and given the same id which your are using and if it is then please update the question with the LogCat output.
And also check that the activity is defined in manifest because there is no mistake in your code to implemts the onclick listener for multiple buttons.
Enjoy!!
Example adding a button listener:
Button b = ((Button)findViewById(R.id.button_name));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
and make sure the button is defined in your xml file with the id #+id/button_name or #id/button_name (just make sure they match)

Android, tell me my mistake please

I am very new to Java as well as Android programming. I was attempting to make a calculator. But as soon as I inserted the square root button, the application crashes whenever I hit divide or square root. I don't know where I'm going wrong. Help,please?
public class MainScreen extends Activity implements OnClickListener {
EditText edit1, edit2;
TextView txt;
Button butt, diff, mul, div, sqr, per;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText)findViewById(R.id.editText1);
edit2 = (EditText)findViewById(R.id.editText2);
txt = (TextView)findViewById(R.id.textView1);
butt = (Button)findViewById(R.id.button1);
butt.setOnClickListener(this);
diff = (Button)findViewById(R.id.button2);
diff.setOnClickListener(this);
mul = (Button)findViewById(R.id.button3);
mul.setOnClickListener(this);
div = (Button)findViewById(R.id.button4);
div.setOnClickListener(this);
sqr = (Button)findViewById(R.id.button5);
sqr.setOnClickListener(this);
per = (Button)findViewById(R.id.button6);
per.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String a;
// TODO Auto-generated method stub
String b;
Integer sum, sub, mul, div;
Double x ;
a= edit1.getText().toString();
b= edit2.getText().toString();
x=(double) Integer.parseInt(a);
switch (v.getId()) {
case R.id.button1:
sum = Integer.parseInt(a)+ Integer.parseInt(b);
txt.setText(sum.toString());
break;
case R.id.button2:
sub = Integer.parseInt(a)- Integer.parseInt(b);
txt.setText(sub.toString());
break;
case R.id.button3:
mul = Integer.parseInt(a)* Integer.parseInt(b);
txt.setText(mul.toString());
break;
case R.id.button4:
div = (Integer.parseInt(a))/(Integer.parseInt(b));
txt.setText(div.toString());
case R.id.button5:
txt.setText((int) Math.sqrt(x));
default:
break;
}}}
It's bad idea to use (int) Math.sqrt(x) - sqrt does not return integer.
When you write textView.setText(int) - Andrid thinks, that you provide id of string resource, and it can't find it and crashes.
Use this:
case R.id.button5:
txt.setText(String.valueOf(Math.sqrt(x)));
break;
You need to be checking the value of be to make sure you are not dividing by 0.
Can you please post the error you are receiving?
Well, you can view the errors doing $ adb logcat. Further, in the last two 'cases' you haven't put a "break".

Categories

Resources