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
}
Related
I need to create this type of UI.
But it should work similar to Radio Button that is when one button got selected other two should be deselected.
There are various approaches like using three Images for each button and also images for pressed and unpressed state.
But how to achieve this using Button Tag.
Perhaps here may be useful to library work
https://github.com/pucamafra/android-segmentedtab
You can create 3 buttons and control other buttons with OnClickListener. For example:
layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
class:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(true);
btn2.setSelected(false);
btn3.setSelected(false);
//Do something
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(true);
btn3.setSelected(false);
//Do something
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(false);
btn3.setSelected(true);
//Do something
}
});
Sure, here .. keep it in selected state in OnClickListener when event x is fired. when event y is fired, keep button2 in selected state *& don't forget to remove button1 from selected state, Just try to improve this, it will work
I just made this , you are welcome to use & commit changes too!
I am new in android programming. I have made an app which is fill in the blanks type app. Until the answer-confirm button is clicked, the next and the previous Button should be disabled. If it is clicked and answer is checked, then next and previous Button to get enabled. please help!!!!!!!!
If you want to disable a button in xml use this code
<Button
android:text="Next"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
for enabling the button when we click on previous then in onClick function(previous) add this code
next.setVisibility(View.VISIBLE);
next is the button
something like that (maybe its not the best way, you can play with it and make it better)
protected void onCreate(Bundle savedInstanceState) {
...
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enableNextButton();
}
});
...
}
private void enableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_active);
nextButton.setClickable(true);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goNext();
}
});
}
private void disableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_inactive);
nextButton.setClickable(false);
}
and in your xml the buttons should be something like
<Button
android:id="#+id/first_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_active"
android:layout_margin="#dimen/generic_margin"
android:text="#string/first_button_text"
android:textColor="#color/white"/>
<Button
android:id="#+id/next_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_inactive"
android:clickable="false"
android:layout_margin="#dimen/generic_margin"
android:text="#string/next_button_text"
android:textColor="#color/white"/>
this way you start with an active button and an inactive button, when the first is pressed you can "activate" the next button
Please use punctuations and uppercases but anyway.
You can check here for more infos about buttons : http://developer.android.com/reference/android/widget/Button.html
Otherwise, the method setVisible() allow you to make visibile or not a button of your layout. Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
EDIT
Oh sorry ! So you can use setEnabled().
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
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?