Changing EditText height and width on Runtime with some animation - android

I have Four EditText with different background.
It looks like this:
I want to cover full screen when a EditText is selected with That EditText. For that I need to change the EditText width and height with some animation on Runtime.
When selected It should look like this:
How can I change EditText size with animation on Runtime ?

ANSWER UPDATED :
use this code in drawable/animation_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="500" />
And set the animation to the edit text using :
Animation anim=AnimationUtils.loadAnimation( this, R.anim.righttoleft);
editText = (EditText)findViewById(R.id.editText1);
Display display = getWindowManager().getDefaultDisplay();
int width=display.getWidth();
int height = display.getHeight();
editText.setWidth(width);
editText.setHeight(height);
editText.startAnimation(anim);

I'm not sure if it can be done with Animations. In android view takes all space before animation finished, so you will see that other editTexts disappears and selected one slowly increasing. Here is rough example how to do it without standart animations, but changing weights in separate thread:
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll0"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<EditText
android:id="#+id/et00"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
android:text="00" />
<EditText
android:id="#+id/et01"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
android:text="01" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<EditText
android:id="#+id/et10"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
android:text="10" />
<EditText
android:id="#+id/et11"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
android:text="11" />
</LinearLayout>
</LinearLayout>
and in code add actions on changing focus:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OnFocusChangeListener focusListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
LinearLayout parent = (LinearLayout) v.getParent();
EditText forDecresing = null;
for (int i = 0; i < parent.getChildCount(); i++) {
if (parent.getChildAt(i) != v) {
forDecresing = (EditText) parent.getChildAt(i);
break;
}
}
LinearLayout pp = (LinearLayout) parent.getParent();
LinearLayout layoutForDecreasing = null;
for (int i = 0; i < pp.getChildCount(); i++) {
if (pp.getChildAt(i) != parent && pp.getChildAt(i) instanceof LinearLayout) {
layoutForDecreasing = (LinearLayout) pp.getChildAt(i);
break;
}
}
startAnimation((EditText) v, forDecresing, layoutForDecreasing, parent);
} else {
}
}
};
((EditText) findViewById(R.id.et00)).setOnFocusChangeListener(focusListener);
((EditText) findViewById(R.id.et01)).setOnFocusChangeListener(focusListener);
((EditText) findViewById(R.id.et11)).setOnFocusChangeListener(focusListener);
((EditText) findViewById(R.id.et10)).setOnFocusChangeListener(focusListener);
}
public void onBackPressed() {
setWeight(findViewById(R.id.et00), 1);
setWeight(findViewById(R.id.et01), 1);
setWeight(findViewById(R.id.et11), 1);
setWeight(findViewById(R.id.et10), 1);
setWeight(findViewById(R.id.ll1), 1);
setWeight(findViewById(R.id.ll0), 1);
}
Thread animationThread;
private void startAnimation(final EditText forIncreasing, final EditText forDecresing, final LinearLayout layoutForDecreasing,
final LinearLayout layoutForIncreasing) {
if (animationThread != null)
animationThread.interrupt();
animationThread = new Thread(new Runnable() {
#Override
public void run() {
int iterations = 0;
int maxIterations = 30;
setWeight(forIncreasing, maxIterations - 1);
setWeight(layoutForIncreasing, maxIterations - 1);
setWeight(forDecresing, maxIterations - 1);
setWeight(layoutForDecreasing, maxIterations - 1);
while (iterations < maxIterations) {
iterations++;
setWeight(forIncreasing, maxIterations - 1 + iterations);
setWeight(layoutForIncreasing, maxIterations - 1 + iterations);
setWeight(forDecresing, maxIterations - 1 - iterations);
setWeight(layoutForDecreasing, maxIterations - 1 - iterations);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
return;
}
}
animationThread = null;
}
});
animationThread.start();
}
private void setWeight(final View view, final float weight) {
runOnUiThread(new Runnable() {
#Override
public void run() {
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.weight = weight;
view.setLayoutParams(params);
}
});
}
I do not know if this is possible for you, but in this example you can add some more movements quite easy.
I do not recommend use it production, if only you have some other options.

