I have two buttons and one imageview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >
<ImageView
android:id="#+id/dog"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/dogsec" />
<ImageButton
android:id="#+id/barkk"
android:contentDescription="#string/desc"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/dog"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:src="#drawable/bark" />
<ImageButton
android:id="#+id/rrrr"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/barkk"
android:layout_below="#+id/barkk"
android:layout_marginRight="41dp"
android:layout_marginTop="15dp"
android:contentDescription="#string/desc"
android:src="#drawable/rrr" />
</RelativeLayout>
a) I need to make Imageview change its' source to another one(I mean the image's default state is picture1, it should show picture2 and go back to picture1) on button "barkk" click. It should not show picture2 as long as barkk is pressed.
b) and I need to display picture3 as long as button rrrr is pressed.
I use exactly ImageView which should change its' source depending on case a) or b) as it is described above.
Help me please. Thanks in advance
Use this code.
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN) {
myImageView.setImageResource(R.drawable.myNewImage);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
myImageView.setImageResource(R.drawable.myNewImage);
}
}
};
Related
I want an action to be performed every time I click on the screen . There is a list view inside Relative layout. When i set setOnCLickListener for relative layout alone when there is no listview inside it it is working but not with listview inside it. The items in the listview will be displayed only when I click on the screen each time . I tried setItemOnClickListener.Thats not working too.
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="#drawable/bg">
<ListView
android:id="#+id/list_msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_below="#+id/meLbl"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:listSelector="#android:color/black"
android:transcriptMode="alwaysScroll"
android:divider="#null" />
<TextView
android:id="#+id/meLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Amanda"
android:textColor="#color/white"
android:textSize="20dp" />
<TextView
android:id="#+id/friendLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Janet"
android:textColor="#color/white"
android:textSize="20dp" />
</RelativeLayout>
The click is working only on the two text views not on the screen.
I want the click to work on the screen
for find out clicked on screen :
getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(SecondActivity.this, "click on screen ", Toast.LENGTH_SHORT).show();
}
return false;
}
});
and for find out clicked on listView :
listView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(SecondActivity.this, "Clicked On ListView", Toast.LENGTH_LONG).show();
return false;
}
});
How can I obtain an EditText as those bellow?
The x button removes the text inserted and the eye button displays the clear password as long as it's pressed. Note that I refer to them as buttons because I don't really know what they actually are nor if they are part of the EditText itself or if they are independent views.
You can just add a button at the right of your EditText
<FrameLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ScreenLoginContainer">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:hint="#string/enter_password_hint"
android:paddingRight="30dp"
style="#style/ScreenPasswordEditText"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+screen_card_ean_enter/show_password_button"
style="#style/ShowHidePasswordButton"/>
</FrameLayout>
You can use android:drawableRight
Set a Drawable to the right of the text, in EditText, in XML .
android:drawableRight="#drawable/Your_drawable"
You can use this :
android:drawableRight="#drawable/your_icon"
Also for click this answer
may help
This is the prefect way to create an EditText with cross button at the end:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<EditText
android:id="#+id/calc_txt_Prise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:textColor="#color/gray"
android:textStyle="bold"
android:hint="#string/calc_txt_Prise"
android:singleLine="true" />
<Button
android:id="#+id/calc_clear_txt_Prise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/delete" />
And now to clear the EditText upon click you can use
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditTextID.setText("");
}
});
And in order to make the password visible till the button is pressed, you can implement it this way:
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
The design support library has a setPasswordVisibilityToggleEnabled method: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)
This would be good for your password edittext, but not the username one.
I've got FrameLayout which consists of EditText and ImageButton and looks like this:
In the code it looks like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/input_field_red"
android:hint="Message"
android:singleLine="true"
android:textColor="#color/dark_grey_3" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/transparent"
android:src="#drawable/cancel" />
</FrameLayout>
Problem is, when input text becomes long enough, ImageButton overlays the text.
Is there a way to prevent this? I mustn't shorten the length of the line. It has to be match_parent. And ImageButton must be positioned on the line and aligned to right.
Do something like this -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/EditViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/input_field_red"
android:hint="Message"
android:layout_toLeftOf="#+id/imageViewId"
android:singleLine="true"
android:textColor="#color/dark_grey_3" />
<ImageButton
android:id="#+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/EditViewId"
android:layout_alignLeft="#+id/EditViewId"
android:layout_gravity="right"
android:background="#android:color/transparent"
android:src="#drawable/cancel" />
</RelativeLayout>
This prevent it from overlapting
Another Method
There is another method to add image at right of edittext.
<EditText
android:id="#+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dip"
android:layout_weight="1"
android:background="#drawable/textfield_search1"
android:drawableRight="#drawable/search_icon"
android:hint="Search Anything..."
android:padding="4dip"
android:singleLine="true" />
and setting onClickListener for image on Right
Let's say I have an EditText editComment with a drawableRight
editComment.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
}
}
return false;
}
});
UPDATE
My button is as big as the view of each ListItem - maybe this causes the problem? Is it possible to press once and let both OnItemclick and OnClick respond?
I have a list view with two textviews and a button. I know there are a lot of questions being posted but I have read a lot of them and not one seems to work.
I want the OnItemClick of each view of the list view to work.
I also want the OnClick of the button inside each view of the list view to work.
Only the onClick of the button seems to respond
In my xml i have set android:focusable="false" for the button. For the textviews I have set android:textIsSelectable="false" android:clickable="false" android:focusable="false"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rectangle_order"
android:layout_gravity="center_horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/order_height"
>
<Button
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="#dimen/text_size"
android:textColor="#color/white"
android:rotation="90"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/timer_size"
android:rotation="90"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="29dp"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
this is the layout for my list view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.5"
android:layout_width="#dimen/order_height"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none"
>
</ListView>
</RelativeLayout>
In the GetView of my adapter i have :
Button b = (Button) rowView.findViewById(R.id.img);
b.setBackgroundResource(R.color.pending);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
});
in my main activity i have a onItemClickListener set to the listView:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG_MAIN, "we are in select onitemclicklistener");
Toast.makeText(getApplicationContext(),"BOOOOH", Toast.LENGTH_SHORT).show();
}
});
Try to replace OnClickListener with OnTouchListener.
Like this:
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
return false;
}
};);
When you return false at this listener, you mean "hey i'm not done with that action(press down), please let the action to the next View"
Well friend ..
I have a design so:
The main Scroll is very necessary when appear the keyboard... If i dont make that, the button can't see...
The problem is when i try move inside the Edittext with the keyboard active...
I tried this:
android:scrollbars="vertical"
and this:
setMovementMethod(new ScrollingMovementMethod());
but not solve this yet
Anyone help me?
grax advance!
Check this without using scroll bar:
add permission in menifest file for corresponding activity.
android:windowSoftInputMode="adjustPan"
Please try this:
Your layout should be similar to example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="200dp"
android:layout_height="500dp"
android:text="some long text"
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
and in your java try this:
EditText edit = (EditText)findViewById(R.id.edittext);
edit.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edittext) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
It could be helpful.