package com.example.addarray;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
int mCounter = 0;
TextView textView;
TextView textView2;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(view -> {
mCounter++;
textView = findViewById(R.id.textView1);
textView.setText(Integer.toString(mCounter));
textView2 = findViewById(R.id.textView2);
editText = findViewById(R.id.editText);
String [] string = new String[mCounter];
for(int i =mCounter-1; i>mCounter; i++){
string[i] = editText.getText().toString();
}
textView2.setText(Arrays.toString(string));
textView3 = findViewById(R.id.textView3);
textView3.setText(editText.getText().toString());
});
}
}
I would like to add the input from the edit text into an array upon each click of the button, however, the element of the array only displayed null as shown in the picture. Is there any way to fix it? Thank you.
You create a new array with each click. No need to create a new array on every click. It is possible to save new values in ArrayList.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> list = new ArrayList<String>();
// *******
button.setOnClickListener(view -> {
// *******
list.add(editText.getText().toString());
// ******
}
}
Related
MainActivity
package com.example.greetings_john_sporn;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText nameText;
TextView textView;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText) findViewById(R.id.nameText);
textView = (TextView) findViewById(R.id.textGreeting);
btn = (Button) findViewById(R.id.buttonSayHello);
nameText.setText("");
}
public void sayHello(View view) {
if(nameText != null) {
textView.setText("Hello " + nameText.getText());
btn.setEnabled(true);
} else {
textView.setText("You must enter a name");
btn.setEnabled(false);
}
}
}
Need help setting the text to "You must enter a name" when the nameText is null. For some reason even when the nameText field is empty it still outputs "Hello" when it should be outputting "You must enter a name".
Probably you need to check if the text of your EditText is null, not the EditText widget itself.
Try changing your condition
if(nameText != null) {
to
if (!TextUtils.isEmpty(nameText.getText())) {
I am new to android studios and was tasked with creating an app programmatically. Depending on which button is pressed, the color description is displayed. I have a feeling it has something to do with the layout I created. Every time I run the program the app crashes and I cannot figure out why. Below is my code:
package com.example.hw3ex2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout layout;
LinearLayout.LayoutParams layoutParams;
String plumDescription = getResources().getString(R.string.plum_is);
String blueDescription = getResources().getString(R.string.blue_is);
String goldDescription = getResources().getString(R.string.gold_is);
String descriptionString;
TextView tv;
Button btnPlum;
Button btnBlue;
Button btnGold;
private int button_color = getResources().getColor(R.color.button_color);
private int background_color=getResources().getColor(R.color.background_color);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGUIByCode();
}
private void buildGUIByCode() {
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create textview
tv = new TextView(this);
tv.setLayoutParams(params);
layout.addView(tv);
//create buttons and add them to layout
btnPlum = new Button(this);
btnPlum.setTag("plum");
btnPlum.setText("Plum");
btnPlum.setLayoutParams(params);
layout.addView(btnPlum);
btnPlum.setOnClickListener(this);
btnPlum.setBackgroundColor(button_color);
btnBlue = new Button(this);
btnBlue.setTag("blue");
btnBlue.setText("Blue");
btnBlue.setLayoutParams(params);
layout.addView(btnBlue);
btnBlue.setOnClickListener(this);
btnBlue.setBackgroundColor(button_color);
btnGold = new Button(this);
btnGold.setTag("gold");
btnGold.setText("Gold");
btnGold.setLayoutParams(params);
layout.addView(btnGold);
btnGold.setOnClickListener(this);
btnGold.setBackgroundColor(button_color);
setContentView(layout);
}
#Override
public void onClick(View view) {
Object tag = view.getTag();
if ("plum".equals(tag)) {//get plum_is string and display in textView
tv.setText(plumDescription);
} else if ("blue".equals(tag)) {//get blue_is string and display in textview
tv.setText(blueDescription);
} else if ("gold".equals(tag)) {//get gold_is string and display in textview
tv.setText(goldDescription);
}
System.out.println(descriptionString);
}
}
I see that you're invoking getResources() before onCreate,
you need to move all of those initialization that uses getResources() within onCreate
public class MainActivity ... {
String plumDescription; // don't initialize here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plumDescription = getResources().getString(R.string.plum_is); // initialize after onCreate
}
}
this code work very well but now when question is CAR button1 = A button2 = R button3 = C when I click button3 button1 = INVISIBLE edit text = A and when I click button2 edit text = A I want when click on button edit text get text from button and check text in edit text
requires: edit text get char form button, add this code to function , remove last index from array
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
Button button;
EditText txt;
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
button = new Button(this);
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
//Button button; //TODO: is local var!
EditText txt; //Global var
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//the problem was here (var button)
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}
Not sure of the questions, but the one thing I did find to be an issue with the code.
txt.setText(txt.getText().toString() + button.getText());
The bolded part doesn't returns a char sequence. Should be the same thing as the edit text.
button.getText().toString();
MainActivity:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.*;
import android.content.Intent;
public class MainActivity extends Activity {
private Spinner spinner1;
private Button btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Get Value from EditText //Save value to a Variable
EditText person0 = findViewById(R.id.spot0); String saved_spot0 = person0.getText().toString();
EditText person1 = findViewById(R.id.spot1); String saved_spot1 = person1.getText().toString();
EditText person2 = findViewById(R.id.spot2); String saved_spot2 = person2.getText().toString();
EditText person3 = findViewById(R.id.spot3); String saved_spot3 = person3.getText().toString();
EditText person4 = findViewById(R.id.spot4); String saved_spot4 = person4.getText().toString();
EditText person5 = findViewById(R.id.spot5); String saved_spot5 = person5.getText().toString();
EditText person6 = findViewById(R.id.spot6); String saved_spot6 = person6.getText().toString();
EditText person7 = findViewById(R.id.spot7); String saved_spot7 = person7.getText().toString();
EditText person8 = findViewById(R.id.spot8); String saved_spot8 = person8.getText().toString();
EditText person9 = findViewById(R.id.spot9); String saved_spot9 = person9.getText().toString();
List<String> filled_spots = Arrays.asList(saved_spot0, saved_spot1, saved_spot2, saved_spot3, saved_spot4, saved_spot5, saved_spot6, saved_spot7, saved_spot8, saved_spot9);
Collections.shuffle(filled_spots); //Shuffle List
Collections.sort(filled_spots); //Sort List
Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
teams_Screen.putStringArrayListExtra ("mylist", (ArrayList<String>) filled_spots);
startActivity(teams_Screen);
}
});
}
}
DisplayTeamsActivity:
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class DisplayTeamsActivity extends AppCompatActivity {
private ListView lv;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_teams);
ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");
}
}
I need to pass the List array from one activity to another and populate the listView I have from the shuffeld array, but something is crashing my app when i click on the button that takes me to the next activity. I dont know if this is correctly done and what is the problem causing it to entirely crash...
you can try this
in first acticity
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("mylist", yourlist_array);
context.startActivity(intent);
in second activity
Intent intent = getIntent();
List stringArray = intent.getStringArrayExtra("mylist");
Do this:
ArrayList<String> filled_spots = new ArrayList<>(Arrays.asList("nome1", "nome2"));
Collections.shuffle(filled_spots); //Shuffle List
Collections.sort(filled_spots); //Sort List
Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
teams_Screen.putStringArrayListExtra("mylist", filled_spots);
At DisplayTeamsActivity you'll need do this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");
}
when using the ab.jar in eclipse I get the following error when trying to run the bot.
“Could not find class ‘org.alicebot.ab.Bot’, referenced from method…”. The chat class is being imported but the bot class cannot be imported.
Here is my code:
import org.alicebot.ab.Bot;
import org.alicebot.ab.Chat;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
String usertext; String response;
String botname="MAVIS";
Bot mavis=new Bot(botname);
Chat chat= new Chat(mavis);
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
final LinearLayout ll2 = (LinearLayout) findViewById(R.id.ll2);
final ScrollView scv = (ScrollView) findViewById(R.id.sv);
final Button btn = (Button) findViewById(R.id.button1);
final EditText medit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
TextView tvu=new TextView(v.getContext());
TextView tvb=new TextView(v.getContext());
TextView tvut=new TextView(v.getContext());
TextView tvbt=new TextView(v.getContext());
TextView tvdivider1=new TextView(v.getContext());
TextView tvdivider2=new TextView(v.getContext());
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tvu.setLayoutParams(lparams);
tvb.setLayoutParams(lparams);
tvut.setLayoutParams(lparams);
tvbt.setLayoutParams(lparams);
tvdivider1.setLayoutParams(lparams);
tvdivider2.setLayoutParams(lparams);
usertext = medit.getText().toString();
if(usertext.trim().length() != 0){
ll1.addView(tvu);
ll1.addView(tvb);
ll2.addView(tvut);
ll2.addView(tvbt);
ll1.addView(tvdivider1);
ll2.addView(tvdivider2);
response=chat.multisentenceRespond(usertext);
tvu.setText("User");
tvb.setText(botname);
tvbt.setText(" : "+ response);
tvut.setText(" : "+ usertext);
medit.setText(" ");
tvdivider1.setText(" ");
tvdivider2.setText(" --------------------");
}
else{
//do nothing
}
}
});
scv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
scv.post(new Runnable() {
public void run() {
scv.fullScroll(View.FOCUS_DOWN);
}
});
}
});
}
}
I have imported the external library and defined the classpath for it. I have also copied the ab.jar in my project folder and even defined the classpath for it. But nothing seems to be working. Am I doing this wrong or are there more libraries that are needed for this to work. Anybody have a solution to my problem?
Your bot name here is MAVIS..did you change the folder name from the bots.zip???else change it..or simply use the predefined bot names(alice2 etc).
Thankz