Android clip custom view to path - java

I am trying to clip one custom view (linearlayout) to another (simple view). I want to clip the path of the linear layout to the path of the view which is a circle. Here are my custom views:
Circle
package classes;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class Circle extends View {
private Paint paint = new Paint();
private Path path = new Path();
public Circle(Context context) {
super(context);
}
public Circle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setColor(int color) {
paint.setColor(color);
invalidate();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
path = new Path();
path.addCircle(getWidth() / 2 - getWidth() / 5, getHeight() / 2 - getHeight() / 5, (getWidth() + getHeight()) / 4, Path.Direction.CW);
path.close();
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
public Path getPath() {
return path;
}
}
Linear Layout:
package classes;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class ClippedLinearLayout extends LinearLayout {
public ClippedLinearLayout(Context context) {
super(context);
this.setWillNotCacheDrawing(false);
invalidate();
}
public ClippedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setWillNotCacheDrawing(false);
invalidate();
}
public ClippedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotCacheDrawing(false);
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.clipPath(Blog.circle2.getPath());
}
}
What am i doing wrong?
EDIT:
My xml:
<RelativeLayout
android:id="#+id/menuButtonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<classes.Circle
android:id="#+id/circle2"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone" />
<classes.Circle
android:id="#+id/menuCircle"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#drawable/menu_button" />
</RelativeLayout>
<Button
android:id="#+id/button3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/close" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:background="#FF0000"
android:contentDescription="#string/ContentDescription"
android:gravity="center"
android:padding="10dp"
android:text="#string/failLogin"
android:textColor="#FFFFFF" />
<ScrollView
android:id="#+id/menuScrollView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:clickable="false">
<classes.ClippedLinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:visibility="gone"
android:orientation="vertical" />
</ScrollView>

Related

Show numeric keyboard with percent symbol

