Switching screens by dragging over the touch screen - android

I had tried a code for Switching Screen by dragging over the touch screen..but it gives an error...can anyone please check what mistake i have done in the code..and error arise at
vf.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
vf.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
code for .xml file
<ViewFlipper android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:id="#+id/tv_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Country" >
</TextView>
<Spinner android:text=""
android:id="#+id/spinner_country"
android:layout_width="200px"
android:layout_height="55px">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:id="#+id/tv_income"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Income" >
</TextView>
<EditText android:text=""
android:id="#+id/et_income"
android:layout_width="200px"
android:layout_height="55px">
</EditText>
</LinearLayout>
</ViewFlipper>
code for .java file
package com.examples.switchactivtybydragging;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.*;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.ViewFlipper;
public class SwitchActivityByDraggingActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
private float downXValue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
#SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
new String[] { "Canada", "USA" });
spinnerCountries.setAdapter(countryArrayAdapter);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
// Flip!
vf.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
// Flip!
vf.showNext();
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
}

Your problem is that you haven't created the animations. For your code to work, you require animations in res/anim/push_left_in.xml and res/anim/push_left_out.xml. It is the absence of these files that is causing your error.
I have written a blog series about Simple Animations which may help you to understand how to create these files, and what you need to put in them.

Related

onTouchEvent() in a Game

I am developing a game application using android.
The Object(a box) slides up and down. And it has to hit the
objects(orange and pink balls) coming towards it from the right end of
the screen that would increase his score.
There will be black balls as well(shot from the right end of the
screen) which he should avoid hitting.
I am having problem with
onTouchEvent(MotionEvent me)
function while implementing the code.
I am following this tutorial in this series of tutorials.
My Questions:
To use the onTouchEvent(MotionEvent me) function do I need to import any class?
The tutorial has declared theonTouchEvent(MotionEvent me) outside the onCreate method. Which is okay. But the program has not called it anywhere. How does it work then?
After writing the code as mentioned in the tutorial, the program is not working as intended. The box appears when the activity starts. However, it disappears as soon as I click on the screen. What could be the problem?
ActivityMain.XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/scoreLabel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text=" : 300"
android:paddingLeft="10dp"
android:gravity="center_vertical" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/startLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="130dp"/>
<ImageView
android:id="#+id/box"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/box"
android:layout_gravity="center_vertical" />
<ImageView
android:id="#+id/orange"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/orange" />
<ImageView
android:id="#+id/black"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/black" />
<ImageView
android:id="#+id/pink"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/pink" />
</FrameLayout>
</RelativeLayout>
MainActivity.java
package com.example.catcheggs1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView scoreLabel;
private TextView startLabel;
private ImageView box;
private ImageView orange;
private ImageView black;
private ImageView pink;
//Position
private int boxY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreLabel=(TextView)findViewById(R.id.scoreLabel);
startLabel=(TextView)findViewById(R.id.startLabel);
box=(ImageView)findViewById(R.id.box);
orange=(ImageView)findViewById(R.id.orange);
pink=(ImageView)findViewById(R.id.pink);
black=(ImageView)findViewById(R.id.black);
//Move To Out of Screen
orange.setX(-80);
orange.setY(-80);
pink.setX(-80);
pink.setY(-80);
black.setX(-80);
black.setY(-80);
//Temporary
startLabel.setVisibility(View.INVISIBLE);
boxY=500;
}
public boolean onTouchEvent(MotionEvent me)
{
if(me.getAction()==MotionEvent.ACTION_DOWN)
{
boxY -= 1 ;
}
box.setY(boxY);
return true;
}
}
1.) No, you do not need to import anything.
2.) For detailed information you can see the link.
It is an event method, it is automatically called, you do not need to call it.
https://developer.android.com/guide/topics/ui/ui-events#java
3.) you use boxY variable when touched and it is assigned an arbitrary number. Your problem is much likely to be caused by that. Rather than that, you should first get the current position than tweak it.
You can get current position with this method.
int[] location = new int[2];
imageView.getLocationOnScreen(location);
https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])
Bonus.) onTouchEvent is an activity method, so you should use view's own event listeners for specific tasks rather than onTouchEvent.
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

android gravity in textview is not working

