How to select next and previous Edit texts in a view - android

I have five Edit Text in my application . I also have two buttons called "Next" and "Previous". Now I want to select the next and previous edit text fields when i click the corresponding buttons form my view dynamically. Is there any way to do this.

btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusDownId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Next");
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusUpId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Back");
}
}
});
This is the XML where you have to set the focus order
<EditText
android:id="#+id/et1"
android:nextFocusDown="#+id/et2"
android:nextFocusUp="#+id/et2"
....../>
<EditText
android:id="#+id/et2"
android:nextFocusDown="#+id/et1"
android:nextFocusUp="#+id/et1"
...../>
Edit
If you are creating view dynamic then you should use below methods to set the next focus
setNextFocusDownId(id)
setNextFocusUpId(id);

i think this may help you,
http://kahdev.wordpress.com/2008/06/29/changing-button-text-in-android/

Make use of
android:nextFocusLeft
android:nextFocusRight
android:nextFocusUp
android:nextFocusDown
in your editText's attributes in your layout.xml.
e.g. android:nextFocusDown="#id/myNextEditText"
For more details about how to use it please follow this link.

Try -
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next :
if(editText1.hasFocus()){
editText2.requestFocus();
}else if(editText2.hasFocus()){
editText3.requestFocus();
}
break;
case R.id.btn_previous :
if(editText2.hasFocus()){
editText1.requestFocus();
}else if(editText3.hasFocus()){
editText2.requestFocus();
}
break;
}
}

Related

When I clicked Button then Cursor should move to next EditText in diffrent Frame?

I have 4 edittext and 1 button.When i clicked Button then Cursor will go Blank Edittext in next Frame.
Please guide me the correct way to achieve my objective.
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
groupno =(EditText)findViewById(R.id.editText1);
start=(EditText)findViewById(R.id.editText3);
end=(EditText)findViewById(R.id.editText4);
groupno.setEnabled(false);
start.setEnabled(false);
end.setEnabled(false);
}
}
Add this inside the setonlick method.U can use this code to move any edittext.
nextEdittext.requestfocus();
use this code :
use outside button click --
EditText.setFocusableInTouchMode(false);
and on button click press :--
ButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countryText.setFocusableInTouchMode(true);
countryText.requestFocus();
}
});
Try this on your button click.
View view = getCurrentFocus().focusSearch(View.FOCUS_FORWARD);
int id = view.getId();
if (id != View.NO_ID) {
findViewById(id).requestFocus();

edittext visibility change at runtime depending upon it's visibility

Hello all i want to do
1]if edittext is visible then invisible and if invisible then visible to it for that i have done this
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// fragment=new BBQ();
// Intent i=new Intent(getApplicationContext(),
// Search_Activity.class);
// startActivity(i);
ed= (EditText) findViewById(R.id.editText1);
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE);
}
}
for me if it if invisible then it make visible but on second click it not invisible what is wrong i am doing?
I'll say that you should else-if condition:
if(ed.getVisibility()==View.INVISIBLE) {
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==View.VISIBLE) {
ed.setVisibility(View.INVISIBLE);
}
Or with the ternary operator:
ed.setVisibility (ed.getVisibility() != View.VISIBLE ? View.VISIBLE : View.INVISIBLE);
May be when the code enters the first if, later you modify the visibility and set again invisible, so it enters again in the second if, try this:
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE)
}
Try this:
Boolean isVisible=true;
ed= (EditText) findViewById(R.id.editText1);
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(isVisible){
ed.setVisibility(arg0.INVISIBLE);
}else{
ed.setVisibility(arg0.VISIBLE);
}
isVisible=!isVisible;
}
Try this..
EditText having tag attribute initility set the tag as visible then while giving invisible setTag("invisible"); or setTag("visible");
<EditText
android:id="#+id/url_edittext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:tag="visible" />
Code
if(ed.getTag().equals("visible"))
{
ed.setVisibility(View.INVISIBLE);
ed.setTag("invisible");
}
else if(ed.getTag().equals("invisible"))
{
ed.setVisibility(View.VISIBLE);
ed.setTag("visible");
}
Edit your code
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(View.GONE);
}
Hope this will solve your issue

android | multiple onclicklistener in dialog

