I'm trying to set my tabs text textAllCaps to false.
For some reason only firs tab has needed results, but the second is still all caps.
https://imgur.com/a/fnv2DzY
I've set tabTextAppearance to MyCustomTextAppearance and in styles two attributes are set to textAllCaps, android:textAllCaps are set to false
activity:
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar"
app:tabTextAppearance="#style/MyCustomTextAppearance" />
in styles:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
in Java:
public class NewLoginActivity extends BaseActivity implements
RegisterFragment.OnFragmentInteractionListener,
LoginFragment.OnFragmentInteractionListener {
public ActivityNewLoginBinding binding;
private List<String> titles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_new_login);
setSupportActionBar(binding.toolbar);
setToolBarTitle(getSupportActionBar(), getResources().getString(R.string.login_action_title));
if (getSupportActionBar() != null){
getSupportActionBar().setHomeButtonEnabled(false); // Disable the button
getSupportActionBar().setDisplayHomeAsUpEnabled(false); // Remove the left caret
getSupportActionBar().setDisplayShowHomeEnabled(false); // Remove the icon
}
binding.tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
titles = new ArrayList<>();
titles.add(getString(R.string.login_action_title));
titles.add(getString(R.string.regi));
Bundle bundle = getIntent().getExtras();
LoginTabAdapter tabAdapter;
tabAdapter = new LoginTabAdapter(getSupportFragmentManager(), this, titles);
binding.viewPager.setAdapter(tabAdapter);
binding.viewPager.setOffscreenPageLimit(1);
binding.tabLayout.setupWithViewPager(binding.viewPager);
if (bundle != null && bundle.containsKey("signup")) {
binding.viewPager.setCurrentItem(2);
}
}
Related
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent+?actionBarSize">
you can see 3rd line of code how to add this two value and get the height with adding two vale "android:layout_height="match_parent+?actionBarSize" in the our program in android studio in java.
Your query is not clear.
You can't do it in XML
android:layout_height="match_parent+?actionBarSize"
However if you want to hide ActionBar from the entire App, you can use
1. styles.xml
<resources>
<!---Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!---Customize your theme here.-->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
2. Hide ActionBar from any particular activity using Java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Take instance of Action Bar
// using getSupportActionBar and
// if it is not Null
// then call hide function
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
}
}
3. Hide ActionBar while user interaction using WindowManager
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// set Windows Flags to Full Screen
// using setFlags function
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
4. Hide ActionBar from any particular activity using try-catch
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// try block to hide Action bar
try {
this.getSupportActionBar().hide();
}
// catch block to handle NullPointerException
catch (NullPointerException e) {
}
}
}
I am a beginner Android developer and I have been struggling the issue that I can't see the toolbar in any activity that inherits Base Activity. According to other resource, to use the same toolbar in the different activities. I have to implement it in Base Activity and inherit it where I need to use it. Could anyone help me figure out the problem?
styles.xml
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
quiz.menu.xml inside of menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/contact"
android:icon="#drawable/ic_contacts_black_24dp"
android:title="Contact"
app:showAsAction="ifRoom" />
<item android:id="#+id/language"
android:title="Language"
app:showAsAction="never" />
<item android:id="#+id/speech"
android:title="Speech"
app:showAsAction="never">
<munu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</munu>
</item>
</menu>
BaseActivity
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.quiz_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.contact:
Toast.makeText(this, "Contact is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.language:
Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.speech:
Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.subitem1:
Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.subitem2:
Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
WelcomeActivity (inherits base Activity)
public class WelcomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
final Button databaseButton = findViewById(R.id.database);
databaseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
Intent databaseIntent = new Intent(WelcomeActivity.this, Questionnaire.class);
startActivity(databaseIntent);
}
});
}
Questionnaire
public class Questionnaire extends BaseActivity {
public Spinner languageSpinner;
public int languageId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionnaire);
...
}
}
The answer is actually simple. You firstly set contentview in OnCreate() method of the BaseActivity class, then you change the view to another xml file in OnCreate() methods of the child classes.
What I suggest is that you do not implement OnCreate() method in BaseActivity class but implement SetContentView() method in BaseActivity.
In short, delete onCreate() method from BaseActivity() and add setContentView() method in the below.
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
P.S - Your quiz_menu.xml file does not look like a menu file. :)
P.P.S - Let me know if it works, or if you have troubles. :)
In my android app I have a default app theme which has no action bar since I am adding my own toolbar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
So this works well with my other activities except settings activity, So as a quick fix I've decided to add a custom style for it via
<style name="SettingsMenu" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorBackground">#color/colorPrimaryDark</item>
<item name="textColor">#color/colorAccent</item>
</style>
and also in the manifest I've added
<activity
android:name=".backend.settings.SettingsActivity"
android:theme="#style/SettingsMenu" //theme here
android:label="#string/title_activity_settings">
</activity>
So in my settings activity which extends AppCompatPreferenceActivity i have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So the above still doesnt show the toolbar.
I have also tried adding a custom toolbar via
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
//here assign the toolbar
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So in my both tries neither toolbar nor actionbar works
Where am i going wrong?
I want to change DatePickerDialogs and TimePickerDialogs globally using their spinner style. For that case I added android:datePickerStyle and android:timePickerStyle to my AppTheme. When I was testing the style changes, everything is fine on a virtual emulated Nexus device. But when I test it on an Samsung device, there where noch changed styles loaded. Both devices run with Nougat.
Here is a snippet from my style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:datePickerStyle">#style/AppTheme.DatePicker</item>
<item name="android:timePickerStyle">#style/AppTheme.TimePicker</item>
</style>
<style name="AppTheme.DatePicker" parent="android:Widget.DatePicker">
<item name="android:spinnersShown">true</item>
<item name="android:calendarViewShown">false</item>
</style>
<style name="AppTheme.TimePicker" parent="android:Widget.Material.TimePicker">
<item name="android:timePickerMode">spinner</item>
</style>
And that is the way I call the DatePickerDialog. (The TimePickerDialogs are called the same way)
DatePickerDialog datePickerDialog = new DatePickerDialog(
getView().getContext(), SettingsFragment.this, 2000 , 10, 1);
datePickerDialog.show();
Settings.Fragment is the fragment that calls the dialogs.
This it how it should look
Nexus device
This it how it should not look
Samsung device
Edit:
The Nexus device is a virtual emulated Nexus 5X with Android 7.1.1 and the Samsung device is a Samsung S7 Edge wit Android 7.0.
I found a workaround for the problem myself.
The workaround is not to use the global styles. Instead I had to write my own DatePickerDialog.
In the custom dialog layout I could use the DatePicker preferences I wanted. This looks like this:
<?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="match_parent"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="#+id/spinnerDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:focusable="true"
android:spinnersShown="true" />
</LinearLayout>
The custom dialog looks like the following:
public class SpinnerDatePickerDialog extends DialogFragment {
private ISpinnerDatePickerDialogListener listener;
private DatePicker datePicker;
// this is no clean android fragment constructor, but we want to use it here
#SuppressLint("ValidFragment")
public SpinnerDatePickerDialog(ISpinnerDatePickerDialogListener listener) {
super();
this.listener = listener;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
datePicker = (DatePicker) view.findViewById(R.id.spinnerDatePicker);
datePicker.updateDate(defaultYear, defaultMonth, defaultDay);
builder.setView(view);
builder
.setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
listener.onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog.this);
}
})
.setNegativeButton(R.string.abort, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
listener.onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog.this);
}
});
return builder.create();
}
}
To send values to the original fragment, I created an interface. The original fragment implements this interface and the custom dialog gets the fragment as an parameter in his constructor. This way I can trigger the listener in the original fragment.
The interface:
public interface ISpinnerDatePickerDialogListener {
void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog);
void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog);
}
The fragment class that calls the dialog:
public class SettingsFragment extends Fragment implements ISpinnerDatePickerDialogListener, ISpinnerTimePickerDialogListener {
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// initialize listeners for text inputs, to open picker dialogs
periodBegin = (EditText) getView().findViewById(R.id.editPeriodBegin);
periodBegin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(PERIOD_BEGIN_DIALOG_ID);
}
});
}
/**
* opens dialog for id
*
* #param id
*/
private void showDialog(int id) {
switch(id) {
case PERIOD_BEGIN_DIALOG_ID:
SpinnerDatePickerDialog datePickerDialog;
datePickerDialog = new SpinnerDatePickerDialog(this);
datePickerDialog.show(getFragmentManager(), datePickerDialog.getTAG());
break;
}
#Override
public void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog) {
// TODO send DatePicker values with listener
// load DatePicker from dialog and set them to EditText text
DatePicker datePicker = dialog.getDatePicker();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
// TODO
String formattedDate = FORMAT.format(new Date(year - 1900, month, day));
periodBegin.setText(formattedDate);
}
#Override
public void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog) {
// Nothing to do here
}
}
PreferenceActivity:
public class UsersettingsActivity extends PreferenceActivity
{
public EditTextPreference nomePreferences;
public static float currValue;
public static ListPreference lista;
public static CharSequence currText;
public static String ilmionome;
public CheckBoxPreference checkboxPrefSpeech;
public CheckBoxPreference checkboxPref;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
this.overridePendingTransition(R.anim.in, R.anim.out);
/** Customizzo la actionbar */
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbarcustom);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
nomePreferences = (EditTextPreference)getPreferenceManager().findPreference("nomepreferenze");
lista = (ListPreference)getPreferenceManager().findPreference("prefSyncFrequency");
currText = lista.getEntry();
currValue = Float.parseFloat(lista.getValue());
Toast valori = Toast.makeText(getBaseContext(), currText, Toast.LENGTH_SHORT);
valori.show();
if(currValue == 2){
currValue = MainActivity.tts.setSpeechRate(2.0f);
} else if (currValue == 1) {
currValue = MainActivity.tts.setSpeechRate(1.0f);
} else if (currValue == 0.5) {
currValue = MainActivity.tts.setSpeechRate(0.5f);
}
}
}
the arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="syncFrequency">
<item name="2">Double/item>
<item name="1">Normal</item>
<item name="0">half</item>
</string-array>
<string-array name="syncFrequencyValues">
<item name="2">2</item>
<item name="1">1</item>
<item name="0">0.5</item>
</string-array>
</resources>
Well, if i choose "Double" for example, and i exit from this activity to come back to my MainActivity, the speech rate still the same! I need exit from app and then it works! So it means that in the onResume() of MainActivity needs something that calls the values from the PreferenceActivity.. Understand now?
EDIT:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Opzioni lettura sms"
android:fontFamily="sans-serif-light"
android:title="Opzioni">
<CheckBoxPreference
android:key="firstDependent"
android:fontFamily="sans-serif-light"
android:summary="#string/autorizza"
android:title="#string/letturanotifiche"
/>
<EditTextPreference
android:title="#string/nomepreferences"
android:key="nomepreferenze"
android:fontFamily="sans-serif-light"
android:summary="#string/nomepreferencesdesc"
android:inputType="text"
android:dialogTitle="#string/nameis"
/>
<ListPreference
android:key="prefSyncFrequency"
android:entries="#array/syncFrequency"
android:summary="#string/pref_sync_frequency_summary"
android:entryValues="#array/syncFrequencyValues"
android:title="#string/pref_sync_frequency" />
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
public class SomePreference extends PreferenceActivity implements OnSharedPreferenceChangeListener{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.task_pref);
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
// Register OnChangeListener
SP.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
//Make sure the item changed was the list_preference
if(key.equals("")){
String value = sharedPreferences.getString(key, "Double");
Intent i = new Intent(this, Main.class);
startActivity(i);
}
}
}
Main Activity
public class Main extends Activity{
protected void onCreate(Bundle savedInstanceState){
\\setup rest here.
SharedPreferences share_someting = PreferenceManager.getDefaultSharedPreferences(this);
String ss = share_something.getString("prefSyncFrequency", "Double");
}
protected void onResume(){
\\setup rest here.
SharedPreference share_something = PreferenceManager.getDefaultSharedPrefernce(this);
String ss = share_something("prefSyncFrequency", "Double");
}
protected void onCreate(){
text_speech();
}
protected void onResume(){
text_speech();
}
public void text_speech(){
SharedPrefernces spp = PreferenceManager.getDefaultSharedPreferences(this);
String sf = spp.getString("prefSyncFrequency", "Double");
if(sf.equals("two")){
currValue = MainActivity.tts.setSpeechRate(2.0f);
}else if(sf.equals("single")){
currValue = MainActivity.tts.setSpeechRate(1.0f);
}else (sf.equals("half")){
currValue = MainActivity.tts.setSpeechRate(0.5f);
}
I just changed below to follwing, I think its easier work this way.
<string-array name="syncFrequencyValues">
<item >two</item>
<item >single</item>
<item >half</item>
</string-array>