I have got a two EditText in activity. I need after some event put text in current position of cursor. How can I do that?
et1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et1.setText("myString");
}
});
et1 is your edit text, do it for the second EditText either.
Related
I've been trying to search for solution for this but ain't lucky enough to find a correct answer.
So my problem is.. I want to remove the first Line break in my EditText after clicking a certain button.
First Line <- This should be removed after I click the button and the rest will remain.
Second Line
Third Line
Fourth Line
EditText txt = findViewById(R.id.editTextID);
Button btn = findViewById(R.id.btnID);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String firstLine = txt.getText().toString().substring(0, txt.getText().toString().indexOf("\n"));
String removeFirstLine = firstLine.substring(0, firstLine.length()-1);
txt.setText(removeFirstLine);
}
});
But this leaves only 1 line each time I click the button.
I believe that this will do what you want:-
btn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
txt.setText(txt.getText().toString().substring(txt.getText().toString().indexOf("\n")+1));
}
});
I'm having trouble taking in text from a few text boxes with one button. I can't seem to get OnClick() to work. I have setContentView(R.layout.activity_load_xactivity); as a test I know it doesn't have anything to do with input.
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_load_xactivity);
}
});
Is in my protected void onCreate(Bundle savedInstanceState). Doesn't work. No errors, just doesn't do anything.
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
setContentView(R.layout.activity_home2);
}
});
On It's own Doesn't resolve setOnClickListener. I have import android.view.View.OnClickListener set. I've tried entering different code where it says setContentView(R.layout.activity_home2);
Try adding an ID to the Layout holding the Views, Buttons (RelativeLayout, ContrasintLayout ect...) in XML. Then in java make a new Layout, and use layout.addContentView(XML) in your button:
Layout a = findViewById(R.id.layout_name);//Use whatever layout TYPE is used in XML
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
a.addContentView(R.layout.activity_load_xactivity);
}
});
Also, In you XML, you should give an ID to your layout then define it in java.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_name">
<!--You can use any Layout Type -->
...
</RelativeLayout>
EDIT:
I know this has nothing to with your question, but if you want to switch activity's use this instead:
Intent b = new Intent(this, JavaActivtyName.class);
startActivity(b);
I am trying to make a simple android application which contains a EditText and a Button . User will enter his data and this application will show him the data as a Toast.
Now The problem I am facing is , whenever button is pressed by user it's just showing a blank toast.Here is my code
EditText et;
String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bsave=(Button) findViewById(R.id.bsave);
et=(EditText) findViewById(R.id.editText1);
data=et.getText().toString();
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText",et.getText().toString());
Intent in=new Intent(MainActivity.this,SecondAct.class);
in.putExtra("DATA", data);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
}
please help me . Thanks in Advance!!!!
You are calling gettext() in oncreate(),when there is no text at all in editbox.. So your log inside onClick() will show data but toast will never show any thing with this code as variable data is ""..
You should do like
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
data=et.getText().toString();
Toast.makeText(MainActivity.this,data, Toast.LENGTH_SHORT).show();
}
});
in button onClick(...)
As shown above you need to save the data in the EditText inside of the Button's on click listener. As your code is now as soon as the edit text is created the text is being saved into the data variable, but you want it to set as soon as the button is clicked.
I am splitting a string and putting the result in edittexts using a loop so that the user can edit the data.thereafter he can save all the data in
the edittexts by just pressing the final button.Problem is i don't know how to get each value from the edittext when he pressses the save button.this is my code:
EditText etstringone,etstringtwo;
Button btn_save;
btnsave=new Button(this);
String mystring="somevalue";
String del="\\|";
String[] splitResult = mystring.split(del);
for (String e : splitResult)
{
etstringone=new EditText(this);
etstringtwo=new EditText(this);
etstringone.setText(splitResult[0]);
etstringtwo.setText(splitResult[1]);
mylayout.addView(etstringone);
mylayout.addView(etstringtwo);
}
mylayout.addView(btnsave);
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
how do i go about this?thanks.
NB:emphasis on using a single button to get all the data,i managed to do a scenario for apppending a new save button each time the
loop runs but its not neat.
The edit texts are dynamically created, but you can still access the object. Either cache a reference to the edit texts or walk the child-views of your "mylayout".
EDIT
From your code, you declare
EditText etstringone,etstringtwo;
You instantiate them
etstringone=new EditText(this);
etstringtwo=new EditText(this);
Now I don't know the scope of this (are they local vars, class/member vars?) if class vars, you can reference them in the
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
body. eg
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String e1 = etstringone..getText().toString();
}
});
I create button like this:
Button button = new Button(this);
button.setText("2012");
button.setBackgroundColor(Color.TRANSPARENT);
button.setOnClickListener(mCorkyListener);
layout.addView(dateButton);
On click listiner i have this method. Here i want to change button text color. Bu View don't have this method
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//v.setBackgroundColor(Color.RED);
//so how to change pressed button text color ?
//v.setTextColor(colors);
}
};
There wouldn't be just one button. There would be many of those and i need to chnage text color when button pressed.
I know you asked about changing text color, and everyone else has pretty well covered that, but you could also change the button color itself (which I find much more visible than a text color change :p)...
import android.graphics.PorterDuff;
To set it to green (assuming you start with a standard gray button):
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Button aButton = (Button) view.findViewById(R.id.abutton);
aButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
}
}
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
button.setTextColor(Color.RED);
}
};
button.setTextColor(Color.WHITE);
This will change the textcolor of the button.Do u want to give a press effet to button when click on it?
the best way to do this is not programmatically (using selector), but if you want to do it programmatically you can cast it to Button and then change the color.
public void onClick(View v) {
Button b = (Button) findViewById(v.getId());
b.setBackgroundColor(Color.RED)l
}
If you are interested to use the View of onClick(View v) then just cast it to Button
public void onClick(View v) {
if(v instanceof Button){
((Button)v).setTextColor(Color.WHITE);
}
}