Why settings.xml layout is overlapping the ActionBar/Toolbar? - java

I'm developing a material design app.
After adding settings.xml in SettingsActivity.java & running the app, the activity is appearing like this:
Here's SettingsActivity.java file's code:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
Here's activity_settings.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
</RelativeLayout>
Here's settings.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="#string/pref_notification" >
<CheckBoxPreference
android:defaultValue="true"
android:key="prefNotification"
android:summary="#string/pref_notify_summary" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
I don't know why the settings.xml layout is overlapping the ActionBar/Toolbar!
Please let me know.
Thanks in advance.

Change your activity_settings.xml to this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent" />
</RelativeLayout>
And change code to this
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();

When you call the fragment on the activity, don't use
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
Instead use
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.Your_Frame_ID_On_xml, new SettingsFragment())
.commit();
I had the same problem for using android.R.id.content so changing it to the ID you use for the frame to hold the fragment on the xml fixed it.

Discovered that PreferenceActivity in Sunny's solution above may crash due to it loads a content view without a ListView. This is fixed by adding it to activity_settings.xml, but the PreferenceFragment will just ignore it. The activity_settings.xml will the look something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/common_actionbar" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Related

How to set onclick event for the navigation icon in Material Components top app bars (Android)

I created a top app bar by using the Material Components(Android), and changed the navigation icon from the menu icon to an arrow icon. The on click event is back to the previous page. I have already read through the official docs and it doesn't work in my case. I have no idea how to set on click event for that arrow icon. Any help is appreciated.
A better image to understand what I am talking
Click me
Link of the docs Click me
settingsActivity.java
package studio.itztaylorau.dashboard;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import studio.itztaylorau.dashboard.R;
import studio.itztaylorau.dashboard.AccountActivity;
public class SettingsActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
View topAppBar = findViewById(R.id.topAppBar);
topAppBar.setNavigationOnClickListener {
Intent i = new Intent(getApplicationContext(), Account.class);
startActivity(i);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
settingsActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="#+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="#string/topAppBarTitle_Settings"
android:background="#color/appTopBar"
app:navigationIcon="#drawable/ic_baseline_arrow_back_24"
/>
</com.google.android.material.appbar.AppBarLayout>
</FrameLayout>
</LinearLayout>
You're making a fragment transaction on the settings FrameLayout placeholder, but this placeholder also contains the AppBarLayout of the toolbar.
To solve this:
Step 1: Get the AppBarLayout out of the Fragment FrameLayout placeholder, So, change the layout to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/appTopBar"
app:navigationIcon="#drawable/ic_baseline_arrow_back_24"
app:title="#string/topAppBarTitle_Settings" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Step 2: Instead of topAppBar.setNavigationOnClickListener, use topAppBar.setOnClickListener
View topAppBar = findViewById(R.id.topAppBar);
topAppBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Account.class);
startActivity(i);
}
});

RecyclerView doesn't Show Data When Adding two fragments to FragmentManager

I have 2 fragments with recyclerview. InteractionsFragment and its copy InteractionsFragmentCopy (exact same replica of InteractionsFragment). When I try to add them, only the one that gets added first its recyclerview data is displayed, the other fragment's recyclerview data is blank as shown on the screenshot:
---Activity code---
public class HomeActivity extends AppCompatActivity {
private InteractionsFragment interactionsFragment = InteractionsFragment.newInstance();
private InteractionsFragmentCopy interactionsFragmentCopy = InteractionsFragmentCopy.newInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, interactionsFragment).show(interactionsFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container2, interactionsFragmentCopy).show(interactionsFragmentCopy).commit();
}
}
---Layout file---
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.HomeActivity"
android:background="#color/Blue">
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
android:orientation="vertical"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".9"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
android:background="#color/Yellow">
</FrameLayout>
<FrameLayout
android:layout_margin="16dp"
android:background="#color/Yellow"
android:id="#+id/fragment_container2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".9"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent">
</FrameLayout>
</LinearLayout>
Any idea why this is happening?
I have the answer here
answer
It turned out that since I'm using viewpager inside the fragment then I hade to change the viewpager adapter instantiation from:
InteractionsFragment.ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
to
InteractionsFragment.ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
That's fixed the issue

Imageslider with description

I have a fully working imageslider with an Adapter and indicator dots. But now I want to add descriptions for each image with a TextView. Anyone have an idea how to do this? Thank you.
EDIT: see code below
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private String[] imageUrls = new String[]{
"http://image1.png",
"http://image2.png",
"http://image3.png",
"http://image4.png",
"http://image5.png"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(viewPager);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" />
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="#+id/tabDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabBackground="#drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
</RelativeLayout>
remove image view from xml file. only keep viewpager element there.
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Then u need to create custom adapter with layout what u want. You can find steps to create custom adapter by following this https://inducesmile.com/android/understanding-android-viewpager-and-custom-pageradapter/
then u need to change viewPager.setAdapter(adapter); to set object of your custom adapter

Why am I not getting PreferenceCategory's summary even after declaring it in settings.xml file?

I'm developing a material design app & I have declared an xml file for SettingsActivity.
The problem I'm encountering is that the PreferenceCategory's android:summary tag is not showing any summary even after declaring it in settings.xml.
This is the screenshot of SettingsActivity.java (see: no summary here):
Here's SettingsActivity.java file's code:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "prefNotify";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTheme(R.style.prefCategoryStyle);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
Here's activity_settings.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/windowBackground"
tools:context="com.abc.xyz.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent"/>
</RelativeLayout>
Here's settings.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="prefNotification"
android:title="#string/prefNotification"
android:summary="#string/pref_notify_summary">
<CheckBoxPreference
android:id="#+id/checkBoxPref1"
android:key="checkBoxPref1"
android:summary="#string/checkBoxPref1"
android:defaultValue="false"/>
<CheckBoxPreference
android:id="#+id/checkBoxPref2"
android:key="checkBoxPref2"
android:summary="#string/checkBoxPref2"
android:defaultValue="true"/>
<CheckBoxPreference
android:id="#+id/checkBoxPref3"
android:key="checkBoxPref3"
android:summary="#string/checkBoxPref3"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>
Please let me know what is wrong here!
Thanks in advance.
You'll need to make your own layout to show the title and summary.
<PreferenceCategory
android:key="prefNotification"
android:title="#string/prefNotification"
android:summary="#string/pref_notify_summary"
android:layout="#layout/custom_preference_category">
and create custom_preference_category.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/listSeparatorTextViewStyle" />
<TextView
android:id="#android:id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" />
</LinearLayout>
Mess around with the layout to fit your needs

Android L support library Toolbar appears under views

I have a mainactivity.xml that has a framelayout and inside that has a toolbar
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<android.support.v7.widget.Toolbar
android:id = "#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</FrameLayout>
Inside my mainactivity.java, I have this
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
Based on the tutorial here, http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html, I should have a custom Toolbar that acts as an actionbar. However, the toolbar that appears happens under the fragments that I add in the block. The toolbar is supposed to appear above them and shift them downwards, but now the views cover the actionbar toolbar. Is this a glitch or am I simply forgetting something? This actually worked before but is now broken for some reason.
The Toolbar needs to be outside of the FrameLayout that is going to hold your Fragment like so:
<LinearLayout 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:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id = "#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
</FrameLayout>
</LinearLayout>

Categories