Button to change text to a string from an array - android

simple question what do i need to add to this code so that when the button is pressed it replaces the text view with one of the strings from the array list?
package com.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
tx.setText("Hello");
}
});
}
}
-----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">Ihavenever</string>
<string-array
name="list">
<item>#string/girl</item>
<item>#string/boy</item>
<item>#string/man</item>
<item>#string/women</item>
<item>#string/dog</item>
<item>#string/cat</item>
</string-array>
</resources>
so in a nutshell the app launches there is a button at the bottom once pushed it puts one of the items in the string array in the textview in the middle of screen and when you push it again it gives you another one and another one till there is none left and then restarts from the first one ?

Make
int counter = 0;
global (outside onCreate()).
Then in onCreate() after declaring your button:
Button b = (Button) findViewById(R.id.button1);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.list); //get the array
((TextView) findViewById(R.id.textView2)).setText (list [counter]); //set the initial message.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
counter++;
if (counter >= list.length)
counter = 0;
tx.setText(list [counter]); //set the new message.
}
});

Related

Coding a calculator

I'm just a beginner in coding using Android studio and I have a couple of questions in coding a calculator (The calculator is used to only compute carbon emissions so it varies a little bit compared to an actual calculator):
How do I have a clear all button?
How do I add all of the
products that the user has calculated and show its sum? (Kind of like
how those tracking expenses kind of app)
This is my code so far:
package com.carschoolingpromo.scires.carschooling;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button caremissionscheck = findViewById(R.id.checkcar);
caremissionscheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent tocarbonemissions = new Intent(Calculator.this, CarbonEmissions.class);
startActivity(tocarbonemissions);
}
});
Button distancecheck = findViewById(R.id.checkdestination);
distancecheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent checkdistance = new Intent(Calculator.this, DistancesList.class);
startActivity(checkdistance);
}
});
final Button multiply =(Button)findViewById(R.id.multiply);
final EditText num1 =(EditText)findViewById(R.id.carboninput);
final EditText num2 =(EditText)findViewById(R.id.distanceinput);
final TextView ans =(TextView)findViewById(R.id.answer);
final TextView carbontotal=(TextView)findViewById(R.id.sumofcarbon);
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
ans.setText(String.valueOf(n1*n2));
}
});
}
}
How do I have a clear all button?
You need to create a new button in the layout file, and get a reference of it in your code, then set a click listener on that reference. Then in the onClick() implementation you will clear your 2 edit texts and 2 text views, something like this:
final Button clearAll =(Button)findViewById(R.id.clear_all);
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1.setText("");
num2.setText("");
ans.setText("");
carbontotal.setText("");
}
});
How do I add all of the products that the user has calculated and show its sum? (Kind of like how those tracking expenses kind of app)
For this you need to use some sort of a way to show a list on your UI, I suggest you look into this tutorial by google to creating lists

Replace string text?

I'm trying to develop an app that consists of multiple arrays and textviews and I've run into a problem.
I have 3 textviews and 2 arrays.
One of my arrays contains sentences.
My first question is how can I highlight specific words in each sentence?
Ex: "This is my first array item"
I need to highlight a word in the string so that when it is displayed in textview1, it will appear like this...
"This is my first array item"
My other array contains words. They are displayed in textview2 and should also be highlighted.
My next question is how can I replace the current highlighted word in textview1 with the new highlighted word from textview2, and also have a 3rd textview displaying the new sentence?
Ex: If the chosen item from my sentence array is "This is a test" (displayed in textview1) And the item from my word array is "website" (displayed in textview2) the result would be "This is a website" (displayed in textview3)
Here is my code so far
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;
public class Class1 extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class1);
Button btn_sentence = (Button) findViewById(R.id.btn_sentence );
Button btn_word = (Button) findViewById(R.id.btn_word );
Button btn_newsentence = (Button) findViewById(R.id.btn_newsentence );
final Random ran = new Random();
final Resources res = getResources();
final TextView text1 = (TextView) findViewById(R.id.sentencetext);
final TextView text2 = (TextView) findViewById(R.id.wordtext);
final TextView text3 = (TextView) findViewById(R.id.newsentencetext);
btn_sentence.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] NewText = null;
NewText = res.getStringArray(R.array.sentences);
String strRandom = NewText[ran.nextInt(NewText.length)];
text1.setText(strRandom);
}
});
btn_word.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] NewText2 = null;
NewText2 = res.getStringArray(R.array.words);
String strRandom2 = NewText2[ran.nextInt(NewText2.length)];
text2.setText(strRandom2);
}
});
btn_newsentence.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
**text3.setText(" " + (text1.getText()) + " " + (text2.getText()) + " ");**
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}}
The bold text is as close as I've gotten, but all that really does is combine both TexttView1 and textview2, and display it in textview3... which isn't exactly what I want.
Help would be much appreciated
~TylerNormal
You can use simple HTML-tags, wrap your words you want to highlight into those using this:
TextView text = findViewByID(R.id.myTextView);
String test = "This is my first array item";
// example - the word in brackets is to be replaced and captured
// it is just an example to show how you can use it later with more
// complex cases
test = test.replaceAll("(first)", "<b>$1</b>");
text.setText(Html.fromHtml(test));

