I m trying this tutorial : http://android-er.blogspot.in/2014/01/get-text-from-dynamically-added-view.html \
where i m trying to edit the text when user click the insert button, it works fine only if i put position as static value.
but when i try to get position this app unfortunat stop.
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
final TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Button buttonInsert = (Button)addView.findViewById(R.id.insert);
buttonInsert.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
String showallPrompt = "";
View childView = container.getChildAt(position);
TextView childTextView = (TextView) (childView.findViewById(R.id.textout));
String childTextViewText = (String) (childTextView.getText());
showallPrompt += position + ": " + childTextViewText + "\n";
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
((TextView) (childTextView.findViewById(R.id.textout))).setText("HELLO ");
}});
container.addView(addView, 0);
}});
LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);
}
}
So here, i used to get position by
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
but not working . How to solve this problem.Actually i m tring to do listview with editable text and show as textview.
Related
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 );}*/
});
}
}
I am new to android studio, and I don't understand why my app crash when I start using the seekbar.
Could someone please help me?
public class MainActivity extends AppCompatActivity {
SeekBar seekBar; //declare seekbar object
TextView textView, textView2;
ListView simpleListview;
//declare member variables for SeekBar
int discrete;
int start = 50;
int start_position = 50;
//progress tracker
int temp = 0;
//declare objects for ViewStub
ViewStub stub;
CheckBox checkBox;
ListView lv; //declare Listview object
Button button;
View view;
public MainActivity() {
discrete = 0;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare viewstub object
stub = findViewById(R.id.viewStub1);
#SuppressWarnings("unused")
View inflated = stub.inflate();
stub.setVisibility(View.INVISIBLE);
//ViewStub logic
checkBox = findViewById(R.id.checkBox1);
//handle checkbox click event
checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
//remove objs from parent view to allow for child view objs
checkBox.setVisibility(View.GONE);
seekBar.setVisibility(View.GONE);
textView.setVisibility(View.GONE);
stub.setVisibility(View.VISIBLE);
}
}
});
//seekbar logic
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView.setText(" Celsius at 0 degrees");
//set default view
seekBar = findViewById(R.id.seekbar);
seekBar.setProgress(start_position);
//create event handler for SeekBar
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (temp == 0) {
//for initial view result
//Toast.makeText(getBaseContext(), "Fahrenheit result: 32 degrees",
//Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result 32 degrees") ;
}
else{
//Toast.makeText(getBaseContext(), "Fahrenheit result: "
//+ String.valueOf(discrete) +
//" degrees",Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result:" + String.valueOf(discrete) + "degrees");
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar){ }
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// To convert progress passed as discrete (Fahrenheit) value
temp = progress - start;
discrete = (int) Math.round((((temp * 9.0) /
5.0) + 32));
//convert C to F temp textView.setText(" Celsius at " + temp + " degrees");
textView.setText(" Celsius at " + temp + " degrees");
}
});
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView2 is not initialised here...
Change above code to
textView = findViewById(R.id.textview);
textView2 = findViewById(R.id.textView2);
TextView textView = findViewById(R.id.textview);
TextView textView2 = findViewById(R.id.textView2);
initialize textView2
You seem to have missed that part
I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....
Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity
Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}
In my main activity, I'm using a dialog to show a listview on clicking a button, the listview contains three textview, one edittext and one done button. By clicking the done button I'm showing only those rows which contain values in edittext using loop in a linear layout in my main activity, now what I want is that when I'll again reopen the dialog for list the edittext field will contain those values which have previously been added. How can I do that?
This is the code for populating the listview in the dialog,
allProducts = dh.gettAllProductInfo();
adapter = new ProductListAdapter(getApplicationContext(), allProductss);
productListView.setAdapter(adapter);
and this is when done button is clicked...
doneButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<productinfo> AddedItems = new ArrayList<productinfo>();
try
{
for(int i = 0;i<allProductss.size();i++)
{
View view = productListView.getChildAt(i);
EditText qty = (EditText) view.findViewById(R.id.quantity_edittext);
String qtyVal = qty.getText().toString();
TextView product = (TextView) view.findViewById(R.id.product_textview);
String pName = product.getText().toString();
TextView p_code = (TextView) view.findViewById(R.id.product_code);
String pCode = p_code.getText().toString();
if(qty.getText().toString().matches(""))
{
//Do nothing
}
else
{
AddedItems.add(new productinfo(pCode, pName, "", qtyVal, ""));
}
}
I try to make an example based on what I've understood what you want. Moreover, you'll need to adapt it to your code works. This is the example:
private ArrayList<productinfo> AddedItems;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to put this part of code when you show the dialog so that
// it will be executed all the time you open the dialog
if(AddedItems != null){
allProducts.addAll(AddedItems);
AddedItems = null;
}
else{
allProducts = dh.gettAllProductInfo();
}
adapter = new ProductListAdapter(getApplicationContext(), allProductss);
productListView.setAdapter(adapter);
doneButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AddedItems = new ArrayList<productinfo>();
try
{
for(int i = 0;i<allProductss.size();i++)
{
View view = productListView.getChildAt(i);
EditText qty = (EditText) view.findViewById(R.id.quantity_edittext);
String qtyVal = qty.getText().toString();
TextView product = (TextView) view.findViewById(R.id.product_textview);
String pName = product.getText().toString();
TextView p_code = (TextView) view.findViewById(R.id.product_code);
String pCode = p_code.getText().toString();
if(!qty.getText().toString().matches(""))
{
AddedItems.add(new productinfo(pCode, pName, "", qtyVal, ""));
}
}
}
catch(Exception e){}
}
}
}
I am trying to get the text in the EditText component named textfield and see if it equals "facebook". It does not work though and I can't for the life of me find out why. Also in the if statement my is supposed to be the ID for the picture.
public class Guess extends Activity {
ImageView image;
ImageView stat;
Button okButton;
EditText textfield;
Integer my;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess);
final Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("myimage");
final int resid = bundle.getInt("resourceID");
my = pic;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(pic);
image.setAdjustViewBounds(true);
stat = (ImageView) findViewById(R.id.imageView2);
stat.setImageResource(R.drawable.incorrect);
okButton = (Button) findViewById(R.id.button1);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textfield = (EditText) findViewById(R.id.editText1);
if("2130837510".equals(my.toString()) && "facebook".equals(textfield.getText()));
{
stat.setImageResource(R.drawable.correct);
}
}
});
}
}
textfield.getText() returns an Editable not a String. To make it work pass the actual String object using textfield.getText().toString().
You have a semi colon on the end of your if statement
if(1 == 2);
{
System.out.println("passed");
}
so the code below will execute anyway.