Hot questions for Using Android EditText in android activity

Question:

I have a Java class that uses PSO to calculate the best global solution for the optimisation of services that a user inputs in the main activity of a UI. When I run my UI, I have printed the bit combination calculation in the terminal using System.out.println(), however I need that same print to print in my EditText view in an activity called SolutionActivity.java.

Here is what my terminal shows me:

I/System.out: Particle value: 11.0
I/System.out: Particle bit string: [true, false, true, true]
I/System.out: Particle goodness: 5.0
I/System.out: Time spend: 2.8145
I/System.out: Iterations: 4.6209
I/System.out: Success: 3724.0
I/System.out: true false true true 

Here is the code in the Java class (CustomUseCase.java) that prints this...

// ...

    this.found += (bpso.getFound() ? 1 : 0);
    this.iterations += bpso.getSolIterations(); //use the method in bpso to get number of iterations taken
    long end = System.currentTimeMillis() - start; //end time minus start time

    this.sumTimes += end; //override the time spent variable

    System.out.println("Particle value: "      + Particle.getValue(Particle.bestGlobal()));
    System.out.println("Particle bit string: " + Arrays.toString(Particle.bestGlobal()));
    System.out.println("Particle goodness: "   + customService.getGoodness(Particle.bestGlobal()));
}

System.out.println("Time spend: " + sumTimes/max);
System.out.println("Iterations: " + iterations/max);
System.out.println("Success: " + found);

boolean[] bestCombo = Particle.bestGlobal();

for(Boolean b: bestCombo){
    System.out.print(b + " ");
}

System.out.println();

Now I need the System.out.print(b + " "); printed in a EditText view in my SolutionActivity. The number boolean variables in bestCombo[] will vary depending on what the user inputs in the main activity, if the user inputs 3 services, then the bestCombo will have 5 elements, if the user inputs 6 services, the bestCombo will have 8 elements. Here is what it looks like at the moment...

public void setUserResults(){
    EditText userGlobal = (EditText) findViewById(R.id.userGlobal);
    EditText best = (EditText) findViewById(R.id.best);

    best.setText(); //i need the bestCombo solution to be input here!! e.g True False True False
}

Answer:

You could have a method in CustomUseCase.java that calculates "bestCombo" and returns it as a String rather than printing it to the console. Something like:

class CustomUseCase {
    .
    .
    .

    public static String getBestCombo() {
        .
        .
        .

        boolean[] bestCombo = Particle.bestGlobal();

        String bestComboString = "";
        for (Boolean b : bestCombo){
            bestComboString = bestComboString + b + " ";
        }

        return bestComboString;
    }
}

And then call it:

String bestComboString = CustomUseCase.getBestComboString();
best.setText(bestComboString);

Question:

I readed a lot of similar questions, but no one answers mine or can solve my problem. I have a EditText like this in the layout:

    <EditText
        android:id="@+id/editText2"
        android:layout_width="248dp"
        android:layout_height="59dp"
        android:layout_marginStart="21dp"
        android:layout_marginTop="36dp"
        android:width="360dp"
        android:ems="5"
        android:hint="@string/ultimos4Dig"
        android:inputType="number"
        android:maxLength="10"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

I need that when the user are writing the numers of the month and year, a slash appear or disappearwhen he is writing. If he writes 2 numbers then a slash needs to appear. If he erases and its only one number, then the slash needs to disappear.

I need that in the editText appears the date as: 14/06

Here is my code but its not working.

  protected void onCreate(Bundle savedInstanceState) {

        EditText editText2 = (EditText)  findViewById(R.id.editText2);
        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable text) {
                if (text.length() == 2) {
                    text.append('/');
                }
            }
        });

FYI i made a class for a solution of this, thanks


Answer:

Use Masked Edit Text.

https://github.com/santalu/mask-edittext

<com.santalu.maskedittext.MaskEditText
    android:id="@+id/editText2"
    android:layout_width="248dp"
    android:layout_height="59dp"
    android:layout_marginStart="21dp"
    android:layout_marginTop="36dp"
    android:width="360dp"
    android:ems="5"
    android:hint="@string/ultimos4Dig"
    android:inputType="number"
    android:maxLength="10"
    android:textSize="24sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:met_mask="##/##"/>

Question:

Hello! Trying to input a number to the EditText field, press the calc531button and have that number get to the inputDouble variable.

xml:

<EditText
    android:inputType="numberDecimal"
    android:id="@+id/inputField"
    android:layout_below="@+id/title5"/>

<Button
    style="?android:attr/buttonStyleSmall"
    android:text="Calculate"
    android:id="@+id/calcBtn"
    android:onClick="inputField" />