Android Apptitude practice app

I want to develop an aptitude app..
for that in my text view i have to display first question .. On clicking next button i have to display second question.. on again clicking same next button third question have to be displayed.. liked that i want to display some 30 questions ..all questions should be displayed in single java file.I Tried to display two questions . but for multiple questions i could not find the code..
package com.example.asl;
import java.util.Arrays;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Aptitude extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
final TextView tv=(TextView) findViewById(R.id.textView1);
final String Question[]={"what is UR Name","What is ur Age","Whats ur Qualification"};
Button btnNext = (Button) findViewById(R.id.button1);
final TextView cumulos = (TextView) findViewById(R.id.textView1);
//TextView respostas = (TextView)findViewById(R.id.respostas);
Random randPhrase = new Random();
final String[] cum = {"what is UR Name","What is ur Age","Whats ur Qualification"};
//String[] resp = getResources().getStringArray(R.array.resp_cumulos);
String textout = "";
String textresp = "";
//Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);
btnNext.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
int i = 0;
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
}
}
});
//btnPrevious.setOnClickListener(new OnClickListener(){
//public void onClick(View arg0) {
//if(i>0){
// i-=1;
// cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
// }
// }
//});
}
}
enter code here
Initializing your counter in your onClick() is always going to reset it
Initialize it outside of onClick() and increment it in onClick() as you are.
public class Aptitude extends Activity {
int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
...
}
public void onClick(View arg0) { // rename arg0 to something meaningful
// like v or view for readibility
// int i = 0; remove this guy
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
If this doesn't fix your problem then please explain what the problem is but I'm sure this part was causing you trouble.

Android with Eclipse Buttons

I'm just starting out with the Android/Eclipse SDK and I have no previous Java experience.
I've seen lots of tutorials for running Toasts onclick but I'm trying to make it so that when a button is clicked a text field is populated with the text of that button.
In other words if I press a button which is labelled as 'Hello' then the contents of a textfield will become 'Hello'.
Any help is greatly appreciated.
TextView mText=(TextView)findViewById(R.id.textview1);
Button mbutton=(Button)findViewById(R.id.button1);
mbutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mText.setText("Hello");
}
});
Hope i may works..
Wrapping it up. You must have a "button1" and a "textview1" defined in your main.xml.
package my.dummy.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class HelloActivity extends Activity implements OnClickListener {
Button b=null;
TextView tv=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1); // button1 set in main.xml
b.setOnClickListener(this);
tv=(TextView)findViewById(R.id.textview1); // textview1 set in main.xml
}
public void onClick( View v ) {
if (v == b) {
tv.setText( b.getText() );
}
}
}
write code in onClick()
String text = view.getText();
// use toast here to display text..
Suppose your button's id is button1, and textview's id is textview1
Button My_Button=(Button)findViewById(R.id.button1);
TextView textView = (TextView)findViewById(R.id.textview1);
My_Button.setOnClickListener(new OnClickListener(){
public void onclick(View v){
textView.setText("hello");
}
);

How can I keep my Java Android code short?

Hi I am developing an Android app using an EditText, and a add en minus button to control it's value.
I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.
Anyone an idea how to repeat the same code for multiple values?
This is my current code:
package com.lars.MyApp;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAppActivity extends Activity {
int currentValue = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText Value = (EditText) findViewById(R.id.Value);
Button addButton = (Button) findViewById(R.id.addButton);
Button minusButton = (Button) findViewById(R.id.minusButton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addValue();
Value.setText("" + currentValue);
}
});
minusButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
minusValue();
Value.setText("" + currentValue);
}
});
}
public void addValue(){
if(currentValue <= 999){
currentValue = currentValue + 1;
}
}
public void minusValue(){
if(currentValue >= 1){
currentValue = currentValue - 1;
}
}
}
You should refactor your OnClickListeners so that they are generic. Probably the easiest way to do this is to change your addValue() method to be addValue(View v), and minusValue() to minusValue(View v). Then, in the layout xml, you add a property android:onClick=addValue or android:onClick=minusValue. You'll have to update the method bodies so that they check the view's id and do the right thing based on that, but you won't have to create and set a whole bunch of OnClickListeners in the onCreate method - you'll get that for free.

Categories

Resources