bee to android programming and learning things.
i have the following code with me and what i need is to move only two selected images when button click happen
first user will select two images
then on button click they will move to left direction
here is my code
<ImageView
android:id="#+id/imLady"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<ImageView
android:id="#+id/imLady2"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<ImageView
android:id="#+id/imLady3"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Move" />
looking forward for suggestions,solutions from u guys:) regards
you need to setAnimations to your ImageViews in onClick() function of Button
ImageView v1,v2;
Button b;
TranslateAnimation ta1,ta2;
...
public void onCreate(Bundle savedInstances)
{
super.onCreate(savedInstances);
setContentView(R.layout.layoutname);
...
initiate and refer button and imageviews
...
ta1=new TranslateAnimation(fromx,tox,fromy,toy);
ta1.setDuration(1000); //1 second
ta2=new TranslateAnimation(fromx,tox,fromy,toy);
ta2.setDuration(1000); //1 second
b.setOnClickListener(new View.onClickListener(){
#override
public void onClick(View arg0)
{
v1.startAnimation(ta1);
v2.startAnimation(ta2);
}
});
}
refer to TranslateAnimation Documentation here
Related
I have a set of image view that I want to change when it is clicked. The first view is set by default, and I want the second one to appear when the first one is clicked. I also want the reverse to occur when the second one is visible. How can I do this? (Example: Default is Img 1 default, img 2 hidden. On click, img 2 shows, img 1 is hidden. On click again, reverse occurs).
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_off_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp" />
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_on_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_marginTop="134dp" />
Use the Following Code:
ImageView img1=(ImageView)findViewById(R.id.img1);
ImageView img2=(ImageView)findViewById(R.id.img2);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img1.setVisibility(View.GONE);
img2.setVisibility(View.VISIBLE);
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img2.setVisibility(View.GONE);
img1.setVisibility(View.VISIBLE);
}
});
in both of button onclick you must check to see if other button is not visible then set it visible and if it is visible then do not do any thing. you can check visibility of buttons with this code
if(mybutton.getVisibility()==View.VISIBLE)
{
//set visibiliy of mybutton
}
I am making an android app in which I have a screen whose xml looks like:
<LinearLayout
android:id="#+id/btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal" >
<Button
android:id="#+id/viewCalendar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_name_2"
android:gravity="center"
android:layout_margin="0.5dp"
android:text="View Calendar"
android:textColor="#android:color/primary_text_dark" />
<Button
android:id="#+id/createNewMeeting"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_name_2"
android:gravity="center"
android:layout_margin="0.5dp"
android:text="Add New Meeting"
android:textColor="#android:color/primary_text_dark" />
</LinearLayout>
Now you can see I have two Buttons View Calendar and Add New Meeting. The problem is View Calendar button responses on single click while Add New Meeting button responses on double click (Note that this happens only when I come into this screen first time after app starts and works fine until the app closed). When app closed and start again then first time again on this screen this button firing after two taps and so on...
Here is the Java code nothing new just setting onClickListener on Button
addNew = (Button) findViewById(R.id.createNewMeeting);
addNew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code for add new meeting
Intent intent = new Intent(getActivity(),
ActivityReminderNew.class);
startActivity(intent);
}
});
viewCalendar = (Button) findViewById(R.id.viewCalendar);
viewCalendar.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code for calendar view
}
});
R&D: I have been searching for this problem but mostly I get the answers like to set android:focusableInTouchMode="true" or android:focusable="true" in xml but its not solving my problem.
Hope I explained my problem in detail and you guys could understand my issue. Any type of help would be appreciated.
I allow the App's User to change the ImageButton's pictures from the drawable folders?
It's possible?
I want associate this action after onLongClickListener,
I put in the Drawable folder about 3 or 4 pictures(png) and the User can choose one for its ImageButton.
Yes, you can. On onLongClickListener click, you can pop up the option and then put a switch statement and put the following for each of the cases:
aButton.setImageResource(R.drawable.image2);
Here is the more detailed answer:
Put the following at the bottom of the layout (just before the last closing layout tag)
<FrameLayout
android:id="#+id/imagebuttonselectorlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imgButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1" />
<ImageButton
android:id="#+id/imgButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image2" />
</LinearLayout>
</FrameLayout>
Then, in the java class file, add the following lines:
FrameLayout mFrameLayout;
ImageButton mImageButton1;
ImageButton mImageButton2;
mFrameLayout = (FrameLayout)findViewById(R.id.imagebuttonselectorlayout);
mImageButton1 = (ImageButton)findViewById(R.id.imgButton1);
mImageButton2 = (ImageButton)findViewById(R.id.imgButton2);
For the onLongClick of the main image button
mImageButton1.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mFrameLayout.setVisibility(View.VISIBLE);
return true;
}
});
Add the following lines in the same file to complete the functionality:
mImageButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image1);
mFrameLayout.setVisibility(View.GONE);
}
});
mImageButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image2);
mFrameLayout.setVisibility(View.GONE);
}
});
I am trying to change text size when button is clicked.
xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"
android:textSize="30sp"
android:layout_margin=""/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="72dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/textView1"
android:text="Button2" />
This is my code :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
//txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
txtmain.setTextAppearance(getApplicationContext(), 12);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
txtmain.setBackgroundColor(Color.YELLOW);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,80);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
When I click button 1, it gives proper output but when I click button 2 after clicking button1 output changes.
Here is my output :
This would appear to be quite similar to known a known issue on ICS, see https://code.google.com/p/android/issues/detail?id=22493 and https://code.google.com/p/android/issues/detail?id=17343. The second of these suggests that a workaround is to add a "\n" to the text in the text view. Reading up those pages and those they link to may help resolve the issue for you.
The problem is
txtmain.setHeight(41);
in first button click, So it will change the height of the textview from WRAP CONTENT to a fixed size. Just remove it..
Why are you setting the text box height wen you click the button1?
Hi am new to android and have spent over 10 hours looking for this answer but i cant seem to find and understand it. I am at the main xml page. I am trying to create a button that goes to another page. I need the easiest and simplest way to do this. Can anyody please help me? Heres my code for my main 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
Go to your activity Initialize your button
Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your action
Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
startActivity(newActivityIntent);
}
});
You can also do it different...
You can instantiate a Button in your Activity:
private Button button;
Inside the onCreate(Bundle bundle) method you find your button, as defined at this Activity XML, which is the one you used setContentView(R.layout.yourxml);:
button = (Button) findViewById(R.id.button);
Then you use an OnClickListener:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Inside the onClick method, you instantiate an Intent
Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);
CurrentActivity = the one you are, and ActivityToGo the one you want to load.
startActivity();
Read the basics on Android here.
public class MyActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
// Use a switch on v.getId() if you have multiple clickable views.
// Since there's just one button, ...
Intent intent = new Intent(this, TargetActivity.class;
startActivity(intent);
}
}
You have to learn How to switch between Activities/Screens in Android, you can find here a well explained tutorial for beginners
This process is documented on the website concerning buttons. Google search Android Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button"
android:clickable="true"
android:onClick"insertMethodNameHere"
/>
This will call the method you define in the onClick tag, then start a new activity or update the view. Whatever you need to do