Android EditTextPreference custom dialog: Add functionality to button - java

I am trying to add a preference which will store a top level path so that they app can auto-discover some files in that directory on the phone. I have added an EditTextPreference and changed the dialog of it so that it has a button as well as a text box.
<EditTextPreference android:id="#+id/etpTopLevelAutoDiscoverPath"
android:key="settings_top_level_AutoDiscover_path"
android:summary=""
android:title="Location to search for custom configurations"
android:dialogLayout="#layout/preference_browse_to_path"
android:defaultValue=""/>
I am struggling however gaining access to the button to add a click listener to it. I think my main problem is that I can't instantiate the custom dialogs view because I don't know when the dialog for EditTextPreference is displayed.
This is how I am attempting to set the get the button object but obviously when it gets to creating the button it crashes because the view doesn't exist.
View browseToPathView = this.findViewById(R.id.browseToPathView);
Button browse = (Button)browseToPathView.findViewById(R.id.btnRootBrowse);
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dtg.cinet/com.dtg.cinet.PreferencesScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
I am fairly new to android and have come into a project midway so it is entirely possible that I am just not doing it in the right way but I am fairly stuck at the moment and can't figure out what to do.

Related

Getting InstantiationException: Unable to instantiate fragment Error after changing system font

The app is crashing when the app is in the background and I changed the font and open the app again.
Getting Below error
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment could not find Fragment constructor
Steps
Open the bottom sheet dialog CategoriesBottomSheetDialog
Minimize the application (in the background)
Changed font
Open the app again
I can provide a fix using bundle but I want to know if using kotlin plugin no-arg concept can apply this annotation on CategoriesBottomSheetDialog and allow the class to be invoked with the empty constructor.
#NoArg
class CategoriesBottomSheetDialog(
override val di: DI,
screenType: MainScreenType
) : DIAware, BottomSheetDialogFragment() {}
I tried but not working even though the root cause is unable to invoke an empty constructor. Any inputs on this note to kind of solve without using bundle?

How do I change the main xml file from another activity?

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java
public class Settings extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Switch switchButton;
final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);
switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}
}
});
if (switchButton.isChecked()) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}}
public void toast1(View view) {
android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}
/*public void switch1(View view) {
ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setBackgroundColor(Color.GRAY);
}*/
}
In the Code I am trying to change the background of the main activity xml with :
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
and when I run the app and click the intent it will crash with the error:
"java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null
object reference"
I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:
Create button in settings activity which will change something in main activity.
Create onClickListener for your button.
Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)
I hope you understood the idea.
Link to the Android developer tutorial about App key values storing & saving
Link to the StackOverflow much easier explanation & examples
There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.
From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).
On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.
So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.
The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

Enabling button from another activity - Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference

I've created a login with SQLite on Eclipse, once the user enters their details it checks the database and returns true or false. If true is returned, I want the program to enable the 'My Account' button so that when the user exits the login form the My Account button is accessible.
The button is located in activity_main.xml.
The login works absolutely fine, it's just the button enabling.
The code below shows the error stated in the title. Do I need to use intents?
Login.java
if(success)
{
Log.d("login", "user logged");
Button actBTN=(Button)findViewById(R.id.myaccountbtn);
session = true;
actBTN.setEnabled(session);
}
else
{
Log.d("login", "user not logged");
}
As I understand, you have two activities: MainActivity and LoginActivity. The problems are that you are trying to access button in your MainActivity from LoginActivity. Android doesn't work this way - you have access only to your current view tree inflated from XML to particular activity. So I recommend to you take a look to startActivityForResult.You can handle your login flow inside LoginActivity, pass result back to MainActivity and then enable or disable button depending on login results.

Robotium - Trying to click home button in app

I'm new to robotium and i'm trying to write a quick and dirty script to run through all screens in an app.
The problem i have mainly with the 'home button' in the app. I've tried lots of options but i cant seem to get it to click there except with index, which is not what i want.
When i check out the button with the hierarchyviewer it looks like this:
Link
However when i try for example:
assertTrue(
"Wait for text (id: myapp.R.id.home) failed.",
solo.waitForImageById("myapp.R.id.home", 20000));
solo.clickOnImage((ImageView) solo.findViewById("myapp.R.id.home"));
solo.waitForActivity("MenuActivity");
It fails at the waitForImageByID line. Ive tried multiple options like waitForImageButton etc, but i just cant seem to get it clicked. What am i missing here?
junit.framework.AssertionFailedError: View with id: '0' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1990)
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1970)
at com.bitbar.recorder.extensions.OtherUtils.a(OtherUtils.java:246)
at com.bitbar.recorder.extensions.OtherUtils.b(OtherUtils.java:241)
at com.bitbar.recorder.extensions.v.a(Waiter.java:71)
at com.bitbar.recorder.extensions.ExtSolo.waitForImageButtonById(ExtSolo.java:4176)
at com.example.android.apis.test.Test.testRecorded(Test.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)
Use the following line to press the home button in the action bar
solo.clickOnActionBarHomeButton();
The issue is that the id that it is referring is not in your application, it is in androids default R file, try android.R.id.home and it should work fine. It is worth noting though that if your application uses action bar sherlock to support the action bar pre 4.0 that this will have a different ID there and you will have to handle this in your test.
You can see this for yourself looking at: http://developer.android.com/reference/android/R.id.html
When you are using ActionBarSherlock there are two different Ids you have to check, android.R.id.home for API-Level>11 and abs__home for lower levels (provided by ActionBarSherlock):
View homeButton = activity.findViewById(android.R.id.home);
if (homeButton == null) {
homeButton = activity.findViewById(R.id.abs__home);
}
What about this code:
ArrayList<LinearLayout> ll = solo.getCurrentViews(LinearLayout.class);
//You can change 1 with the ordinal number of LinearLayout you want to click.
solo.clickOnView(ll.get(1));
or also
ArrayList<ImageView> iv = solo.getCurrentViews(ImageView.class);
//You can change 0 with the ordinal number of Image you want to click.
solo.clickOnView(iv.get(0));
I think if you identify the correct id for view or linear layout or image view it should work.
Dave C's answer was working only partially for me. The button was clicked but before the preceding screen was loaded assertions had started and thus were always false. The solution is to run "home click" on the main thread (Robotium 5.2.1):
getInstrumentation().runOnMainSync(new Runnable() {
#Override
public void run() {
solo.clickOnActionBarHomeButton();
}
});
From your question I can see that it is an image view. You can click on any view using the following piece of code.
View view = solo.getView("View_name_from_hierachy_viewer");
solo.clickOnView(view);
View_name_from_hierachy_viewer in your case will be "home".
Let me know if this does not work.

Error caused when converting android activity based app to fragment based

I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference

Categories