How to highlight the selected item in linear layout in android - android

I am having linear layout, in which it has one imageview and one textview, when i touch the linear layout, it should highlight the image as well as text inside the linear layout with different color. I have tried using set background for the linear layout when it is pressed since i have used ontouch-listener it doesn't shows the color even i tried to change the colors programmatically but it didn't works.
Can anyone tell me how i can change the imageview and textview color when a linear layout is touched.
Xml:
<LinearLayout
android:id="#+id/linear_otherexpense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#drawable/linearlayout_check"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/img_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:scaleType="centerInside"
android:src="#mipmap/expense"
android:tint="#6666FF" />
<TextView
android:id="#+id/tv_otherexpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="#string/dayexpense"
android:tint="#color/colorPrimary" />
</LinearLayout>
Selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
Code:
li_otherexpense.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(NewDaybook_Activity.this, AddingNewExpense.class));
NewDaybook_Activity.this.finish();
return false;
}
});

You can use this code. this is working fine for me .May be it will helpfull
findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
// When you Touch Down
// U can change Text and Image As per your Functionality
break;
case MotionEvent.ACTION_UP:
//When you Release the touch
// U can change Text and Image As per your Functionality
break;
}
return true;
}
});

Try this,
linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
imageView=(ImageView)findViewById(R.id.img_otherexpense);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
return true;
}
});

Related

NestedScrollView with constraint layout as child not scrolling

I want to scroll the whole layout above keyboard but its not working. I was able to scroll manually but want it to scroll
programatically when a user sets focus on the edit text.
Please help me. Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true"
android:paddingBottom="?attr/actionBarSize">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
......
edit_text_code.setOnFocusChangeListener { view, hasFocus ->
if (hasFocus) {
scrollView.post {
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN)
}
}
}
Method 1:
First get the x/y coordinates of your Edittext using View.getLocationOnScreen (int[] location) ( After the method returns, the array contains the x and y location in that order)
Then do :
scrollView.scrollTo(x, y);
If above method doesn't work try below method :
Method 2:
edit_text_code.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// put your edit text id below
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
XML Code :
<EditText
android:id="#+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText>

Android : flowlayout inner button with close icon onclick event

I have a requirement to create a button with close icon like as below:
i have use button and i have fatch problem when i click on button remove button.
but my requirement is onclick close icon when remove form list.
so please help me which control use so my problem solution.
Use this code it will solved your problem
buttondelete.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (buttondelete.getRight() - buttondelete.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
return true;
}
}
return false;
}
});
i would add to flow layout customLaout which contains of grey background and linear layout with 2 elements inside. TextView and ImageView.
Both has own ClickListeners
something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="Example Text"
android:id="#+id/buttonTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/buttonExitIcon"
android:src="#drawable/your_image_source"
/>
</LinearLayout>
and add Click listeners for both imageView and TextView
{
RelativeLayout mainView = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
Button bt_close = (Button)child.findviewById(R.id.btnclose);
bt_put.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout pView = (RelativeLayout)v.getParent();
mainView.removeView(pView);
}
});
mainView.addView(child);
}
Create a child.xml using the RelativeLayout with childs (TextView for text and Button as Remove button)
Inflate the xml layout , add each views into your main_layout and
setonclicklistner to button.
when clicked on button , get the parent view from the clicked view and remove the view from main_layout.
this may help you.

EditText event propagation

I have a custom view and EditText element in my relative layout, i can't receive touch events in my custom view, how can i propagate touch events to my custom view;
My layout looks like ;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" >
<com.test.ui.CustomView
android:id="#+id/noteView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="#+id/etPageInput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top" />
</RelativeLayout>
I add touch listener to my EditText and Custom view as like ;
EditText pageInput = (EditText) findViewById(R.id.etPageInput);
pageInput.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
CustomView pageView = (CustomView) findViewById(R.id.noteView);
pageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch event received by custom view !!!")
return true;
}
});
I'm not able to receive touch events in my CustomView, any idea what am i missing ?
try to set clickable as true in xml for your custom view
and
try to setonclicklisener