I have tried to explain my Logic for your problem...i hope so it would work for you..
EditText editText = new EditText(this);
editText.setOnClickListener(new OnClickListener() {
public void onClick(){
// Before getSize was introduced (in API level 13), you could use
// the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
editText.setWidth(width);
editText.setHeight(height);
}
}

Related

How to get First Visible child item of android Horizontal scrollview on scrollChange event

I need to find the first visible child item of android Horizontal scrollview on scrollChange event. I have followed this link How to get First Visible child item of android scrollview on scrollChange event
but couldn't find what rowView is.
horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollPos = horizontalScrollView1.getScrollX();
Log.e("scroll x:", scrollPos + "");
}
});
layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:id="#+id/layLandmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:id="#+id/viewWhite"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerInParent="true"
android:background="#color/white" />
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
Class file:
ImageView iv;
RelativeLayout layLandmarks = (RelativeLayout) findViewById(R.id.layLandmarks);
FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(2000, FrameLayout.LayoutParams.WRAP_CONTENT);
layLandmarks.setLayoutParams(lp1);
JSONObject landmarks = jsonObject1.getString("landmarks");
JSONArray jsonArray1 = new JSONArray(landmarks);
for (int j = 0; j < jsonArray1.length(); j++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
landmarkId = jsonObject2.getString("id");
landmarkName = jsonObject2.getString("title");
landmarkDesc = jsonObject2.getString("description");
latitude = jsonObject2.getDouble("latitude");
longitude = jsonObject2.getDouble("longitude");
video_time = jsonObject2.getString("video_time_in_sec");
// Log.e("video_time_in_sec", video_time);
landmarkIcon = jsonObject2.getString("image");
iv = new ImageView(VideoPreviewWithRecycler.this);
iv.setId(i);
Picasso
.with(VideoPreviewWithRecycler.this)
.load(landmarkIcon)
.resize(40, 45)
.into(iv);
params = new RelativeLayout.LayoutParams(40, 45);
params.topMargin = 0;
params.leftMargin = Integer.parseInt(video_time);
params.addRule(RelativeLayout.RIGHT_OF, 0);
layLandmarks.addView(iv, params);
}
Update:
horizontalScrollView1.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View v, final int scrollX, int scrollY, final int oldScrollX, int oldScrollY) {
horizontalScrollView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int firstVisibleItemIndex= getFirstVisibleItem1(horizontalScrollView1);
Log.e("firstVisibleItemIndex >>",firstVisibleItemIndex+"");
}
});
}
});
private int getFirstVisibleItem1(HorizontalScrollView scrollView) {
ViewGroup viewGroup = (ViewGroup) scrollView.getChildAt(0); // cast to ViewGroup here because I think we only need to getFirstVisibleItem if ScrollView have multiple items
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
if (view.getX() + view.getWidth() >= scrollView.getScrollX()) {
return i; // if you want to receive the position, return i here
}
}
return 0;
}
I think it may help
private View getFirstVisibleItem(HorizontalScrollView scrollView) {
ViewGroup viewGroup = (ViewGroup) scrollView.getChildAt(0); // cast to ViewGroup here because I think we only need to getFirstVisibleItem if ScrollView have multiple items
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
if (view.getX() + view.getWidth() >= scrollView.getScrollX()) {
return view; // if you want to receive the position, return turn i here
}
}
return null;
}
The idea is check each view x position and scroll x value
I implemented some basic scrolling animation in my app. I used this piece of code and it is working perfectly fine.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
case RecyclerView.SCROLL_STATE_DRAGGING:
case RecyclerView.SCROLL_STATE_SETTLING:
currentItemPosition=linearLayoutManager.findFirstCompletelyVisibleItemPosition();
}
}
my requirement was to find the position during settling of scroll ,you can try the same during SCROLL_STATE_IDLE
There are few methods but I prefer only one.
All Views have "boolean View.getGlobalVisibleRect(..)" ( https://developer.android.com/reference/android/view/View.html#getGlobalVisibleRect(android.graphics.Rect) ) method that returns FALSE if the View is completly hidden by its parent.
So all HorizontalScrollView's child has that HScrollView as its parent and if that method returns FALSE it means that View is not visible. The first child returning TRUE is the first visible child inside that HScrollView.
You can use Linearlayout Instead of RelaytiveLayout as a child of HorizontalScrollView. Like this:
<?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="wrap_content">
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/layLandmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp" />
</HorizontalScrollView>
</RelativeLayout>
Note: You can also use custom layout to inflate in views in LinearLayout for more performance in case of nested Layouts. (I used a TextView not Custom Layout here)
In your activity use something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HorizontalScrollView horizontalScrollView = findViewById(R.id.horizontalScrollView);
TextView iv;
final LinearLayout layLandmarks = findViewById(R.id.layLandmarks);
for (int j = 0; j < 10; j++) {
iv = new TextView(this);
iv.setId(j);
iv.setText("TEXT " + j);
iv.setPadding(20, 20, 20, 20);
layLandmarks.addView(iv);
}
horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int firstVisibleItemIndex = getFirstVisibleItem(layLandmarks, horizontalScrollView);
Log.e("ItemIndex >>", firstVisibleItemIndex + "");
}
});
}
Now Use the code below. It modified version of #Phan Van Linh Answer.
private int getFirstVisibleItem(LinearLayout linearLayout, HorizontalScrollView horizontalScrollView) {
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View view = linearLayout.getChildAt(i);
if (view.getX() + view.getWidth() >= horizontalScrollView.getScrollX()) {
return i; // if you want to receive the position, return i here
}
}
return 0;
}
I Hope this will solve your problem. :)

