How to change the position of RadioGroup - android

I am making some exercise to get used to Android dev.
So I'm trying to change the position of a Radiogroup in the layout by having the user press buttons
here is the code I'm using, it actually changes the position, but not by much, it barely moves form the original position when I hit center or right.
else if(group==gravedad)
{
if(checkedId==R.id.izq)
{
gravedad.setGravity(Gravity.LEFT);
}else if(checkedId==R.id.ctr)
{
gravedad.setGravity(Gravity.CENTER);
}else if(checkedId==R.id.der)
{
gravedad.setGravity(Gravity.RIGHT);
}
}
Maybe i messed up the xml...here is how I have set it up
<RadioGroup
android:id="#+id/gravedad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
<RadioButton
android:id="#+id/izq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Izquierda" />
<RadioButton
android:id="#+id/ctr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centro" />
<RadioButton
android:id="#+id/der"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Derecha" />
</RadioGroup>

if your api is >11 then you can do like this to change the position of radio group..
RadioGroup r=(RadioGroup)findViewById(R.id.radioGroup1);
r.setX(30);
r.setY(30);

Related

Disable Click on RadioGroup inside layout

I want to disable the clicks on the RadioGroup placed inside the Relative Layout! In a way that my only Relative Layout should be clickable. I want this on some certain condition my XML is as follow:
<RelativeLayout
android:id="#+id/shouldNotPlayLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="44dp"
android:layout_marginRight="44dp"
android:layout_marginTop="40dp">
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:saveEnabled="false">
<RadioButton
android:id="#+id/radio_option_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
<RadioButton
android:id="#+id/radio_option_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
<RadioButton
android:id="#+id/radio_option_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
</RadioGroup>
</RelativeLayout>
I am stopping the clicks on the radio group as:
binding.questionLayout.radioOptionA.setEnabled(false);
binding.questionLayout.radioOptionB.setEnabled(false);
binding.questionLayout.radioOptionC.setEnabled(false);
binding.questionLayout.radioOptionA.setFocusable(false);
binding.questionLayout.radioOptionB.setFocusable(false);
binding.questionLayout.radioOptionC.setFocusable(false);
binding.questionLayout.shouldNotPlayLayout.requestFocus();
binding.questionLayout.shouldNotPlayLayout.setClickable(true);
binding.questionLayout.shouldNotPlayLayout.setFocusable(true);
The Problem I am facing is that sometimes any of these three radio button can still be clickable and the user can click any radio button. But I want to disable the click on the radio group/button and get the click done on the relative layout!
Can somebody please figure any blunders done by me?
Add binding.questionLayout.radio_group.setEnabled(false)
I have made a demo with your requirement & here's what you need,
private void manageEnableState(boolean isEnable) {
radioOptionA.setEnabled(isEnable);
radioOptionB.setEnabled(isEnable);
radioOptionC.setEnabled(isEnable);
radioOptionA.setFocusable(isEnable);
radioOptionB.setFocusable(isEnable);
radioOptionC.setFocusable(isEnable);
shouldNotPlayLayout.setClickable(!isEnable);
shouldNotPlayLayout.setFocusable(!isEnable);
}
Where isEnable is used to set your RadioButton enable state..
Please I giving what you have done but then also please try following the way it will help,
private void enableDisbleRadioGroup(RadioGroup groupId, boolean isEnable) {
for (int i = 0; i < groupId.getChildCount(); i++) {
groupId.getChildAt(i).setEnabled(isEnable);
}
}
In above method pass your radio group id and enable disable mode what you want,

Android design game card UI

I need help with the design of my card game.
I having a Vector of cards (player cards), each item appers in ImageView (Already done and working).
private Vector<Card> myCards;
I having a LinearLayout that holding 5 imageView's.
<LinearLayout
android:id="#+id/myCardsLayout"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:paddingBottom="10dip">
<ImageView
android:id="#+id/my_card_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="goToMainMenu"
/>
<ImageView
android:id="#+id/my_card_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="goToMainMenu"
/>
<ImageView
android:id="#+id/my_card_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="goToMainMenu"
/>
<ImageView
android:id="#+id/my_card_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="goToMainMenu"
/>
<ImageView
android:id="#+id/my_card_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="goToMainMenu"
/>
</LinearLayout>
I want to choose the cards that i want to drop by click on the card, And that the player will see this by jumping the card slightly up (or another recommended way).
I will implement that on android:onClick function (goToMainMenu is a temporary statement).
Any idea ? Thanks!
UPDATE1
public void myCard1OnClick(View view) {
view.setSelected(true);
if(view.isSelected()) {
int y = view.getTop();
view.setTop(y - 30);
}
else{
int y = view.getTop();
view.setTop(y + 30);
}
}
You can make an onclick function on cards that moves and give them a function that takes their x and y cordinates and then you just inflate y to move up.
try to use setX() and setY() view properties to move across a space.
And about the issue of moving the card and after that it get back to its original position, maybe its because the set selected its not working, and then enters to the else statement.

Scrollview with many buttons over an image