Pass click event through the view android

I have frame layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center" >
</LinearLayout>
<LinearLayout
android:id="#+id/widgetOverlayFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
</LinearLayout>
First layout contains widget layout, and second one is transparent and receives onLongClick for dragging method.That works, the problem is when i want to interact with widget, to click something, click event is taken by overlayFrame. How can i pass click event trough overlayFrame to widgetView?
EDIT:
Now I'm trying to attach GestureLister to overlayFrame LinearLayout, to somehow see difference between MotionEvent that represent single click and long click. The problem that I'm facing is that OnLongPress in gesture listener is always called whatever I do, single click or long click.
Is there an explanation for this behavior? Tnx!
In my case I added flag to layoutParams of my view, which should have passed a touch event under:
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
This way view will ignore all touches (like view with visibility GONE)
Instead of using GestureListener you can override the onTouchListener in this way.
This will call longPress when the timer runs out and if an up comes in between it will cancel the LongPress
CountDownTimer c;
long time=5000;
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
c= new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
time=millisUntilFinished;
}
public void onFinish() {
Log.i("","LONG PRESS");
}}.start();
break;
case MotionEvent.ACTION_UP:
if (time>0) {
Log.i("example","short click");
c.cancel();
}
break;
}
return true;
}
Maybe setting the overlay's attribute android:focusable="false" or android:focusableInTouchMode="false" would be enough.
You could also make your own overlay widget and override its onInterceptTouchEvent method to return false always.
public class NotTouchableLinearLayout extends LinearLayout {
// Override constructors
// ...
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
Instead of setting a GestureListener on the top layout, you should create your own class that extends LinearLayout and to override it's onTouchEvent method.
There you can implement the logic of long click \ short click etc.
The click events will first be sent to the widget layout, and only if it doesn't handle them (hence it's onTouchEvent returns false), you will get them on the top layout.
edit:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center" >
</LinearLayout>
<com.example.touchtesting.MyLinearLayout
android:id="#+id/widgetOverlayFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
</com.example.touchtesting.MyLinearLayout>
</FrameLayout>
change the com.example.touchtesting to the name of your package.
and this is the class:
public class MyLinearLayout extends LinearLayout{
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
private long startClick = 0;
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startClick = ev.getEventTime();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (ev.getEventTime() - startClick < 500) {
Log.i("example","short click");
}
else {
Log.i("example","long click");
}
break;
}
return true;
}
}
To get the long clicks to pass through the widgetView LinearLayout add android:longClickable="true". (For ordinary clicks add android:clickable="true")
So widgetView becomes:
<LinearLayout
android:id="#+id/widgetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:longClickable="true">
</LinearLayout>

android-How to put scroll view under scroll view

My application need to 2 scroll views(sv1,sv2). sv1 is main scroll view and sv2 is sub scroll view of sv1.
is it possible? then how to do it.
Following one is my answer. it is working in simulator only but not working in device means device support sv1 only sv2 is not working.
How to do it?
thanks
<ScrollView android:id=”#+id/parent_scroll”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:background=”#drawable/dotted_bg”
android:focusableInTouchMode=”false”>
<LinearLayout />
<LinearLayout />
<LinearLayout >
<ScrollView android:id=”#+id/child_scroll”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#drawable/text_box_bg”>
<TextView android:id=”#+id/text_description”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:textColor=”#color/gray”
android:textSize=”12dip”
android:padding=”5dip”
android:scrollbars=”vertical”/>
<!–ScrollView>
</LinearLayout>
Step 1 : Provide unique id to both the scrollview.
Step 2 : get reference of that two scrollview in your activity.
parentScroll=(ScrollView)findViewById(R.id.parent_scroll);
childScroll=(ScrollView)findViewById(R.id.child_scroll);
Step 3: Now set touch listeners for both.
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG,”PARENT TOUCH”);
findViewById(R.id.child_scroll).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
Log.v(TAG,”CHILD TOUCH”);
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Done …

Categories

Resources