Android: populating Linear Layout programmatically, It gets extra space

I'm trying to populate a LinearLayout horizontally with some ImageViews programmatically. In horizontal directon, everything works very well, but it reserves extra space vertically what I don't want.
Here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="es.uam.dadm.jacopo_grassi_connecta4.Settings" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/your_color_container" >
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adv_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/adv_color_container" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sounds" />
<Switch
android:id="#+id/sounds_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
and here's the java code:
private PlayerDataSource playersdb;
private Player player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Bundle extras = getIntent().getExtras();
playersdb = new PlayerDataSource(this);
player = playersdb.getPlayer(extras.getString(Utils.PARAM_PLAYER_ID));
buildColors();
Switch sounds = (Switch)findViewById(R.id.sounds_switch);
sounds.setOnCheckedChangeListener(this);
sounds.setChecked(player.getSounds() == 0 ? false : true);
}
#Override
public void onClick(View v) {
Integer tag = (Integer) v.getTag(R.id.TAG_ADV);
if(tag == null){ //your color
tag = (Integer) v.getTag(R.id.TAG_YOU);
player.setColor(tag);
}else{ //adv color
player.setColorAdv(tag);
}
playersdb.updatePlayer(player);
buildColors();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
player.setSounds(1);
}else{
player.setSounds(0);
}
playersdb.updatePlayer(player);
}
private void buildColors(){
LinearLayout parent = (LinearLayout)findViewById(R.id.your_color_container);
parent.removeAllViewsInLayout();
for (int i = 0; i < 5; ++i) {
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 0.2f;
ImageView v = new ImageView(this);
Integer drawable = null;
switch (i){
case 0:
drawable = R.drawable.red_piece;
break;
case 1:
drawable = R.drawable.yellow_piece;
break;
case 2:
drawable = R.drawable.green_piece;
break;
case 3:
drawable = R.drawable.purple_piece;
break;
case 4:
drawable = R.drawable.azure_piece;
break;
}
v.setLayoutParams(params);
v.setImageResource(drawable);
v.setTag(R.id.TAG_YOU, drawable);
v.setOnClickListener(this);
if (drawable.equals(player.getColor())) {
v.setBackgroundColor(getResources().getColor(R.color.azul));
}
parent.addView(v);
}
parent = (LinearLayout)findViewById(R.id.adv_color_container);
parent.removeAllViewsInLayout();
for (int i = 0; i < 5; ++i) {
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 0.2f;
ImageView v = new ImageView(this);
Integer drawable = null;
switch (i){
case 0:
drawable = R.drawable.red_piece;
break;
case 1:
drawable = R.drawable.yellow_piece;
break;
case 2:
drawable = R.drawable.green_piece;
break;
case 3:
drawable = R.drawable.purple_piece;
break;
case 4:
drawable = R.drawable.azure_piece;
break;
}
v.setLayoutParams(params);
v.setImageResource(drawable);
v.setTag(R.id.TAG_ADV, drawable);
v.setOnClickListener(this);
if (drawable.equals(player.getColorAdv())) {
v.setBackgroundColor(getResources().getColor(R.color.azul));
}
parent.addView(v);
}
}
and that's the result:
Obviously, the images are perfect squares.
What am I doing wrong?
Does it work if you change the android:layout_height of your outer LinearLayout to "wrap_content"?
Try to make your LinearLayout.Params width & height to "LayoutParams.WRAP_CONTENT" & also height of linear layout in XML file
This is how to solve your problem.
(the below code should be added into your for loop before parent.addView(v)
You need to use a Runnable thread in order to get the Views width which will be set as the height at runtime or else you'll be trying to set the Height before you know the View's width because the layout hasn't been drawn yet.
v.post(new Runnable() {
#Override
public void run() {
LinearLayout.LayoutParams mParams;
mParams = (LinearLayout.LayoutParams) v.getLayoutParams();
mParams.height = v.getWidth();
v.setLayoutParams(mParams);
v.postInvalidate();
}
});
v.postInvalidate() is called to force the view to redraw while on a separate thread than the UI thread
Solved thanks to #Terence
Added
v.setAdjustViewBounds = true;
Try parent.addView(v, params);

Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview

I am trying to scroll item to the middle position of the screen after selecting it.I have used Horizontalscrollview and LinearLayout inside it to add item. I show in the XML
XML
<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"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updown"
android:layout_weight="0.8"
android:fadingEdgeLength="0dp" >
<LinearLayout
android:id="#+id/horizontalbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tlbackground"
android:baselineAligned="true"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Class
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout l1 = (LinearLayout)findViewById(R.id.horizontalbar);
for(int i = 0; i < 20; i++)
{
Button b = new Button(this);
b.setText("t"+i);
ll.addView(b);
}
}
}
I want something like in figure
before click
after click
How can I Scroll that selected item to the middle in HorizontalScrollview?
After long try , I have got it with this
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int scrollX = (b.getLeft() - (screenWidth / 2)) + (b.getWidth() / 2);
hsl.smoothScrollTo(scrollX, 0);
}
});
ll.addView(b);
}
This is how your onCreate should look like:
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.horizontalbar);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int center = (width - b.getWidth())/2;
hsl.scrollTo(b.getLeft() - center, b.getTop());
}
});
ll.addView(b);
}
The only thing i didn't discover is why i need to substract additional 200px (in my case). Probably some padding or something else. Instead of 200, you could discover how many dips is it and call function:
public static int convertDipToPixels(Context c, float dips) {
return (int) (dips * c.getResources().getDisplayMetrics().density + 0.5f);
}

