Handling Radio Group and Button in Fragment - java

first take a look on a part of my fragment xml file `
<LinearLayout
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rbclick"
android:text="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="4" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="5" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="rbclick"
android:text="6" />
</RadioGroup>
</LinearLayout>
now take look on my fragment java class
public class MealEntry extends Fragment {
private MealEntryViewModel mViewModel;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "MealEntry";
public RadioGroup rg;
RadioButton rb;
public static MealEntry newInstance() {
return new MealEntry();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
return view;
}
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(radiobtnid);
}
previously by this procedure i mean the (rbclick function ) can handle the radio group and radio button in Activity(i repeat Activity). but in Fragment the onClick method is not working. how can i resolve this? I've tried this way for fragment.
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)getActivity().findViewById(radiobtnid);
}
I think this handler is not working. cause it's got crash when i click the radio button.
here's is Log cat.
12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.closeup, PID: 3720
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9

Here is solution to handle radio group in fragment.
1.Remove all line android:onClick="rbclick" from each RadioButton in meal_entry_fragment.xml file, and create an id for each RadioButton.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="4" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="5" />
<RadioButton
android:id="#+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="6" />
</RadioGroup>
</LinearLayout>
2.Create an instance of OnCheckedChangeListener in MealFragment class.
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
// Write your code here
break;
case R.id.radio1:
// Write your code here
break;
case R.id.radio2:
// Write your code here
break;
case R.id.radio3:
// Write your code here
break;
case R.id.radio4:
// Write your code here
break;
case R.id.radio5:
// Write your code here
break;
case R.id.radio6:
// Write your code here
break;
default:
break;
}
}
};
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
return view;
}

Argument View v in public void rbclick(View v) is the RadioButton you are looking for

Try using this code below:
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)v.findViewById(radiobtnid);
}

Related

How to open a fragment from a button click which is included in another layout

Hello i want to open a fragment with button click but the button is in the layout snipet_profile.xml and i have included snipet_profile.xml in fragment_profile now when i click the button it dosent open the fragment that i wanted to be open ,Fragment which i want to open on button click is EditProfile
ok if you think it is liitle bit confusing see my code
i have tried a method where first you have to find the layout which have the button in my case it is RelativeLayout so i implemented this way
ProfleFragment.java
RelativeLayout relativeLayout = view.findViewById(R.id.snipet_profile); // R.id.snipet_profile is the layout that i have included in Profile Fragment
and now the button
Button editProfileButton;
editProfileButton = relativeLayout.findViewById(R.id.edit_profile_button); t// i have included relativelayout.findViewById so it can navigate or in simple this method i found
Error // after implementing the answer
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myappnotfinal, PID: 12566
android.view.InflateException: Binary XML file line #29 in com.example.myappnotfinal:layout/fragment_edit_profile: Binary XML file line #29 in com.example.myappnotfinal:layout/fragment_edit_profile: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #29 in com.example.myappnotfinal:layout/fragment_edit_profile: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:858)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at com.example.myappnotfinal.Fragments.Profile.Edit_Profile.onCreateView(Edit_Profile.java:23)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.IllegalArgumentException: adjustViewBounds not supported.
at de.hdodenhof.circleimageview.CircleImageView.setAdjustViewBounds(CircleImageView.java:141)
at android.widget.ImageView.<init>(ImageView.java:215)
at android.widget.ImageView.<init>(ImageView.java:188)
at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:98)
at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:94)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
at android.view.LayoutInflater.createView(LayoutInflater.java:858) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:686) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:538) 
at com.example.myappnotfinal.Fragments.Profile.Edit_Profile.onCreateView(Edit_Profile.java:23) 
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) 
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) 
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100) 
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:224) 
at android.app.ActivityThread.main(ActivityThread.java:7562) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
Now the full code
Profile_Fragment.java
Button editProfileButton;
#SuppressLint("SourceLockedOrientationActivity")
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
RelativeLayout relativeLayout = view.findViewById(R.id.snipet_profile);
editProfileButton = relativeLayout.findViewById(R.id.edit_profile_button);
editProfileButton.setOnClickListener(this::onClick);// added this line of code according to the answer
return view;
}
#Override
public void onClick(View view) {
Fragment fragment;
if (view.getId() == R.id.edit_profile_button) {
fragment = new Edit_Profile(); // Edit_Profile fragment
replaceFragment(fragment);
}
}
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(String.valueOf(new Profile_Fragment()));
transaction.commit();
}
fragment_profile.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:overScrollMode="never"
android:paddingTop="20dp">
<include
android:id="#+id/snipet_profile"
layout="#layout/snipet_profile" />
</LinearLayout>
snipet_profile.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="#+id/edit_profile_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Followers"
android:layout_centerHorizontal="true"
android:layout_marginStart="5dp"
android:layout_marginTop="20sp"
android:backgroundTint="#color/dark_red"
android:text="#string/edit_profile"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="normal" />
</Button>
Update 1 // added EditProfile java and xml files
Edit_Profile.java // these is the fragment im trying to open on button click
public class Edit_Profile extends Fragment {
private CircleImageView profilePhoto;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_profile, container, false);
profilePhoto = view.findViewById(R.id.circleImageView);
setProfileImage();
initImageLoader();
return view;
}
private void initImageLoader() {
UniversalImageLoader universalImageLoader = new UniversalImageLoader(getContext());
ImageLoader.getInstance().init(universalImageLoader.getConfig());
}
private void setProfileImage() {
String imgUrl = "https://64.media.tumblr.com/1276b4edef49034af70bda14325385e3/d8872c747cafa206-96/s500x750/aa915fc49a84b5295f0cd44145d655b66eb906a6.jpg";
UniversalImageLoader.setImage(imgUrl, profilePhoto, null, "");
}
}
fragment_edit_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<TextView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="10dp"
android:text="#string/done"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="bold|normal" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circleImageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="#+id/done"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/change_profilePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/circleImageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/change_photo"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/change_profilePhoto"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:text="#string/user_name"
android:textColor="#color/white"
android:textSize="16sp" />
<EditText
android:id="#+id/userNameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:autofillHints="User Name"
android:background="#null"
android:hint="#string/add"
android:inputType="textNoSuggestions"
android:textColor="#color/white"
android:textColorHint="#color/lite_grey"
android:textSize="16sp" />
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/userNameEditText"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/name"
android:textColor="#color/white"
android:textSize="16sp" />
<EditText
android:id="#+id/editTextTextFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Name"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:autofillHints="ADD"
android:background="#null"
android:hint="#string/add"
android:inputType="textNoSuggestions"
android:textColor="#color/white"
android:textColorHint="#color/lite_grey"
android:textSize="16sp" />
</RelativeLayout>
you have to set OnClickListener for this Button... use editProfileButton.setOnClickListener(this); - this is used as your Fragment implements View.OnClickListener
edit: well, your stacktrace is saying everything...
IllegalArgumentException: adjustViewBounds not supported.
at de.hdodenhof.circleimageview.CircleImageView.setAdjustViewBounds(CircleImageView.java:141)
just remove android:adjustViewBounds="true" line from XML-declared CircleImageView. and also remove android:scaleType="centerCrop". read THIS article, in which you can find:
The ScaleType of CircleImageView is always CENTER_CROP and if we try to change it we get an exception. So it is perfect for the profile pictures.
Enabling adjustViewBounds is not supported as this requires an unsupported ScaleType

How to fix ViewPager to prevent app to getting error and crash?

i'm working on login page, based on Androidhive tutorial and problem is Viewpager work fine with 2 layout but for 3 layouts app gonna crash.
In androidhive tutorial used 2 layout but i want to use 3 because i'm getting sms then otp then register, so problem is with 3rd layout which it won't appear and crash.
Androidhive Tutorial Link:
Part 1 Part 2
Here are the codes :
XML
<LinearLayout
android:id="#+id/layout_phone_registration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="25dp"
android:layout_marginTop="100dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:inputType="textCapWords"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_enter_mobile"
android:textColor="#android:color/white"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/inputMobile"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_mobile"
android:inputType="phone"
android:maxLength="12"
android:padding="5dp"
android:textColor="#color/colorPrimary"
android:textCursorDrawable="#null"
android:textSize="18dp" />
<Button
android:id="#+id/btn_request_sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:text="#string/lbl_next"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_otp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="25dp"
android:layout_marginTop="100dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_sit_back"
android:textColor="#android:color/white"
android:textSize="16dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/msg_manual_otp"
android:textColor="#android:color/white"
android:textSize="12dp" />
<EditText
android:id="#+id/inputOtp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:hint="#string/lbl_enter_otp"
android:inputType="number"
android:maxLength="6"
android:padding="10dp"
android:textCursorDrawable="#null"
android:textSize="18dp" />
<Button
android:id="#+id/btn_verify_otp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="#string/lbl_submit"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_complete_registration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:id="#+id/inputName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_name"
android:padding="5dp"
android:singleLine="true"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<EditText
android:id="#+id/inputAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#android:color/white"
android:fontFamily="sans-serif-light"
android:hint="#string/lbl_email"
android:padding="5dp"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<Button
android:id="#+id/btn_register_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:text="#string/lbl_next"
android:textColor="#android:color/white"
android:textSize="14dp" />
</LinearLayout>
</ir.atlaspio.atlasdrinkingservice.AdvancedUI.MyViewPager>>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="60dp"
android:indeterminateTint="#color/colorAccent"
android:indeterminateTintMode="src_atop"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layout_edit_mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_edit_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="16dp" />
<ImageButton
android:id="#+id/btn_edit_mobile"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#null"
android:src="#drawable/ic_exit_to_app_black_24dp" />
</LinearLayout>
Java
private ViewPager viewPager;
private ViewPagerAdapter adapter;
OnCreate =>
viewPager.setCurrentItem(0);
adapter = new ViewPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Public Class Activity =>
class ViewPagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
public Object instantiateItem(View collection, int position) {
int resId = 0;
switch (position) {
case 0:
resId = R.id.layout_phone_registration;
break;
case 1:
resId = R.id.layout_otp;
break;
case 2:
resId = R.id.layout_complete_registration;
break;
}
return findViewById(resId);
}
}
Logcat
08-29 10:48:12.426 4499-4499/ir.atlaspio.atlasdrinkingservice
E/InputEventSender: Exception dispatching finished signal.
08-29 10:48:12.426 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: Exception in MessageQueue callback:
handleReceiveCallback
08-29 10:48:12.436 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: java.lang.UnsupportedOperationException: Required
method destroyItem was not overridden
at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:201)
at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:128)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1172)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)
at android.support.v4.view.ViewPager.pageRight(ViewPager.java:2888)
at android.support.v4.view.ViewPager.arrowScroll(ViewPager.java:2844)
at android.support.v4.view.ViewPager.executeKeyEvent(ViewPager.java:2764)
at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2738)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3876)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3850)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3549)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3606)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3742)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.
As per your error
java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
You need to add this method in your pager adapter
#Override
void destroyItem (ViewGroup container, int position, Object object){
((ViewPager) container).removeView((View) object);
// more code if needed
}
Override destroyItem method in your ViewPagerAdapter class and remove obj as container.removeView(obj as LinearLayout) where LinearLayout is your root layout
#Override
void destroyItem(ViewGroup container, int position, Object obj) {
container.removeView(obj as LinearLayout)
}

ViewGroup is not resolved in Android Studio

I'm creating a small animation app using Android studio. When a button press images should work according to the particular animation.
There's an error with line viewGroup = (viewGroup)findViewById(R.id.ViewGroup);
It shows "Cannot resolve symbol ViewGroup". I typed Alt+Enter and selected a solution. But nothing happened.
public class MainActivity extends Activity {
private ViewGroup viewGroup;
private ImageView imageView, imageView2, imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10,imageView11,imageView12;
private Button button,button1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView5= (ImageView) findViewById(R.id.imageView5);
imageView6=(ImageView) findViewById(R.id.imageView6);
imageView7=(ImageView) findViewById(R.id.imageView7);
imageView8=(ImageView) findViewById(R.id.imageView8);
imageView9=(ImageView) findViewById(R.id.imageView9);
imageView10=(ImageView) findViewById(R.id.imageView10);
imageView11=(ImageView) findViewById(R.id.imageView11);
imageView12=(ImageView) findViewById(R.id.imageView12);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TransitionManager.beginDelayedTransition(viewGroup, new Fade());
fade(imageView, imageView2, imageView3,imageView4,imageView5,imageView6);
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Slide());
toggle(imageView7,imageView8,imageView9,imageView10,imageView11,imageView12);
}
});}
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
private static void fade(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
Here's my activity_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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/myAniLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignBottom="#+id/button"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/ic_launcher"
android:layout_alignTop="#+id/imageView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#android:drawable/star_big_on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView9"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView9"
android:layout_alignStart="#+id/button1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView8"
android:layout_alignEnd="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#android:drawable/star_big_on"
android:layout_above="#+id/button"
android:layout_alignStart="#+id/imageView4" />
Your logcat shows
"Cannot resolve symbol 'ViewGroup'"
viewGroup=(viewGroup)findViewById(R.id.ViewGroup); // (ViewGroup)
viewGroup Not declare in your XML
(viewGroup) must be (ViewGroup) . (viewGroup)
The ViewGroup class is a subclass of the View class. ViewGroup
instances work as containers for View instances to group View
instances together.
http://developer.android.com/intl/es/reference/android/view/ViewGroup.html
I don't see an element ViewGroup in the xml. You need to add it in the same xml or include it.
As per your xml there is not an any id with ViewGroup in your xml code. please give id to your layout with ViewGroup and cast it as below,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
there was also a spelling mistake in your line,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
As they pointed out, you have no R.id.ViewGroup in your layout.
If you really need the view group try this:
viewGroup = (ViewGroup)findViewById(android.R.id.content)
The problem is that you have,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
and you have to change it for this,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
The V must be a capital letter.
Also watch for the id of your Viewgroup in your xml, check if it is R.id.Viewgroup or R.id.viewgroup

Java, XML - onClick crashes my program

UPDATE: So I forgot to clarify this, but the button is at first non-existent on the App UNTIL dynamically added. Once the layout has been inflated with the button, the button then causes the crash. I apologize for the issues that came up from this development.
So there is a button that keeps crashing my program no matter what (unless I remove the onClick). I tested every line in my Java code to see what was causing it but even after leaving it empty, it still crashes it.
From my MainActivity.Java, the method that causes the crash:
public void changeState(View v) {
String owe = "O";
String debt = "D";
button = (Button) findViewById(R.id.decl);
if (button.getText().toString().equals("D")) {
button.setText("O");
} else {
button.setText(("D"));
}
}
Also from the Java file, the layout inflation method that creates the button causing the crashes:
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}
The above Java code inflates this xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#CCFFFF"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="81dp"
android:orientation="horizontal">
<EditText
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:layout_weight="2"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="14dp"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F" />
<Button
android:id="#+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
<!--android:onClick="changeState"-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">\
<EditText
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="(Ex 123.25)"
android:inputType="numberDecimal"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
<EditText
android:id="#+id/owed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="(Ex $, coffee)"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:orientation="vertical"
android:background="#CFCFCF"
/>
The Button:
<Button
android:id="#+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:onClick="changeState"
/>
Crash log
08-09 22:44:22.792 1951-1951/com.xephos.detra E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xephos.detra, PID: 1951
java.lang.IllegalStateException: Could not find a method changeState(View) in the activity class android.app.Application for onClick handler on view class android.widget.Button with id 'decl'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at ...
Edit:
Put this in your MainActivity after setContentView():
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
Button b = (Button)view.findViewById(R.id.decl);
if (b != null) {
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
if ("D".equals(b.getText().toString())) {
b.setText("O");
} else {
b.setText(("D"));
}
}
});
}
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}

RadioButton setChecked NullPointerException

I don't get this to work and I don't see why.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference
at eu.twenty1media.snakeplus.fragment.MainFragment.onCreateView(MainFragment.java:48)
String lastlevel = msettings.getLevel();
Log.d("MainFragment.java", "Last level from Database: radio"+lastlevel);
RadioGroup rLevelGroup = (RadioGroup) getActivity().findViewById(R.id.radioLevel);
int resourceId = this.getResources().getIdentifier("radio"+lastlevel, "id", getActivity().getPackageName());
//rLevelGroup.check(R.id.radioextreme); //Nullpointer!
//RadioButton levelbtn = (RadioButton) getActivity().findViewById(R.id.radioextreme); //NULLPOINTER
//levelbtn.setChecked(true);
I tried both methods at the bottom, but none of them seems to work, I always get a NPE. I tried using the resourceId String but as you can see a normal resource id aswell.
<RadioGroup
android:id="#+id/radioLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_marginBottom="16dp"
android:gravity="center">
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radionormal"
android:text="#string/level_normal"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radiohard"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:text="#string/level_hard"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radioextreme"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:text="#string/level_extreme"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
</RadioGroup>
Thanks for the help.
EDIT1: With commenting the Lines below i don't get a Nullpointer Exception.
If it is an an activity:
RadioButton levelbtn = (RadioButton) findViewById(R.id.radioextreme);
levelbtn.setChecked(true);
If it is in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.page_two, container, false);
RadioButton levelbtn = (RadioButton) v.findViewById(R.id.radioextreme);
levelbtn.setChecked(true);
return v;
}
You can also set a radio button checked within the XML
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="#+id/radioButton" />

Categories

Resources