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;
}
});
Related
Currently i am unable to click on the buttons when the palm is rested on the screen.Want to able to touch yes/no buttons even though palm rested on screen.Please check below image for more information.
My activity code:: overide onTouch method of yes/no buttons not calling when palm rested and touching buttons
no_btn = (Button) findViewById(R.id.no_btn);
yes_btn = (Button) findViewById(R.id.yes_btn);
txt = (TextView) findViewById(R.id.txt);
bg = (LinearLayout) findViewById(R.id.root);
views_parent = (LinearLayout) findViewById(R.id.root1);
//views_parent.setFocusable(true);
views_parent.setFocusableInTouchMode(true);
bg.setFocusableInTouchMode(false);
bg.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent arg1) {
return true;
}
});
yes_btn.setId(1);
no_btn.setId(0);
no_btn.setOnTouchListener(this);
yes_btn.setOnTouchListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("child onTouch>>>","onTouch>>>2>>"+v.getId());
if (v.getId() == 0)
{
isTouchable = false;
}else if (v.getId() == 1)
{
isTouchable = false;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/root"
tools:context="com.example.build.multitouch.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/txt"/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:id="#+id/root1">
<Button
android:id="#+id/no_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="0"
android:text="no"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/yes_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="0"
android:text="yes"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
There are only two items in my listview and it covers half of screen but listview still shows scroll around it. I have set height to wrap_content
layout code is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
/>
</LinearLayout>
I don't want scroll to appear on listview because it will have only two items and there is no need to show. I am not sure what is causing scroll to appear.
Any help would be highly appreciated.
Thanking you in advance
UPDATE
Here is my row item xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/roundedshape"
android:alpha="0.7">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="desc"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/ic_insert_emoticon_black_48dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="5dp"
android:text="Assignments"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#000"/>
<TextView
android:id="#+id/last_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title"
android:layout_below="#+id/title"
android:text="Last Update : 01-01-2017"
android:textSize="13sp"
android:textColor="#696969"/>
<TextView
android:id="#+id/unread_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/title"
android:layout_alignBottom="#+id/title"
android:layout_alignParentRight="true"
android:text="04"
android:textSize="12sp"
android:background="#drawable/shape_circular"
android:padding="5dp"
android:textColor="#FFF"
android:textStyle="bold"
android:layout_marginRight="5dp"
/>
</RelativeLayout>
<ListView
...
android:scrollbars="none"
...
/>
try adding:
android:scrollbars="none"
user android:scrollbar="none" like this;
<ListView
android:id="#+id/list_view_meals_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
To hide scrollbars
do this
android:scrollbars="none"
To disable scrolling do this
listView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
PS: I think you have messed up your item view which includes icon title and description.
Use this code .....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="your text view"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="#+id/text_ap"/>
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:layout_above="#id/text_ap"
android:layout_height="match_parent"/>
</RelativeLayout>
try this :
to hide the scrollbar
<ListView
android:id="#+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:scrollbars="none" // add this
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
/>
or in your code:
yourlistView.setVerticalScrollBarEnabled(false);
To disable Scrolling of listview Items try:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Action will not be forwarded
}
return false;
}
});
OR
yourListView.setScrollContainer(false);
I know it's an old question i was searching for solution and i remembered that i use it in another project and i forgot the owner of the solution so here is the code :
public static void justifyListViewHeightBasedOnChildren (ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.
The XML looks like... (But will change slightly based on the answer to another question I posted)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_vertical"
android:background="#color/black"
android:id="#+id/Swipe"
>
<LinearLayout
android:id="#+id/LeftContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/yellow"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<Button
android:id="#+id/ApproveButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#2796C3"
android:text="Approve"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/ApproveUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#215681"
android:text="Undo"
android:layout_toRightOf="#id/ApproveButton"
/>
</LinearLayout>
<LinearLayout
android:layout_alignParentRight="true"
android:id="#+id/RightContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="horizontal"
>
<Button
android:id="#+id/DenyButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Deny"
/>
<Button
android:id="#+id/DenyUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#860000"
android:text="Undo"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/TopContentView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1F1F1F">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="match_parent" >
<ImageView
android:id="#+id/UnreadImage"
android:layout_height="match_parent"
android:layout_width="7dp"
android:src="#drawable/vertical_blue_bar"
android:background="#2796C3"/>
<LinearLayout
android:id="#+id/ListText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:padding="12dp">
<TextView
android:id="#+id/Text_Line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="11dip"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
OriginalTouchX = Math.Ceiling(e.RawX);
index = e.ActionIndex;
pointerId = e.GetPointerId(index);
VelocityTracker.AddMovement(e);
break;
case case MotionEventActions.Move:
VelocityTracker.AddMovement(e);
ChangeX(newX);
VelocityTracker.ComputeCurrentVelocity(1);
float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));
if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
{
QuickApprove();
}
break;
}
}
void QuickApprove()
{
ToggleUndoButton(true, true);
WaitingForUndo = true;
ChangeX(DragThreshold);
this.ApproveTimer.Start(true);
}
private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
{
this.ApproveUndoButton.Visibility = ViewStates.Visible;
this.ApproveButton.Text = "Approving...";
}
private void ChangeX(int newX)
{
int width = this.TopContentView.Right - this.TopContentView.Left;
this.TopContentView.Left = newX;
this.TopContentView.Right = this.TopContentView.Left + width;
}
If in the ToggleUndoButton method I comment out the line
this.ApproveUndoButton.Visibility = ViewStates.Visible;
Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.
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);
}
}
};
I am facing one issue while using customRangeSeekBar in android when rangeseekbar movies based on Edittext values my RangeSeekbar values Startingvalue startingvalue 0 and endingvalue 30
My requirement is suppose i will take two edittexts
firstedittext value is 10 and second edittextvalue is 20 based on this values RangeSeekbar will move
I wrote xml like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<!-- <RelativeLayout
android:id="#+id/relative1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout> -->
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relative1"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="Start"
android:paddingBottom="5dp"
android:textColor="#0072ff"
android:textStyle="bold" />
<EditText
android:id="#+id/edt_starttime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
>
<TextView
android:id="#+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:text="End"
android:textColor="#0072ff"
android:textStyle="bold"/>
<EditText
android:id="#+id/edt_endtime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relative2"
android:orientation="horizontal"
android:layout_margin="10dp"
android:id="#+id/layout">
</LinearLayout>
</RelativeLayout>
I used custom RangeSeekbar class like
RangeSeekBar.java
this class is developed based on this link
and finally my activity class is
MainActivity2.java
public class MainActivity2 extends Activity
{
//declaring widgets and variables...............
WebView webView1;
TextView start,end;
EditText edt_starttime,edt_endtime;
String url,edt_starttime1;
View handler1;
int minValue1;
#Override
//onCreate() starts............
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting refrences of widgets.......
webView1=(WebView)findViewById(R.id.webView1);
start=(TextView)findViewById(R.id.start);
end=(TextView)findViewById(R.id.end);
edt_starttime=(EditText)findViewById(R.id.edt_starttime);
edt_endtime=(EditText)findViewById(R.id.edt_endtime);
edt_starttime = (EditText) findViewById(R.id.edt_starttime);
// edittext listenr starts...........
// editettxt start listener ends.........
//editetxt end listener starts..............
edt_endtime.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE)
{
Log.e("Ram san","jhgkfdjgh");
String edt_endtime1=edt_endtime.getText().toString();
if(edt_endtime1.length()!=0)
{
minValue1=Integer.parseInt(edt_endtime1);
Log.e("", ""+minValue1);
}
return true; // consume.
}
// pass on to other listeners.
return false;
}
});
//edittext listeners ends.................
// create RangeSeekBar as Integer range between 20 and 75
RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(0, 30, MainActivity2.this);
seekBar.setLeft(25);
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>()
{
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, final Integer minValue, Integer maxValue)
{
edt_starttime.setText(""+minValue);
edt_endtime.setText(""+maxValue);
Log.e("values ", ""+ minValue + ", MAX=" + maxValue);
}
});
// add RangeSeekBar to pre-defined layout
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
// layout.setP
layout.addView(seekBar);
}
//onCreate() ends.................
}
How can I set EditText values to the RangeSeekbar when edittext values are based on moving the rangeseekbar?