Is there an idea to show numeric soft keyboard with % symbol programmatically?
For example I need to enter 13% in EditText, so I need numbers and percent symbol and I don't want to show letters in soft keyboard.
The below code shows only numbers and .
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
Any help is appreciated
For a customer i have allowed the '-' char within the layout.xml file (android:digits attribute, see bellow):
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:maxLines="1"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:hint="#string/searchRecord_inputHint"
android:imeOptions="actionSearch"
android:inputType="number"
android:digits="1234567890-"
android:text="#={viewModel.textToSearch}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/switchSearchBy" />
Then, before searching i replace the '-' char occurences by '%'.
Of course, you must inform your users about this logic. Other solution is to build a simple Digits + '%' keyboard with buttons in a DialogFragment or even in your search layout directly.
Update: Integrated numeric keyboard view example
keyboard_num.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="myCls"
type="com.sample.keyboard.KeyboardNumeric"/>
</data>
<merge>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:columnCount="4"
android:rowCount="4">
<!--Row1-->
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_7"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="7"
android:layout_row="0"
android:layout_column="0"
android:onClick='#{(v)->myCls.onKey("7")}'/>
<Button
android:id="#+id/button_8"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="8"
android:layout_row="0"
android:layout_column="1"
android:onClick='#{(v)->myCls.onKey("8")}'/>
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_9"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="9"
android:layout_row="0"
android:layout_column="2"
android:onClick='#{(v)->myCls.onKey("9")}'/>
<Button
android:id="#+id/button_clr"
android:layout_width="120dp"
android:layout_height="64dp"
android:layout_row="0"
android:layout_column="3"
android:foreground="#drawable/ic_clear"
android:onClick='#{(v)->myCls.onClickClear()}'/>
<!--Row2-->
<Button
android:id="#+id/button_4"
android:layout_width="130dp"
android:layout_height="64dp"
android:layout_row="1"
android:layout_column="0"
android:text="4"
android:onClick='#{(v)->myCls.onKey("4")}'/>
<Button
android:id="#+id/button_5"
android:layout_width="130dp"
android:layout_height="64dp"
android:layout_row="1"
android:layout_column="1"
android:text="5"
android:onClick='#{(v)->myCls.onKey("5")}'/>
<Button
android:id="#+id/button_6"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="6"
android:layout_row="1"
android:layout_column="2"
android:onClick='#{(v)->myCls.onKey("6")}'/>
<Button
android:id="#+id/button_percent"
android:layout_width="120dp"
android:layout_height="64dp"
android:text="%"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="50sp"
app:autoSizeStepGranularity="2sp"
android:textStyle="bold"
android:visibility="invisible"
tools:visibility="visible"
android:layout_row="1"
android:layout_column="3"
android:onClick='#{(v)->myCls.onKey("%")}'/>
<!--Row3-->
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_1"
android:layout_height="64dp"
android:layout_width="130dp"
android:text="1"
android:layout_row="2"
android:layout_column="0"
android:onClick='#{(v)->myCls.onKey("1")}' />
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_2"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="2"
android:layout_row="2"
android:layout_column="1"
android:onClick='#{(v)->myCls.onKey("2")}'/>
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_3"
android:layout_width="130dp"
android:layout_height="64dp"
android:layout_row="2"
android:layout_column="2"
android:text="3"
android:onClick='#{(v)->myCls.onKey("3")}'/>
<Button
style="#style/KeyboardPluStyleBlue"
android:id="#+id/button_enter"
android:layout_width="120dp"
android:layout_height="64dp"
android:layout_rowWeight="2"
android:layout_row="2"
android:layout_rowSpan="2"
android:layout_column="3"
android:foreground="#drawable/ic_enter"
android:onClick='#{(v)->myCls.onClickEnter()}' />
<!-- Row 4-->
<Button
style="#style/KeyboardPluStyle"
android:id="#+id/button_0"
android:layout_width="0dp"
android:layout_height="64dp"
android:text="0"
android:layout_row="3"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:onClick='#{(v)->myCls.onKey("0")}'/>
<Button
style="#style/KeyboardPluStyleGray"
android:id="#+id/button_Cancel"
android:layout_width="130dp"
android:layout_height="64dp"
android:text="#string/adlb_dialog_cancel"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="50sp"
app:autoSizeStepGranularity="2sp"
android:layout_row="3"
android:layout_column="2"
android:onClick='#{(v)->myCls.onClickCancel()}'/>
</GridLayout>
The KeyboardNumeric class:
package com.sample.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.widget.LinearLayout;
import com.averydennison.foodxpress.R;
import com.sample.keyboard.databinding.KeyboardQtyBinding;
public class KeyboardNumeric extends LinearLayout {
private KeyboardNumBinding binding;
// constructors
public KeyboardNumeric(Context context) {
this(context, null, 0);
}
public KeyboardNumeric(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public KeyboardNumeric(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if(isInEditMode())//Uses LayoutInflater if you are in Android studio
LayoutInflater.from(getContext()).inflate(R.layout.keyboard_num, this, true);
else
init(getContext());
}
// Our communication link to the EditText
InputConnection inputConnection;
void init(Context context) {
binding= KeyboardNumBinding.inflate(LayoutInflater.from(context),(ViewGroup)this,true);
binding.setMyCls(this);
}
/**
* Call this on a key press
* #param value Key text value as String
*/
public void onKey(String value){
inputConnection.commitText(value, 1);
}
/**
* Call this when Enter key is pressed
*/
public void onClickEnter(){
inputConnection.performEditorAction(EditorInfo.IME_ACTION_DONE);
}
/**
* Call this when Clear key is pressed
*/
public void onClickClear(){
CharSequence currentText = inputConnection.getExtractedText(new ExtractedTextRequest(), 0).text;
CharSequence beforCursorText = inputConnection.getTextBeforeCursor(currentText.length(), 0);
CharSequence afterCursorText = inputConnection.getTextAfterCursor(currentText.length(), 0);
inputConnection.deleteSurroundingText(beforCursorText.length(), afterCursorText.length());
}
/**
* Clall this when Cancel key is pressed
*/
public void onClickCancel(){
inputConnection.performEditorAction(EditorInfo.IME_ACTION_SEND);
}
/** The activity (or some parent or controller) must give us
* a reference to the current EditText's InputConnection
*/
public void setInputConnection(InputConnection ic) {
this.inputConnection = ic;
}
}
EditText that avoids Soft input keyboard to be shown:
package com.sample.views;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
public class CustomEditText extends
androidx.appcompat.widget.AppCompatEditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private boolean softInputEnabled=false;
public void setSoftInputAllowed(boolean enabled){
softInputEnabled=enabled;
}
public boolean getSoftInputAllowed(){
return softInputEnabled;
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
preventSofInputShown();
lockContextMenu();
}
private void lockContextMenu(){
this.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//to keep the text selection capability available ( selection cursor)
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//to prevent the menu from appearing
menu.clear();
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
#Override
public boolean performClick() {
/* synchronized (this){
//Find the currently focused view, so we can grab the correct window token from it.
View view = getRootView().findFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null)
view = this;
else
this.clearFocus();
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}*/
if(softInputEnabled)
return false;
else
return true;
}
private void preventSofInputShown(){
this.setOnTouchListener((v, event) -> {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
return performClick();
default:
return false;
}
});
}
}
fragment_search.xml using CustomEditText + Keyboard:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.sample.views.CustomEditText
android:id="#+id/txtQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:ems="10"
android:inputType="number"
android:longClickable="false"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.sample.KeyboardNumeric
android:id="#+id/keyboardFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Finally, in your SearchFragment code (Attached to fragment_search.xml), just add this: (ie: in onCreate after inflating view)
// pass the InputConnection from the EditText to the keyboard
KeyboardNumeric keyboard = view.findViewById(R.id.keyboardFull);
EditText txtQty=view.findViewById(R.id.txtQty);
InputConnection ic = txtQty.onCreateInputConnection(new EditorInfo());
KeyboardQty.setInputConnection(ic);

