Add up button to PreferenceScreen - java

I can't figure out how I would go about implementing an up button in a PreferenceScreen. An up button displays a caret in your action bar next to your app icon that allows you to navigate the app's hierarchy, more info here.
I have a Preference Fragment that displays when my main activity is opened and I can get the up button to display by adding this line " getActionBar().setDisplayHomeAsUpEnabled(true);":
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
This causes the up button to display in the Preference Fragment, but I only want to show the up button when one of my PreferenceScreens is opened, allowing navigation to the main PreferenceFragment.
My app is analogous to the main settings app. Only the child screens, like Location Access, that opens from the main Settings app has the up arrow.

From this question, I simply added these two code blocks to my Preference Fragment:
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
initializeActionBar((PreferenceScreen) preference);
}
return false;
}
And this one:
/** Sets up the action bar for an {#link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}

If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.

Have a look at this.
For the XML:
<Preference android:title="Acts like a button"
android:key="button"
android:summary="This will act like a button"/>
Then for the Java in your onCreate()
Preference button = (Preference)findPreference("button");
button.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for what you want it to do
return true;
}
});

Related

How to make radio buttons display a text in another activity when clicked in android studio?

I have 6 options that the user can select from and a button that takes them to the next page when clicked. I have two pages like this. After one choice from each page is selected, I would like to display certain text depending on the radio buttons clicked previously, in another activity. How can I do this in java in android studio?
If you are controlling the FragmentManager you can just pass the options as an argument to the next Fragment's constructor otherwise, you can save everything in a static variable (as long as no Views are in there, so don't store the radio buttons there) and access that variable from outside.
Like this:
public class MyFragment {
public static boolean isRadioButtonXPressed = false; //change to true if it's pressed by default
public void onCreate(...) {
radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isRadioButtonXPressed = checked;
}
});
}
}
//from the other fragment
public class MyFragment2 {
public void onCreateView(...) {
if (MyFragment.isRadioButtonXPressed) {
//it's been pressed
} else {
//it's not been pressed
}
}
}
There are many different ways.
As you are using activity, you can use Intent to pass data. Here is a sample:
Intent page = new Intent(FirstActivity.this, SecondActivity.class);
page.putExtra("key", "This is my text");
startActivity(page);
For getting the value on Second Activity onCreate() method:
String value = getIntent().getStringExtra("key");

How to create navigation view open on condition?

First of all, I have two navigation view for both sides in android as shown in this Image. Left side for user who signup as parents meanwhile the right side for the user who signup as a tuition provider. For example, if User A signs up as a parent, so he/she can only open the left side navigation ONLY. I have made some studies, mostly using radio button/radio group. Unfortunately, I have to use the spinner, what coding should I write to ensure the navigation view can be opened for certain users. For your information, I have 3 activity.
1) User.Java wherein coding, I have coding initialize, constructor and getter.
2) RegistrationActivity.Java where I put all the spinner coding.
//USER TYPE SPINNER
List<String> categories = new ArrayList<>();
categories.add(0, "Choose Category");
categories.add("Parents");
categories.add("Tuition Provider");
ArrayAdapter<String> dataAdapter;
dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
RegisterUserType.setAdapter(dataAdapter);
RegisterUserType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==0)
{
//Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
3) Home.Java where I put all my navigation view coding.
menuLeft = (ImageButton) findViewById(R.id.menu_left);
menuRight = (ImageButton) findViewById(R.id.menu_right);
parentsNavigation = findViewById(R.id.nav_view);
tuitionProviderNavigation = findViewById(R.id.nav_view2);
menuLeft.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawer(GravityCompat.START);
}
else
{
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
menuRight.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (drawerLayout.isDrawerOpen(GravityCompat.END))
{
drawerLayout.closeDrawer(GravityCompat.END);
}
else
{
drawerLayout.openDrawer(GravityCompat.END);
}
}
});
parentsNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
UserMenuSelector(item);
return false;
}
});
tuitionProviderNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
UserMenuSelector(item);
return false;
}
});
I can not paste all my coding here because it is too long but I can email you to better understand my XML layout.
All you have to do is lock the drawer that you want to Disable and unlock again if you want to Enable it
use drawerLayout.setDrawerLockMode();
in your case below code will close your one drawer that have gravity start
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
and when you want to unlock again then simply
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START);
Remember one more thing
drawerLayout.setDrawerLockMode() allows the application to restrict the user's ability to open or close the given drawer (cannot open via sliding) but DrawerLayout will still respond to calls to openDrawer(), closeDrawer()
so in your case you also consider disabling the ImageView click because they have these methods
In Your Case
when user will click on a spinner item you check if(position==1)
if this condition true you know it's a parent
so do this in your Home.Java
do this
if(parent==true){
//this will disable right drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
//and this will enable left drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START);
//and also do to disable right ImageView
menuRight.setEnabled(false);
//and also do to enable left ImageView
menuLeft.setEnabled(true);}
now for tuition provider do same but this time enable right and disable left,
also same for menu button.

Android when fragment is removed

I have a FrameLayout and put there some fragments by click on button, next click should remove fragment from FrameLayout, I do this by removeAllViews() (FrameLayout is in another Fragment so translaction method is in Activity).
I need to do some action when removeAllViews() starts and have to do it in Fragment class but something goes wrong.
I tried:
OnDestroy()
OnDestroyView()
OnPause()
in Fragment class
but it works like:
put Fragment in FrameLayout (from Activity)
use removeAllViews() (from Activity)
there is no Fragment in FrameLayout (is clear) but nothing else happens and methods are not working
put new Fragment in FrameLayout (from Activity) - now all methods (OnDestroy() from Fragment class) works (probably it's real time to destroy old fragment)
How is it possible to 'get moment' when Fragment is not exists for user? I want to send some information to server if user hides Fragment.
#Edit3
code from method from Activity where I want to make translaction
public void showProductsList(String productType,int containerID){
List<String> prodNames = new ArrayList<String>();
List<Long> prodIds = new ArrayList<Long>();
DatabaseDAOProdProtein dao = new DatabaseDAOProdProtein(getApplicationContext());
dao.open();
List<DatabaseProduct> productList = dao.getAllProducts();
for(int i=0;i<productList.size();i++){
prodNames.add(productList.get(i).getName());
prodIds.add(productList.get(i).getId());
}
dao.close();
ProductsList productsList = new ProductsList(productType,prodNames,prodIds);
productsList.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
Toast.makeText(getApplicationContext(),"action1 " ,Toast.LENGTH_LONG).show();
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
Toast.makeText(getApplicationContext(),"action2 " ,Toast.LENGTH_LONG).show();
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
Toast.makeText(getApplicationContext(),"action3 " ,Toast.LENGTH_LONG).show();
}
}
});
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(containerID, productsList).commit();
}
I used this method in another Fragment by:
((MainActivity) getContext()).showProductsList("carb", carbContainer.getId());
there is an error:
Error:(560, 21) error: cannot find symbol method setOnSystemUiVisibilityChangeListener(<anonymous OnSystemUiVisibilityChangeListener>)
You say:
"How is it possible to 'get moment' when Fragment is not exists for
user? I want to send some information to server if user hides
Fragment."
I now know you did not mean "hide", so just use the OnDestroy() method.
Try this to trigger the "hide"
View topLevelLayout = findViewById(R.id.top_layout);
topLevelLayout.setVisibility(View.INVISIBLE);
You cannot go into stopped state while Fragment (Activity) is visible. Android destroying activities, killing processes
The best way to make sure something runs via a view is to run it via a post:
topLevelLayout.post(new Runnable()
{
#Override
public void run()
{
topLevelLayout.removeAllViews();
}
}
To get notified of system UI visibility changes, register an View.OnSystemUiVisibilityChangeListener to your view (fragment).
https://developer.android.com/training/system-ui/visibility.html
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getContext(),"action0 " ,Toast.LENGTH_LONG).show();
Fragment your_frag = new ProductsList(productType,prodNames,prodIds);
getSupportFragmentManager().beginTransaction().replace(containerID,your_frag).commit();
getSupportFragmentManager().executePendingTransactions();//make sure onCreateView has executed
your_frag.getRootView().setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
Toast.makeText(getContext(),"action1 " ,Toast.LENGTH_LONG).show();
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
Toast.makeText(getContext(),"action2 " ,Toast.LENGTH_LONG).show();
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
Toast.makeText(getContext(),"action3 " ,Toast.LENGTH_LONG).show();
}
}
});
}
A typical fragment looks like this:
public class HomeFragment extends Fragment {
View mRootView = null;
public HomeFragment(){}//null constructor
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_home, container, false);
return mRootView ;
}
public View getRootView ()
{
return mRootView;
}
}

making ActionBar icon dynamically visible/invisible

My MainActivity calls an AsyncTask to do some network I/O. When the AsyncTask begins, I would like to 'light up' an icon in the ActionBar. When the AsyncTask completes, I would like to make that ActionBar icon invisible, i.e. upon AsyncTask completion, not via a button click.
At this point I can successfully light up the ActionBar icon upon AsyncTask start. At the completion of AsyncTask, I send another broadcast back to MainActivity to indicate that AsyncTask is completed. I need to be able to use this broadcast to make the ActionBar icon invisible. I am attempting to used InvalidateMenuOptions() to force onPrepareMenuOptions to recreate and make the ActionBar icon invisible, but it is not working. Here is my attempt so far - all code is excerpted from my MainActivity class
excerpt from MainActivity:
public class MainActivity extends Activity {
private static boolean dataXferRunning = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
// broadcast receiver to determine if BasestationTransfer is active
basestationBroadcastReceiver = new BasestationBroadcastReceiver();
basestationIntentFilter = new IntentFilter("XFER_RUNNING_ACTION");
boolean hasWifi = CWICUtil.getNetworkState(this);
if(hasWifi) {
if(dataXferRunning == false) {
containerCount = 0;
DBAdapter dbAdapter = CWICApplication.dbAdapter();
try {
containerCount = (int)dbAdapter.getUnsentContainersCount();
} catch (SQLException e) {
Log.e(TAG, "getUnsentContainersCount() failed: " + e.toString());
e.printStackTrace();
}
if(containerCount > 0) {
invalidateOptionsMenu();
startTransfer(); //AsyncTask
invalidateOptionsMenu();
}
}
}
}
}
my BroadcastReciever:
public static class BasestationBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
intent.getAction();
boolean isRunning = intent.getBooleanExtra("XFER_RUNNING", false);
if(isRunning) {
dataXferRunning = true;
} else {
dataXferRunning = false;
}
}
};
my onPrepareOptionsMenu:
#Override
public boolean onPrepareMenuOptions(Menu menu) {
if(dataXferRunning) {
menu.findItem(R.id.action_socketServiceConnected).setVisible(true);
} else {
menu.findItem(R.id.action_socketServiceConnected).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
So to summarize - I'm successfully receiving broadcasts from my AsyncTask with correct values. I have seen much code that does this with a buttonClick, however my issue is dynamically making my ActionBar icon invisible upon completion of AsyncTask. Also - this is my first time posting, let me know if I can clarify any issues plus thanks to this community because you have been getting me out of jams for months now.
In onCreateOptionsMenu(), you can hold on to a reference of your Menu object (via a class variable, or some other mechanism of your choice), like this:
Menu menu;
#Override
public boolean onCreateOptionsMenu( Menu menu ) {
getSupportMenuInflater().inflate( R.menu.my_menu, menu );
this.menu = menu;
return true;
}
Then, when you want to dynamically adjust the menu item's visibility, you can reuse that menu object reference to call menu.findItem(R.id.myId) to get your individual menu items and adjust their properties dynamically however you'd like.

Android - How to make buttons listen more than one time

I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.
The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.
Here is some sample code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
setContentView(R.layout.main); // return to home screen
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.
in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed...
http://developer.android.com/guide/components/fragments.html
I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.
The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener,
when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.
To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners
from the home screen again.
I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...),
as well as when the back button is clicked:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listeners(); // set up all the listeners for the buttons
}
public void listeners()
{
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
listeners(); // recursively call the listeners again to 'start over'
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

Categories

Resources