Create different textviews for list of strings - java

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());
}
}

Related

How to Get Data from AutoComplete Text View on Button Press?

I want to use the if-else statement on Button press - if-else statement get the string from autoComplete text view... but it not works...
I want to make the calculation when selecting the first product - but it does not work.
public class MainActivity extends AppCompatActivity {
TextInputLayout textInputLayout,alkalinityInput,waterVoume;
TextInputEditText waterText,alkaText;
TextView result;
AutoCompleteTextView dropDownText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
waterVoume=findViewById(R.id.waterVolumeInputText);
textInputLayout = findViewById(R.id.productTextDropdown);
dropDownText = findViewById(R.id.dropdown_text);
alkalinityInput = findViewById(R.id.alkalinityInputText);
waterText = findViewById(R.id.waterVtext);
alkaText = findViewById(R.id.alkaVtext);
result=findViewById(R.id.resultTextView);
String[] items = new String[] {
"AquaForest - KH Plus",
"Brightwell - Reef Code B",
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(
MainActivity.this,
R.layout.dropdown_item,
items
);
dropDownText.setAdapter(adapter);
button=findViewById(R.id.calculateButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dropDownText.getText().equals("AquaForest - KH Plus"))
{makeCalculations();
}
}
private void makeCalculations() {
double vouumeValue = Double.valueOf(waterText.getText().toString());
double alkvalue = Double.valueOf(alkaText.getText().toString());
double resultView = 1*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );
double resultView = 5*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );}*/
});
}
}

Android: sorting letters in EditText

[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));
}
});

Trouble with Intent in Android Studio

I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.
First Java Class:
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra("Number", editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString("number");
}
textView.setText(number);
}
}
you are putting "Number" as key but, in your second activity you are trying to retrieve "number"
so change number to Number and it will work.
Its case Sensitive.
Don't make your keys hard-coded in that way.
Just declare public static variable in MainActivity and use it from SecondScreenJ
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString(MainActivity.NUMBER_KEY);
}
textView.setText(number);
}
}
Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.

Android Program...coding Error

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"};

Array adapter context

this is my code:
public class EnterActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.enter);
Button search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] answers = {
"das",
"das",
"dsa"
};
EditText eproduct = (EditText) findViewById(R.id.product);
EditText efactory = (EditText) findViewById(R.id.factory);
GridView grid = (GridView) findViewById(R.id.grid);
String product = eproduct.getText().toString();
String factory = efactory.getText().toString();
FrenchList french = new FrenchList();
int frenchResult = french.getCosher(product, factory);
//error is here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, answers);
}
});
}}
the error is: "The constructor ArrayAdapter(new View.OnClickListener(){}, int, String[]) is undefined"
I did some searching on similar problems and I'm preety sure the problem is that i don't use the context properly, but to my understanding, as long as "this" appears in a class that extends activity I should be alright.
Why is this error showing? how can I fix it?
this refers to the View.OnClickListener class, which is an anonymous inner class in this example. You can use EnterActivity.this.

Categories