when i was dragging down the image,the button is not working

In my xml on button i want to declare image,when i was dragging down the image the button clicking was not working,it was in background,it does come the forground.
How can it be possible to use button? I want to display toast meassage when i click on button.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
ImageView img = null;
AbsoluteLayout aLayout;
int status = 0;
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
boolean first = true;
Bitmap icon;
Button b;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
img = (ImageView) findViewById(R.id.imageView);
icon = BitmapFactory.decodeResource(getResources(), R.drawable.gg);
b = (Button) findViewById(R.id.btnFavorites);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "activity closed",
Toast.LENGTH_LONG).show();
}
});
aLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
// img.setBackgroundColor(Color.TRANSPARENT);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (first) {
x = event.getX();
first = false;
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//
}
if ((event.getX() < (Measuredwidth - icon.getWidth() / 2) && event
.getX() > (icon.getWidth() / 2))
&& (event.getY() < MeasuredHeight
- (icon.getHeight() + 60) && event.getY() > icon
.getHeight() / 2)) {
#SuppressWarnings("deprecation")
// (int) event.getX()-img.getWidth()/2
LayoutParams lp = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (int) 0, (int) event
.getY() - img.getHeight() / 2);
img.setLayoutParams(lp);
}
return true;
}
});
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/ic_launcher"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
<AbsoluteLayout
android:id="#+id/absLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dip" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/gg"
android:scaleType="matrix" >
</ImageView>
</AbsoluteLayout>
</FrameLayout>
if you place image on button , Button will lose control so , you can give drawable (image) to button using background or drawable left or right top bottom ...or else you can implement on click listener even on image view..
Update..
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/favorite_slide"
android:includeFontPadding="false"
android:text="#string/titleFavourites"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
in the code i have given drawableleft, same yo can give for right top left and even padding also possible , and apply your logic for touch

