I have created a spinner in my app which i want to be invisible when someone press the sos button then it should be visible for the user to select one option in it how can i solve it?
I have created a spinner in my app which i want to be invisible when
someone press the sos button
You can set a listener on the button that will set the visibility of the spinner.
Ex.
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.GONE);
}
});
it should be visible for the user to select one option in it how can i
solve it?
I'm not sure what this means. I thought you wanted the spinner to be invisible?
You can use the below code to hide and show the Spinner
//hide
spinner.setVisibility(View.GONE);
//show
spinner.setVisibility(View.VISIBLE);
Also,you can use the below code snippet to get the item selected by user;
spinner.setOnItemSelectedListener(this);
...
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
//Hide
spinner.setVisibility(View.GONE);
//Show
spinner.setVisibility(View.VISIBLE);
Android: How to make a Spinner invisible and then visible again?
#HumanOidRoBo you can do it by this code..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Optional"
android:textSize="20sp" />
<Spinner
android:id="#+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginLeft="5dp">
</Spinner>
</LinearLayout>
and in class add this on click event of SOS
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.VISIBLE); // for Show
// or
mySpinner.setVisibility(View.GONE); // for Hide
}
});
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 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 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().
I need to use spinner as a menu. But problem is that when I click on an item it gets selected and shown at that place a behavior which I want to avoid. Secondly I need that the very first item should always be a heading no matter which of the items has been selected as in the following images:
spinner in normal condition
when user taps the spinner
Now if user taps any of the items heading should not be changed but item should be selected. I have achieved it using ListView but I think I must use proper Android components (if it is really possible).
Thanks in advance.
I have solved the above issue using the following code, but need to use spinner.
layout file
<RelativeLayout
android:id="#+id/header_main"
style="#style/layout_f_w"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/heading_color" >
<TextView
android:id="#+id/headingText"
style="#style/layout_wrap"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="MATCH CENTER"
android:textColor="#color/text_color_white"
android:textSize="15sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/matchcenter_menu"
style="#style/layout_wrap"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/headingText"
android:background="#android:color/transparent"
android:paddingTop="20dp"
android:src="#drawable/drp_down_menu" />
</RelativeLayout>
....
<ListView
android:id="#+id/mainscreen_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:color/black"
android:cacheColorHint="#00000000"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/transparent" />
Setting the views.
TextView headingText = (TextView)findViewById(R.id.headingText);
headingText.setTypeface(Utils.getCustomFont(LiveScoreCrowdScreen.this));
headingText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
matcheCenterMenu = (ImageButton)findViewById(R.id.matchcenter_menu);
matcheCenterMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
....
public void showMatchCenterMenu(View btn) {
ScreenMenuItemsAdapter adapter = null;
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
adapter = new ScreenMenuItemsAdapter(LiveScoreCrowdScreen.this, getResources().getStringArray(R.array.livescorecard_menuitems));
mainscreenMenu.setAdapter(adapter);
mainscreenMenu.setVisibility(View.VISIBLE);
mainscreenMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View row, int position, long id) {
CCApplication.isMenuOpened = false;
switch (position) {
case 0:// Refresh
//refresh screen
break;
case 1:// Highlights
//get highlights
break;
case 2:// Preferences
//get preferences
break;
case 3:// current time
// get current time
break;
}
mainscreenMenu.setVisibility(View.GONE);
}
});
}
just a hacking solution not proper one:
Again set the adapter with the spinner when you done all works in side onItemSelectedListener().
i.e
spinner.setAdapter(your_adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
//Do works here
//atlast again load the same adapter
spinner.setAdapter(your_adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
It will load the same adapter after selecting an item and show you like before.
I'm using this CoverFlow :
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
I want to be able to change View when I click on a button, the button being a back/forward icon which will take you to the previous/next item in the coverflow.
I have modded the coverflow slightly so that I use an XML layout instead.
Here is my onCreate() method:
setContentView(R.layout.main);
CoverFlow coverFlow = (CoverFlow)findViewById(R.id.coverflow);
coverFlow.setAdapter(new ImageAdapter(this));
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setSpacing(-20);
coverFlow.setSelection(0, true);
coverFlow.setAnimationDuration(1000);
tv = (TextView)findViewById(R.id.textView1);
coverFlow.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getBaseContext(), String.valueOf(arg2), Toast.LENGTH_SHORT).show();
}
});
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv.setText(String.valueOf(arg2));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do something
}
});
My XML:
<?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"
android:background="#drawable/home_background"
android:orientation="vertical" >
<com.demo.app.coverflow.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="39dp"
android:layout_marginLeft="35dp"
android:background="#drawable/button_left" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="39dp"
android:layout_marginRight="35dp"
android:background="#drawable/button_right" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="55dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="hello"
android:textColor="#color/White"
android:textSize="16dp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="170dp"
android:layout_marginTop="15dp"
android:src="#drawable/unsa_logo" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:layout_marginTop="23dp"
android:src="#drawable/pnc_logo" />
</RelativeLayout>
Thanks David, I'm one of the Mike colleagues but I've never written a line of Java.
I work in the iOS department of our company.
I understood what you were talking about. Your answer was helpful and well written!
I just had to add :
if ( currentimageposition < coverFlow.getcount()-1) {
coverFlow.setSelection(currentImagePosition +1, true);
currentImagePosition = currentImagePosition +1;
}
Because your onItemSelected was not called (update the current marker), I don't know why.
However, coverFlow.selection( int , bool) is not animated. It is just a set method when the view is loaded. For example, coverFlow.selection(3,true) will set the fourth image on the center of the coverflow.
While I was trying to find out how to add the animation I came across this which does everything in just one line :
go left
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));
go right
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
(just add each line to their respective button onclick methods
Hope this can help anyone else who finds them self in the same situation as I was :)
I hope i understand your question. if so, here's an idea:
you need to store a private instance variable that tracks the current position of the image that is being displayed from the coverflow. then, you update it from the setOnItemSelectedListener() method, which is inherited from Gallery. On the "back" or "forward" button, you simply set your current selection +/- 1 depending on the button pressed.
so in your Activity, add an instance variable int that will keep track of position like...
public class MyActivity {
private int currentImagePosition = -1;
//add the rest of your onCreate code here...
}
Next, you need to be able to update the current position of the image that is currently being displayed in the coverflow. you can do so by overriding setOnItemSelectedListener like so (inside your onCreate method):
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
currentImagePosition = position; //this will update your current marker
}
};
Lastly, all you have to do is set each button's onclick listeners to change to the previous or next image by using setSelection( currentImagePosition+1 ) or setSelection( currentImagePosition-1 ). By the way, what's the true or false parameter of setSelection() do? i'm not sure what it does so i'm just going to use true like you did initially. your onCreate() method will now look something like:
#Override
public void onCreate(Bundle savedInstanceState){
//insert your other code in onCreate here in this spot....
//add the following lines to your code:
Button goLeft = (Button) findViewById(R.id.button1);
goLeft.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition > 0 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition - 1, true);
}
}
} ) ;
Button goRight = (Button) findViewById(R.id.button2);
goRight.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition < coverFlow.getCount()-1 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition + 1, true);
}
}
} ) ;
}
NOTE: the part about updating position is assumed to work from reading Get the position of the current image displayed in Gallery so i hope this works. if not, at least i tried :D
Good luck!!!