edittext visibility change at runtime depending upon it's visibility - android

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

Related

how to set buttons visibility in kotlin

Hello I wanna know how to control somethings visibility with button clicker
VisibilityButton.setOnClickListener(
makebtninvisible()
)
public void makebtninvisible (View v) {
Something.setVisibiity(View.INVISILE)
It should be
VisibilityButton.setOnClickListener{
VisibilityButton.visibiity=View.GONE
}
try this way :
VisibilityButton.setOnClickListener(
VisibilityButton.visibility = if (VisibilityButton.visibility == View.VISIBLE){
VisibilityButton.visibiity=View.INVISILE
} else{
VisibilityButton.visibiity=View.VISIBLE
}
}

Check if EditText Number Decimal is Empty

I tried everything.
if (editText == null)...
if (etNumero1.getText().toString().isEmpty())...
if (etNumero1.getText().toString().equals("")...
if (etNumero1.getText().toString().trim().length() >= 0))
Nothing works. Even my teatcher cant tell whats wrong.
Sorry for bad english im Brazilian.
Check, If the value is not an Empty
if (!etNumero1.getText().toString().isEmpty())
{
if(etNumero1.getText().toString().contains("."))
{
//Do your functionality here
}
}
else
{
//
}
Try
if (Integer.parseInt(etNumero1.getText().toString()) > 0)
If you specify your question, I could answer in better condition.
Use TextUtils.isEmpty("") check for null if not convert to formart to ###.##
Make sure that you are not making mistake on editText etNumero1??
I know editText is Edit Text but what is etNumero1?? please replace etNumero1 to editText, it should work
if (editText.getText().toString().isEmpty())...
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final 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().isEmpty()){
System.out.println("empty string");
}else{
System.out.println("" + editText.getText().toString());
}
}
});
}
if(String.valueOf(editText.getText() != null){
Double double = Double.parseDouble(editText.getText().toString);
}

How can I change the onClick action of a ImageView dynamically?

I have a ImageView that does an action called setAsFavorite(). This action sets a product as favorite, so I change the image of the ImageView. But when the product is favorite, I want to change the onclick event, so when the user presses the imageView, the action called should be unsetAsFavorite() instead of setAsFavorite.
How can I change the onclick event dynamically?
You could use A flag to know if it has been set as favorite then act on that
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isSetAsFavorite){
unsetAsFavorite();
} else {setAsFavorite();}
}
});
where is isSetAsFavorite is of boolean type.
The answer given by av_lee is sufficient. But there is a better option.
Use if instead,
boolean isFavourite = false;
public void switchFavorite(){//Replace function setAsFavorite() with this
if(isFavourite)
isFavourite = false;
else
isFavourite = true;
}
This way you decrease the number of function needed for the job.
I think there are two operation in your onclick method setAsFavorite() & unsetAsFavorite()
int myFlag =0;
onclick(){
if(myFlag == 0)
setAsFavorite();
else
unsetAsFavorite();
}
void setAsFavorite(){
myFlag = 1;
//do your work...
}
void unsetAsFavorite(){
myFlag = 0;
//do your work...
}
Get a reference to your ImageView:
getView().findViewById(R.id.your_img_view_id)
and then assign a new listener to it like this:
yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your code here
}
});
This way you change an old listener with the new one.

How to select next and previous Edit texts in a view

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

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

Categories

Resources