I want to print A,B,C..... in series, but when I click the button it's Unfortunately stopped,
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView alphabets = (TextView) findViewById(R.id.alphabet);
Button next = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
};
next.setOnClickListener(listener);
the reason is an IndexOutOfBoundException here:
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
change it to
for (int i=0;i<alpha.length;i++)
alphabets.setText(alpha[i]);
}
with your code you try to enter the last element +1 and that element does not exists.
btw. your textview will only display the last element!
EDIT:
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
StringBuffer alphabet = new StringBuffer();
for (int i=0;i<alpha.length;i++){
alphabet.append(alpha[i]);
}
alphabets.setText(alphabet.toString());
Try this
String alpha[30];
alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
Related
Currently working in Android Studio and running into a little trouble. Trying to get my rollScore button to roll over and over. At this point it "rolls" once and stops. I've tried a for loop and while loop and unable to get it to allow multiple "rolls".
public class GameScreen extends AppCompatActivity {
private Button rollButton; // Roll button being declared as a variable
private TextView rollScore; // Text view being declared as a variable
private TextView totalSCore; //
private int mCounter = 0;
private int totalRuns = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Random randomGenerator = new Random();
final int randomInt = randomGenerator.nextInt(7) + 1;
rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rollScore.setText(Integer.toString(randomInt));
mCounter++;
}
});
}
}
You have to generate a new random number on each click on your button.
Like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Random randomGenerator = new Random();
int randomInt;
rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
randomInt = randomGenerator.nextInt(7) + 1;
rollScore.setText(Integer.toString(randomInt));
mCounter++;
}
});
}
You need to generate a new random number after every roll e.g.
rollScore.setText(Integer.toString(randomGenerator.nextInt(7) + 1));
I have a class which I use a camera to point at an object and get all the strings it can see on the object. I then pass the list into this intent so the user can choose a specific string which I will then use to send to another activity.
However, I want to create a new textview for each string and then put an OnClickListener on each of the textviews, but I am unsure on how I can put an OnClickListener on a textview that I won't know exist as it will be created depending on the list.
public class SelectProductName extends AppCompatActivity {
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
}
}
}
So for example: getString is {"Chicken","Pie"}
I want a textview to be created for each string.
I'm unsure on how I would get the name of the textview to add the OnClickListener to it.
You can add the listener just like you would do with any view:
LinearLayout ll = findViewById(R.id.layout);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code goes here
}
});
}
public class SelectProductName extends AppCompatActivity implements OnClickListener{
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(this);
}
}
public void onClick(View view) {
TextView tv = (TextView)view;
Log.i("clicked", tv.getText().toString());
}
}
i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
[Here is a screenshot of my output][1]
I am trying to sort the elements of an array.
I have stored the elements to the array from EditText1(ed) and I want to sort them and display them in EditText2.
I am done with storing them and displaying them, i wanted to sort them using Collections.sort(array); but it shows me that there is something wrong.
This is my code so far:
public class MainActivity extends AppCompatActivity {
List<EditText> allEds = new ArrayList<EditText>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
allEds.add(ed);
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
Log.e("My data", strings[i]);
ed2.setText(strings[i]);
}
}
});
[1]: http://i.stack.imgur.com/dowdE.jpg
I think this will probably work (replace the code in the onClick with this):
String[] strings = ed.getText().toString().split("\\r?\\n");
Arrays.sort(strings);
String output = TextUtils.join("\\n",strings);
ed2.setText(output);
may be something like this?
public class MainActivity extends AppCompatActivity {
List<String> allEds = new ArrayList<String>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = ed.getText().toString();
String lines[] = str.split("\\r?\\n");
Arrays.sort(lines);
ed2.setText(TextUtils.join("\n", lines));
}
});
I am currently fooling around with the android SDK (eclipse) and i am trying to make a simple button-based TicTacToe game.
I made an array where i save the Buttons and i am trying to make the ClickEvents dynamically:
Here is the Code (Which works so far):
public class MainActivity extends Activity
{
final Button[] buttons = new Button[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons[0] = (Button) findViewById(R.id.button1);
buttons[1] = (Button) findViewById(R.id.button2);
buttons[2] = (Button) findViewById(R.id.button3);
buttons[3] = (Button) findViewById(R.id.button4);
buttons[4] = (Button) findViewById(R.id.button5);
buttons[5] = (Button) findViewById(R.id.button6);
buttons[6] = (Button) findViewById(R.id.button7);
buttons[7] = (Button) findViewById(R.id.button8);
buttons[8] = (Button) findViewById(R.id.button9);
for(int i = 0;i < 9;i++)
{
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
//
}
});
}
}
}
The problem comes when i try to reference to a button in the onClick-Method using 'i'.
For example:
#Override
public void onClick(View arg0)
{
//buttons[0].setText("X"); works by the way
buttons[i].setText("X");
}
Eclipse tells me: "Change modifier of 'i' to final"
Well but if 'i' would be final, i wouldn't be able to increase 'i' in the loop.
So i tried this:
#Override
public void onClick(View arg0)
{
final int position = i;
buttons[position].setText("X");
}
Which ends up resulting with the same Error "Change modifier of 'i' to final"
So my Question is: How can i make this work?
I have tried google but i wasn't able to find anything and this really is the first time i ever encountered this problem .
Hope you guys can help me out!
EDIT:
Well the solution was simple, here is the now working code:
for(int i = 0;i < 9;i++)
{
final int position = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[position].setText("X");
}
});
}
You can do something like this:
for(int i = 0;i < 9;i++)
{
final int position = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[position].setText("X");
}
});
}
Create another final int var in your loop set it the same value as i and reference this var from you listeners.
for(int i = 0;i < 9;i++)
{
final int index = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[index].setText("X");
}
});
}
Have a separate class which implements View.OnClickListener and have a constructor to accept the index value