java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_five);

    View.OnClickListener calcButton = new View.OnClickListener() {
        public void onClick(View v) {
            textHold = (EditText) findViewById(R.id.inputField);
            stringHold = textHold.getText().toString();
            inputDouble = Double.parseDouble(stringHold);
        }
    };

logcat Error:

java.lang.IllegalStateException: Could not find a method squat1RMinputField(View)
.........
Caused by: java.lang.NoSuchMethodException: inputField [class android.view.View]

Any help will be much appreciated!


Answer:

You are calling onClickListener to the same button twice and implementing only once.

If you already have defined android:onClick=""inputField"" inside the xml having Button, you just need to implement the inputField method in your activity.

public void inputField(View v){
    textHold = (EditText) findViewById(R.id.inputField);
    stringHold = textHold.getText().toString();
    inputDouble = Double.parseDouble(stringHold);
}

and remove the View.OnClickListener calcButton... from your onCreate() method.

Question:

I have a problem that my search function doesn't work. I want to make a search function on ListView that when I type some text the items are filtered, and when I click on some item it goes to another activity.

My code:

package com.shady.letsdoit;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

public class Search extends AppCompatActivity {
    ArrayList<HashMap<String, String>> productList;
    private EditText search_box;
    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        search_box = (EditText) findViewById(R.id.search_box);
        final ListView list_view = (ListView) findViewById(R.id.list_view);
        String products[] = getResources().getStringArray(R.array.search);

/** <string-array name="search"> <item>@string/Android_whats_android</item> <item>@string/Android_perso_wallpaper</item> <item>@string/Android_perso_ringtone</item> <item>@string/Android_perso_lockscreen</item> <item>@string/Android_perso_langueg</item> <item>@string/Android_perso_dualsim</item> <item>@string/Android_perso_datetime</item> <item>@string/Android_perso_bluetooth</item> <item>@string/Android_perso_appmanagre</item> <item>@string/Android_perso_accounts</item> <item>@string/Android_f_m</item> <item>@string/Android_insta</item> <item>@string/Android_telgram</item> <item>@string/Android_bestapp</item> <item>@string/Android_root</item> <item>@string/Pc_whats_win</item> <item>@string/Pc_boot</item> <item>@string/Pc_nasb_win_7</item> <item>@string/Pc_nasb_win_8</item> <item>@string/Pc_nasb_win_10</item> <item>@string/Pc_win7_perso</item> <item>@string/Pc_win10_perso</item> <item>@string/Pc_win_internet_browser</item> <item>@string/Pc_win_internet_download</item> <item>@string/Pc_win_safe</item> <item>@string/Pc_win_errors_softwere</item> <item>@string/Pc_win_errors_hardwere</item> <item>@string/Pc_win_bestsoftweres</item> </string-array> **/

        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.subject_name, products);
        list_view.setAdapter(listAdapter);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                if (selectedItem.equals("@string/Android_perso_lockscreen")) { /**each and every single of these "@string..." are an Activity for example * @string/Android_perso_wallpaper = Android_perso_wallpaper.class * that I want to open it when I click on same Item * I tried some methods but it didn't work plz help me thanks. */
                    Intent intent = new Intent(Search.this, Android_perso_lockscreen.class);
                    startActivity(intent);
                }
            }
        });
        search_box.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                Search.this.listAdapter.getFilter().filter(cs);
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    /**
     * my action bar
     **/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                Intent search = new Intent(this, Search.class);
                search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(search);
                return true;
            case R.id.action_home:
                Intent home = new Intent(this, MainActivity.class);
                home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(home);
                return true;
            case R.id.action_fav:
                Intent favorite = new Intent(this, Favorits.class);
                favorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(favorite);
                return true;
            case R.id.action_phone:
                Intent phone = new Intent(this, phon.class);
                phone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(phone);
                return true;
            case R.id.action_pc:
                Intent pc = new Intent(this, pc.class);
                pc.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(pc);
                return true;
        }
        return false;
    }
}

Answer:

For your searching in the listview you can call the method::

private void setFilter(CharSequence cs) {
    String filterProducts[] = new String[]{};
    if (!TextUtils.isEmpty(cs)) {
        int j = 0;
        for (int i = 0; i < products.length; i++) {
            if (products[i].contains(cs)) {
                filterProducts[j] = cs.toString();
                j++;
            }
        }
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, filterProducts);
        list_view.setAdapter(listAdapter);

    }else{
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, products);
        list_view.setAdapter(listAdapter);
    }
}

Call it from here ::

 @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
          setFilter(cs);
 }

For that redirecting to next activity..You have to explain what you want to do and what is the problem you facing..because your code is not that much clear indication about it.