My TextView Doesn't Seen

I want to see on the screen a Button object, a TextView object and a MyView object. But only appear Button object and MyVıew object. I think , MyView overlapping(above the TextView) with TextView. Because If I don't add(b.addView(a);)MyView object to my layout, Button and TextView objects appear on the screen. But If I add(b.addView(a);) MyView , TextView is gone. How can I solve this problem?
MyView.java file:
package com.example.mehmet.catchtheball;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyView extends View {
public MyView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("lightGray"));
canvas.drawCircle(500, 500, 150, paint);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Customer.java file :
package com.example.mehmet.catchtheball;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class Customer extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
MyView a = new MyView(this);
RelativeLayout b = (RelativeLayout)findViewById(R.id.relativeLayout);
b.addView(a); // If I do this , TextView gone.
final TextView label = (TextView) findViewById(R.id.textView3);
label.setText("HelloEveryOne"); // My TextView
a.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
}
}
activity_customer.xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mehmet.catchtheball.main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="undo"
android:text="#string/undo" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
tools:textColor="#android:color/background_dark" />
</RelativeLayout>
Try with this activity_customer.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.mehmet.catchtheball.main">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="undo"
android:text="#string/undo" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
tools:textColor="#android:color/background_dark" />
</RelativeLayout>
Add a LinearLayout as container below textView3 and add all to then
In the XML here you posted, add a with layout_belowOf=textView3, at your code where you do b.addView(a) replace with: LinearLayout c = findViewById(R.id.ll_c_container) and c.addView(a)
If you want to all components to be in an linear axis you can have just one linearlayout (all addViews will add below the last view)
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="undo"
android:text="#string/undo" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginStart="27dp" />
</RelativeLayout>
In your java class just do this one (Sorry i am writing kotlin code
MyView a = new MyView(this);
val params = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, textView3.id)
params.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE)
relativeLayout.addView(textView, params)

Button touch listener blocking custom scrollview touch event listener