hi programmers my big respect to all of you.
im new here in android programming.
im having a problem in working my radio button with a function of gravity in my textview by aligning them from center, left and right.
My API is 23
and im following this tutorial youtube tutorial but mine is not working
package com.example.testavd;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnCheckedChangeListener {
TextView txtV;
RadioGroup rg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtV = (TextView) findViewById(R.id.textOut);
rg = (RadioGroup) findViewById(R.id.rdG);
rg.setOnCheckedChangeListener(this);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtV.setText("boom");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbCenter:
txtV.setGravity(Gravity.CENTER);
break;
case R.id.rbLeft:
txtV.setGravity(Gravity.LEFT);
break;
case R.id.rbRight:
txtV.setGravity(Gravity.RIGHT);
break;
}
}
}
My 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: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="com.example.testavd.MainActivity" >
<RadioGroup
android:id="#+id/rdG"
android:layout_width="fill_parent"
android:layout_height="match_parent"
>
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rbLeft"
android:layout_below="#+id/rbLeft"
android:layout_marginTop="36dp"
android:text="Right" />
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/button1"
android:text="Left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/rbRight"
android:layout_marginBottom="16dp"
android:text="Center" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="32dp"
android:text="Output"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Use android:gravity="center_horizontal" on the RadioGroup to center the radiobuttons.Also gravity doesn't work in RelativeLayout You have to align according to parent or other widgets. If want to use gravity or layout_gravity use linearlayout.
Your TextView parent is a RelativeLayout, you cannot use gravity with RelativeLayout.
Try using in xml layout file
android:layout_centerHorizontal="true"
or in Activity
case R.id.rbCenter:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txtV.setLayoutParams(params);
break;
Solution 1 :
android:gravity sets the gravity of the content of the View its used on.
you have used gravity in TextView having wrap_content width, so it will not make any diferrence as view is having width as much as content.
try it with match_parent width and you will see difference.
android:layout_width="macth_parent"
Solution 2 :
use android:layout_gravity, it sets the gravity of the View or Layout in its parent.
no matter how much content your TextView is having, it will set whole TextView in center/right/left aligned.
To make it more affective make sure following things :
When you are using android:gravity use width of view as match_parent.
When you are using android:layout_gravity use width of view as wrap_content.
refer this to programmatically set layout_gravity : How to set layout_gravity PROGRAMATICALLY?

button hovering not working in android

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

How to create buttons that display images?

I created three buttons and each button shows text when it is clicked. Now I need to add to that so the buttons show text + an image. all the texts and images will only appear if the buttons are clicked. For example, when the user clicks on first button, text1 + image1 appears. When the user clicks on second button, text2 + image2 appears in the same places of text1 and image1. Any help please? here are my codes:
XML (I couldn't post the first code part here but below is the most important parts)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 1"
android:id="#+id/click_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/response"
android:layout_below="#+id/click_btn"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 2"
android:id="#+id/button15"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/response"
android:layout_toEndOf="#+id/response" />
</RelativeLayout>
Java:
package com.example.android.ch;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp; ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_recipes);
resp = (TextView)this.findViewById(id.response);
Button b = (Button)this.findViewById(id.click_btn);
Button c = (Button)this.findViewById(id.button15);
Button d = (Button)this.findViewById(id.button16);
ImageView imageView = (ImageView) findViewById(id.imageView);
b.setOnClickListener(this);
c.setOnClickListener(this);
d.setOnClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("2 medium sweet potatoes (about 1 pound total) \n1/2 teaspoon salt\n1/2 teaspoon ground cumin\n1/2 teaspoon chili powder\n1/2 teaspoon paprika\n1/4 teaspoon ground black pepper");
break;
case R.id.button15: /** on button15 button click */
resp.setText("1 large egg yolk, at room temperature\n1/2 cup extra-virgin olive oil; more for grilling\n2 teaspoons minced fresh flat-leaf parsley\n1 teaspoon minced fresh tarragon\n1 1/2 teaspoons fresh lemon juice; more to taste Kosher salt");
break;
case R.id.button16: /** on button16 button click */
resp.setText("1/2 cup butter, softened\n1 cup packed light brown sugar\n1 1/2 cups all-purpose flour\n3 cups rolled oats\n1 teaspoon ground cinnamon");
break;
}
}
}
yes you can add image or icon to a button with text.
use below code for this -
<Button
android:drawableRight="#drawable/ic_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
here i have added one more property android:drawableRight you can change it to android:drawableLeft, android:drawableTop, or android:drawableBottom and set image on button.
Use drawableLeft (or any direction) attribute to display image with text,
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
Above code will display image like
include the imageview and textview in a layout and keep it's visibility as invisible/gone.Then on clicking a button(for example button1) inside the onclick listener method you need to try like
imageview.setimage("your image");
textview.setText("your text");
change this code inside each buttonclick listener with various value.
I got right answer with many of you and thank you all but I meant to create three buttons and show text+image Not inside the icon. To do that I created three fragments xml files so when the user click on a button it will bring the fragment and place it.

Android: ImageView that extends onTouchEvent for Drag

I wrote a simple cards game where the user plays his card
doing TAP on one of the three imageviews (the cards)
I'd like to add the possibility of playing the card
by dragging the ImageView on the "table"
(the table is another layout or simply another part of the screen).
I tryed to use the techinc indicated at
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/
but as it uses AbsoluteLayout, it introduces a lot of limitations
on my current layout, more it requires adjustments depending
to the device screen resolution where the app runs.
I'd like to avoid this continue using
-if possibile- the RelativeLayout.
Maybe the starting point is extenting the ImageView
adding the onTouch support but i couldn't reproduce
the wished effect (drag)
Any idea or suggestion or sample code?
Thanks!
Just to give an idea, this is the draft of layout.
The 3 cards below should be moved via drag.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="#+id/player_cards_layout"
>
<ImageView android:id="#+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="#+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
<ImageView android:id="#+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_1"
/>
<ImageView android:id="#+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
here is nice example from link. i hope this will help you.
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:id="#+id/LinearLayout01">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/btn"
android:text="Drag Me"></Button>
</FrameLayout>
src/com/beanie/example/Home.java
package com.beanie.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
public class Home extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private FrameLayout layout;
private int status;
private LayoutParams params;
private ImageView image;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}

Categories

Resources