I want to do something similar to a Map behavior where I have a large Image (bigger than the phone screen) and a group of buttons I want to put over special XY locations over the image, and when the user scrolls the image (horizontally and vertically), the buttons keep the same position over the image (like markers do). Then the user can click over a button and open a new activity.
I want to do this in xml. Any suggestion?
I cant figure out how to attach the buttons with the image XY positions:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/map_view"
android:background="#drawable/myImage"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 1"
android:id="#+id/radioButton" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 2"
android:id="#+id/radioButton2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 3"
android:id="#+id/radioButton3" />`
/>
</RelativeLayout>
</ScrollView>
Instead of scrollview you should change imageview position on touch. If user touches top-down , imageview position should goes to negative direction same as top-down pixels. Therefore your imageview can move to right-left as well.
Get the touch position inside the imageview in android
Android move view on touch event
Here, make sure the the ImageView should always be larger than the screen as you say.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/map_view"
android:layout_width="1800px"
android:layout_height="1800px"
android:background="#drawable/the_large_image"/>
<RadioButton
android:id="#+id/radioButton"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 1"/>
<RadioButton
android:id="#+id/radioButton2"
android:layout_marginTop="200dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 2"/>
<RadioButton
android:id="#+id/radioButton3"
android:layout_marginTop="80dp"
android:layout_marginLeft="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 3"/>
</RelativeLayout>
</ScrollView>
Thanks to #Sheychan that gave me some clue for getting this as I wanted.
First of all: the xml. Use 2 scroll views (one main that is vertical, and another horizontal), so that we can get horizontal and vertical scrolling. Then a RelativeLayout so that we can put the radiobuttons over the image in desired positions. Is important the "src" and "scaleType" in image so that we can get correct image dimensions. Here is the code for xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/svVertical">
<HorizontalScrollView android:id="#+id/hsvHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isScrollContainer="true">
<ImageView
android:id="#+id/map_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagen"
android:scaleType="centerCrop"/>
<RadioButton
android:id="#+id/radioButton"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 1"/>
<RadioButton
android:id="#+id/radioButton2"
android:layout_marginTop="200dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 2"/>
<RadioButton
android:id="#+id/radioButton3"
android:layout_marginTop="80dp"
android:layout_marginLeft="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Position 3"/>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
But with this solution we got odd scrolling (only vertical or horizontal, not both at the same time: call it "diagonal" scrolling). To resolve this: a simple override on Activity holding this layout:
ScrollView scrollY;
HorizontalScrollView scrollYChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollY = (ScrollView)findViewById(R.id.svVertical);
scrollYChild = (HorizontalScrollView)findViewById(R.id.hsvHorizontal);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
scrollYChild.dispatchTouchEvent(event);
scrollY.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
This start working better, but still in an odd way. Is like the move "jumps" while scrolling.
The solution: take away the action bar:
On the Manifest, add the "NoActionBar" theme in the application tag (or the activity tag)
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
But, be careful, cause the activity must not be an "ActionBarActivity", you must change it to "Activity".
And, just in case, by Android recommendation, add a compatible old SDK versions with a "hide the status bar" on the OnCreate of the Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
scrollY = (ScrollView)findViewById(R.id.svVertical);
scrollYChild = (HorizontalScrollView)findViewById(R.id.hsvHorizontal);
}
And that's all folks :) Hope it helps you as it helped me.

Only one radio button selected at a time

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?
public void onRadioButtonClick(View v)
{
RadioButton button = (RadioButton) v;
boolean checkBox1Selected;
boolean checkBox2Selected = false;
Toast.makeText(MainActivity.this,
button.getId()+ " was chosen."+R.id.wificheckBox,
Toast.LENGTH_SHORT).show();
if ( button.getId() ==R.id.wificheckBox) {
// Toggle status of checkbox selection
checkBox1Selected = radiowifiButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
radiomobileButton.setChecked(false);
checkBox2Selected = false;
}
else if (button.getId() ==R.id.wifimobilecheckBox) {
// Toggle status of checkbox selection
checkBox2Selected = radiomobileButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
radiowifiButton.setChecked(false);
checkBox1Selected = false;
}
}
}
main xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp">
<LinearLayout
android:id="#+id/ll_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wifimobilecheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:checked="true"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi or mobile network"
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_2"
android:paddingTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wificheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi "
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
</RadioGroup>
Drawabe- button_radio
<item android:state_checked="true"android:state_pressed="false" android:drawable="#drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="#drawable/checkbox"/>
If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="#drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="#+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="#+id/rb_female" worked for me in RadioGroup.
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.
With my understanding you have two doubts.
How to customize RadioButtons with custom drawables??
Checkout the answer here.
How to deselect the previously selected radiobutton??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
Hope this will help you.
To check/uncheck radio buttons not part of a RadioGroup,
add a method in the android GUI designer under the
RadioButton Properties 'onClick' (In this example radio1_onClick).
In your activity.xml under the xml for the RadioButton
RadioButton
[blah blah blah]
android:id="#+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}

Radio buttons on click listenener

I have radio buttons in an application I am creating. When the user clicks on one option, I want the other radio buttons to be unclickable.
I'm not sure how to do this, the radio buttons are set in an onclick listener and the logic is set within on click.
What about calling setEnabled(false) on the rest of the buttons? If there are too many buttons you can consider putting in an array:
RadioButton[] buttons; //you put here all buttons..
// then, when you catch the click, call a method like this
private void disableOtherButtons(RadioButton buttonClicked)
for(RadioButton button : buttons){
if( button != buttonClicked ){
button.setEnabled(false);
}
}
}
If your using XML you can group them using RadioGroup, in the following example only one of the radio buttons is selectable at a time.
<RadioGroup
android:id="#+id/rg_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="#+id/rb_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sales"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regional"
android:layout_above="#+id/rb_configup1"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Global"
android:layout_above="#+id/rb_configup2"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad-hoc"
android:layout_above="#+id/rb_configup3"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
</RadioGroup>

Categories

Resources