Inside my Activity I start a simple dialog.
final Dialog myDialog = new Dialog(this);
myDialog.setContentView(R.layout.testing);
...
My testing.xml Layout consists of nothing but 10 ImageViews, id`s are '1' to '10'.
I want every ImageView to be clickable and to do something.
The define the onclick() methode in the .xml file isn`t working, as the methode can't be found when the dialog is viewed.
The only way I got it work is following: define 10 onclick-listeners:
ImageView img_1 = (ImageView) myDialog.findViewById(R.id.1);
ImageView img_2 = (ImageView) myDialog.findViewById(R.id.2);
...
img_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
execute_funtion(1);
myDialog.cancel();
}
});
img_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
execute_funtion(2);
myDialog.cancel();
}
});
...
However, that's really bad code, I have 10 times nearly the same lines.
So my question: How can I make that work with clean code?
I thought about a multiple onclicklistener (overwride the onClick() function and make a switch/case in the functions or something like that), but it's not working.
I'm happy about every idea!
Thanks
/EDIT
Here a snippet of the .xml file
<ImageView
android:id="#+id/1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:onClick="myFunction"
android:src="#drawable/ic_launcher" />
Make your Activity implement OnClickListener and then process the onClick event like below:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img1:
...
break;
case R.id.img2:
...
break;
}
}
You should let your Activity/Fragment implement the OnClickListener.
When you do that you will have to override the onClick method in that particular activity/fragment.
Set the onClickListeners on the images as follows:
img_1.setOnClickListener(YourActivity.this);
Then in that onClick method you can put a switch case or an if else if case as follows
#Override
public void onClick(View v)
{
if(v==img_1) {
//do this
} else if(v==img_2) {
//do that
}...
}
or
#Override
public void onClick(View v)
{
switch (v.getId()) {
case img_1.getId(): // do this
break;
case img_2.getId(): // do that
break;
.
.
.
default : break;
}
}

how to make text view clickable in android?

is it possible in android to make text view clickable if yes then how ??and if not then what will be the way for make a label clickable??i want to implement a call activity using this
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+keywordxmlparsing.phone));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("dialing-example", "Call failed", activityException);
}
}
thanks for ur responses in advance...
textView.setOnClickListener(new View.OnClickListener());
Have you tried this?
More easier directly in the XML : with clickable = true
<TextView
android:id="#+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="#string/forgotPassword"
android:onClick="forgotPassword"
android:clickable="true"
/>
We can also get click event on TextView same as Button & ImageView.
and method is also same for all View.
like as
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
First in your java file cast your TextView by xml id
TextView tv = (TextView)findViewById(R.Id.textView1);
then,
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Honestly, I found a flat button to work better for what I was doing with a RecyclerView:
<Button
android:id="#+id/btnFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"/>
Source: https://stackoverflow.com/a/30884132/2328637
Then customizing the button to fit my layout and finally adding the following to my MainActivity.java under onCreate:
Button btnFoo = (Button) findViewById(R.id.btnFoo);
btnFoo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FooActivity.class);
startActivity(intent);
}
});
Try this:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
In the xml for that TextView include the field
android:onClick="functionName"
Then in your java file associated with that xml include this function
public void functionName(View view){
// do your stuff
}
you can set a onclick listener to the texview like button.infact button inherits the properties from textview.
Though it was long ago when you asked your question. But I think that the right thing to attain what you wanted is to set TextView xml attribute android:autoLink. For example:
<TextView
...
android:autoLink="phone" />
Simply try this one:-
Implement View.OnClickListener, then simply apply switch case and define the id of your text view in the case and pass the intent.
example:-
#Override
public void onClick(View v) {
//TODO Auto-generated method stub
switch (v.getId()) {
case R.id.textView:
startActivity(new Intent(this,CalledClass.class));
break;
default:
break;
}
//here textView is id for the textView I chose.
TextView is also derived of View like- EditText,ListView etc.,so we can use
textView.setOnClickListener(new View.OnClickListener());

Variable OnClick Listener Android

Is there a way to have 1 onClick Lister for many buttons where I can toss a case statement to do things based on what buttons were clicked.
I know I can make 100 different listeners for 100 buttons but I have to think I can create some nifty variables to do it in less lines of code.
Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.
// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};
If you are using 1.6+ version of the SDK you can use android:onClick to set the onClick handler of a view. In your activity you must have a method with the following signature. The view is the view that was clicked.
void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//do something fantastic;
break;
}
}
public class MainActivity extends Activity implements View.OnClickListener{
btnXXX.setOnClickListener(this);
public void onClick(View v) {
if (v.getId()==R.id.btnXXX){
dialog.show();
} else {
handleOtherViews(v);
}
}
Alternatively, you can specify the method to call in xml:
<Button android:id="#id/button" android:text="#string/button" android:onClick="someMethod" />

Categories

Resources