click event of button not getting raised

I am trying to make something like customised quick badge with some buttons on it. I have successfully desgined it but when i am clicking on those buttons it does nothing. Can someone tell me where am i going wrong.
demo_layout.xml
<?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" >
<Button
android:id="#+id/likemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<Button
android:id="#+id/likequickaction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
</LinearLayout>
popup_grid_layout.xml
<?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" >
<FrameLayout
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip"
android:background="#drawable/quickaction_top_frame" />
<ImageView
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/quickaction_arrow_up" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header2"
android:background="#drawable/quickaction_slider_background"
android:fadingEdgeLength="0.0dip"
android:paddingLeft="1.0dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_left" />
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/api_buttons" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_right" />
</LinearLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scroll"
android:background="#drawable/quickaction_bottom_frame" />
</RelativeLayout>
api_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/quickaction_slider_background" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/one"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="One" />
<Button
android:id="#+id/two"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Two" />
<Button
android:id="#+id/three"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Three" />
<Button
android:id="#+id/four"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Four" />
<Button
android:id="#+id/five"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Five" />
<Button
android:id="#+id/six"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Six" />
</TableRow>
</HorizontalScrollView>
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext()
.getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
protected void onCreate() {
}
protected void onShow() {
}
private void preShow() {
if (this.root == null) {
throw new IllegalStateException(
"setContentView was not called with a view to display.");
}
onShow();
if (this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);
this.window.setContentView(this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ this.anchor.getWidth(), location[1] + this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
// display on bottom
if (rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
}
LikeQuickActionDemo.java
public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.demo_layout);
this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
this.likemenuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
this.likequickactionButton = (Button) this
.findViewById(R.id.likequickaction);
this.likequickactionButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu(0, 200);
}
});
}
private static class DemoPopupWindow extends BetterPopupWindow implements
OnClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
protected void onCreate() {
// inflate layout
LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
// setup button events
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
View item = row.getChildAt(j);
if (item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}
// set the inflated view as what we want to display
this.setContentView(root);
}
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(),
Toast.LENGTH_SHORT).show();
this.dismiss();
}
}
}
The problem is in DemoPopupWindow.onCreate method:
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
root will be RelativeLayout defined in popup_grid_layout.xml. Later you iterate through all its children and set onclick listeners only if child view is TableRow, which is never the case. I guess what you want there is to find horizontalScrollView1 view and do the same with its children instead, something like:
View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
View v = horizontalScrollView1.getChildAt(i);
if (v instanceof TableRow) {
...

Categories

Resources