How is using OnClickListener interface different via XML and Java code? [duplicate]

These are exactly the same. android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your DoIt method. Here is what using a android:onClick=”DoIt” does internally: Button button= (Button) findViewById(R.id.buttonId); button.setOnClickListener(new View.OnClickListener() { … Read more

How to change the color of a button?

The RIGHT way… The following methods actually work. if you wish – using a theme By default a buttons color is android:colorAccent. So, if you create a style like this… <style name=”Button.White” parent=”ThemeOverlay.AppCompat”> <item name=”colorAccent”>@android:color/white</item> </style> You can use it like this… <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:theme=”@style/Button.White” /> alternatively – using a tint You can simply … Read more

How to set focus to a button widget programmatically?

Yeah it’s possible. Button myBtn = (Button)findViewById(R.id.myButtonId); myBtn.requestFocus(); or in XML <Button …><requestFocus /></Button> Important Note: The button widget needs to be focusable and focusableInTouchMode. Most widgets are focusable but not focusableInTouchMode by default. So make sure to either set it in code myBtn.setFocusableInTouchMode(true); or in XML android:focusableInTouchMode=”true”

Rounded Button in Android

You can do a rounded corner button without resorting to an ImageView. A background selector resource, button_background.xml: <?xml version=”1.0″ encoding=”utf-8″ ?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <!– Non focused states –> <item android:state_focused=”false” android:state_selected=”false” android:state_pressed=”false” android:drawable=”@drawable/button_unfocused” /> <item android:state_focused=”false” android:state_selected=”true” android:state_pressed=”false” android:drawable=”@drawable/button_unfocused” /> <!– Focused states –> <item android:state_focused=”true” android:state_selected=”false” android:state_pressed=”false” android:drawable=”@drawable/button_focus” /> <item android:state_focused=”true” android:state_selected=”true” android:state_pressed=”false” android:drawable=”@drawable/button_focus” … Read more

How do I open a new fragment from another fragment?

Add following code in your click listener function, NextFragment nextFrag= new NextFragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.Layout_container, nextFrag, “findThisFragment”) .addToBackStack(null) .commit(); The string “findThisFragment” can be used to find the fragment later, if you need.

Android Fragment onClick button Method

Your activity must have public void insertIntoDb(View v) { … } not Fragment . If you don’t want the above in activity. initialize button in fragment and set listener to the same. <Button android:id=”@+id/btn_conferma” // + missing Then @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_rssitem_detail, container, false); Button … Read more

Change Screen Orientation programmatically using a Button

Yes it is implementable! ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); ActivityInfo.SCREEN_ORIENTATION_PORTRAIT setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ActivityInfo http://developer.android.com/reference/android/content/pm/ActivityInfo.html Refer the link: Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait); Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape); buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } }); buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }); http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html