I have a custom diagonal scrollview like this
public class Vscroll extends ScrollView {
public Vscroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Vscroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Vscroll(Context context) {
super(context);
}
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
and
public class Hscroll extends HorizontalScrollView {
public Hscroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Hscroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Hscroll(Context context) {
super(context);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
This is the XML
<?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">
<com.cukil.schoolfrenzy.Vscroll android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_width="fill_parent" android:id="#+id/vScroll">
<com.cukil.schoolfrenzy.Hscroll android:id="#+id/hScroll"
android:scrollbars="none"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/peta_container"
android:background="#drawable/peta_kota" >
<ImageView
android:id="#+id/bSekolah"
android:layout_width="120dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="114dp"
android:layout_marginTop="248dp"
android:src="#drawable/tmuka1" />
</RelativeLayout>
</com.cukil.schoolfrenzy.Hscroll>
</com.cukil.schoolfrenzy.Vscroll>
<RelativeLayout
android:id="#+id/dashboard"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/tNama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#fff"
android:text="test"
android:textSize="20sp" />
<Button
android:id="#+id/bPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/b_pause" />
</RelativeLayout>
</RelativeLayout>
And this is the touch event listener
public boolean onTouchEvent(MotionEvent event) {
float curX, curY;
final ImageView bSekolah = (ImageView) findViewById(R.id.bSekolah);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mx = event.getX();
my = event.getY();
break;
case MotionEvent.ACTION_UP:
curX = event.getX();
curY = event.getY();
if (mx == curX && my == curY) {
bSekolah.setVisibility(View.VISIBLE);
bSekolah.callOnClick();
} else {
bSekolah.setVisibility(View.INVISIBLE);
}
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
break;
case MotionEvent.ACTION_MOVE:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
isBeingDragged = true;
mx = curX;
my = curY;
break;
}
return false;
}
I want to give the bSekolah a click listener, and when I clicked it, application goes into another activity.
But the problem is, when I drag the scroll started in bSekolah's position, the scrollview goes messy.
Please tell me the problem, help :)

Custom View not showing: no height, but still width

In my current project I run into an dead end. My custom view, only created to show a specific color, won't show in the view of my emulator (although it seems to show up correctly in the editor of Eclipse) except if it had a minimum height, but then it only get this exact height. I want it to match_parent.
The problem might be that it is in the item layout of a list view.
That's the code of my Custom View:
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class ColorView extends View {
private int mColor;
private Context mContext;
private int width;
private int height;
public ColorView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorView, 0, 0);
try {
mColor = a.getColor(R.styleable.ColorView_color, 0xFFFFFF);
} finally {
a.recycle();
}
}
public void setColor(int hexColor) {
mColor = hexColor;
invalidate();
}
public void setColorFromResource(int resID) {
mColor = mContext.getResources().getColor(resID);
invalidate();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.d("DEBUG", Integer.toString(w) + " " + Integer.toString(h));
width = w;
height = h;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(mColor);
Log.d("DEBUG", "drawn");
}
}
And a simplified version of my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:raabeapp="http://schemas.android.com/apk/res/com.example.myapp"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/row_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.myapp.ColorView
android:id="#+id/status_view"
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:minHeight="50dp"
raabeapp:color="#FF0000" />
<TextView
android:id="#+id/hour_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/status_view"
android:text="1"
android:textSize="#dimen/hour_textsize"
android:width="70dp" />
<TextView
android:id="#+id/text_hourtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/hour_view"
android:text="#string/hour_text"
android:textSize="#dimen/hourtext_textsize" />
Ok, I figured out a workaround:
When I use a LinearLayout instead of a RelativeLayout I get the result I want.
It seems that the problem lies in the RelativeLayout. If someone knows why this is, or how I could still use a RelativeLayout I would be very interested. This answer does not provide a full solution, but in case someone runs into my problem it could provide a ugly workaround.
Use android:layout_height="match_parent" for your RelativeLayout.

Expand ImageView to maximum possible maintaining aspect ratio in Android

I have found lots of answers in this site but they are not working or just for single cases. In my case:
I would like to load an image and show it into a ImageView.
The width of the ImageView must be the maximum possible: parent layout width.
The height must be the one that allows re-scale the image maintaining the aspect ratio.
How do achieve this? Here is my layout with the ImageView inside (currently I have been using fitXY but the image does not preserve the aspect ratio, even if using adjustViewBounds:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutPictureGalleryItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#E0E0DE"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >
<TextView
android:id="#+id/textName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="esto es una prueba para ver cómo queda el layout"
android:textColor="#000000"
android:textSize="12sp"
android:typeface="serif"
android:padding="2dp" />
<TextView
android:id="#+id/textDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="2dp" />
</LinearLayout>
</LinearLayout>
Use this widget instead of your imageview and let the width match parent:
package com.myapp.widgets;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context)
{
super(context);
}
public ResizableImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ResizableImageView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Drawable drawable = getDrawable();
if (drawable != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
if (diw > 0)
{
int height = width * drawable.getIntrinsicHeight() / diw;
setMeasuredDimension(width, height);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

Categories