Getting custom layout reference in PreferenceFragmentCompat - android

I created a preference which has a custom ImageView layout which is something like this:
<Preference
android:key="profile_picture_preference"
android:layout="#layout/layout_profile_picture"></Preference>
My layout is this:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileImageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#ABCDEF" />
But I don't know how can get a reference to it. I have tried to get it inside onCreateView method by doing this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
ImageView imageView = (ImageView) view.findViewById(R.id.profileImageView);
return view;
}
But ImageView is null.
Thank you for all your help.

I did not find any offical way. So I managed like this:
#Override
public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState);
recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
#Override
public void onChildViewAttachedToWindow(View view) {
if (view.getId() == R.id.profileImageView) {
profileImageView = (ImageView) view;
}
}
#Override
public void onChildViewDetachedFromWindow(View view) {
}
});
return recyclerView;
}

You can specify a custom layout in your theme. There you could add your ImageView
For example:
styles.xml
<style name="YourTheme" parent="Theme.AppCompat">
<!-- ... -->
<item name="preferenceTheme">#style/YourTheme.PreferenceThemeOverlay</item>
</style>
<style name="YourTheme.PreferenceThemeOverlay" parent="#style/PreferenceThemeOverlay">
<item name="android:layout">#layout/fragment_your_preferences</item>
</style>
fragment_your_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/profileImageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#ABCDEF" />
<!-- Required ViewGroup for PreferenceFragmentCompat -->
<FrameLayout
android:id="#+id/list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
And then in onViewCreated() of your fragment class you can start using the ImageView:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ImageView image_view = (ImageView)view.findViewById(R.id.profileImageView);
if (image_view != null)
{
// Your code
}
}

I have spent my day trying to solve this... Here is my answer : I have been using onCreateView instead of onCreatePreferencesto load the view :
public static class MyFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
//setPreferencesFromResource(R.xml.my_fragment_xml, rootKey); //remove this (!)
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false); //root layout from the fragment xml
view myView = view.findViewById(R.id.myView); //this works (!)
return view;
}
}

Related

How to use layout class in the fragment's onCreateView() method?

I have res/layout/main_menu_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<sidekick.cpp.CMainMenuButton
android:id="#+id/manualMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_manual_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/editorMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_editor_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/exitMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_exit_text" />
</LinearLayout>
This layout is used in the class below:
package sidekick.cpp;
import android.widget.*;
import android.content.*;
import android.view.*;
public class CMainMenuLayout extends LinearLayout {
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.main_menu_layout, null);
this.addView(view);
} }
Now I want to use the layout in a fragment. If I use the layout's ID (R.layout.main_menu_layout) in onCreateView() then the below code seems to work:
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_layout, container, false);
}
...
}
But I need to use the CMainLayout class, not R.layout.main_menu_layout. The code below seems to work too but is it correct?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CMainMenuLayout layout = new CMainMenuLayout(this.getContext());
return layout;
}
First, you can inflate xml layout in CMainMenuLayout directly
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_layout, this, true);
}
then use CMainMenuLayout class in xml for CMainMenuFragment. For example
res/layout/main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<sidekick.cpp.CMainMenuLayout
android:id="#+id/mainMenuLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
and in CMainMenuFragment
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
...
}

onClickListener in Button doesn't work

I'm trying to display log value on the console, when I click on a Button inside a fragment. I don't know why but nothing happens when I click.
I tried by all possible ways (onClick directly on the layout, setOnClickListener extends OnClickListener, setting "clickable"=true).
Thank you for the help!
public class displayFacesFragment extends Fragment {
public static final String TITLE = "Faces";
public String selectedPeriod;
public Set<String> idFurnitures;
Button button75;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View inputFragmentView = inflater.inflate(R.layout.fragment_display_faces, container, false);
button75 =(Button) inputFragmentView.findViewById(R.id.button75);
button75.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Test", "onClickListener");
}
});
return inputFragmentView;
}
<FrameLayout 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:id="#+id/fragment_faces"
tools:context="com.example.haddad.managemyrounds.controller.fragment.displayFacesFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<Button
android:id="#+id/button75"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>

How could i get ActionBar instance from AppCompatDialogFragment?

I need to get ActionBar instance from AppCompatDialogFragment.
public class EditTextFragment extends AppCompatDialogFragment{
EditText etContent;
TextEditedListener textEditedListener;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black);
//get ActionBar belongs to EditTextFragment
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if(etContent == null){
etContent = (EditText) inflater.inflate(R.layout.et_content,null);
}
return etContent;
}
interface TextEditedListener{
void onTextEdited(String txt);
}
}
I have try this,but it seems not work.
ActionBar actionBar = ((MyActivity) getContext()).getSupportActionBar();
setCustomActionBar(actionBar);
Thanks for any help
you can get action bar using typecasting to ActionBarActivity context.
Try this,
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
I find the likely answer here.So i solve the problem this way .
1.Create My own toolbar //fake_action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fake_action_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="#android:drawable/ic_menu_manage"
android:background="#color/background_material_dark"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<TextView
android:id="#+id/actionbar_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:textStyle="bold"
android:text="title"
android:textSize="18sp"
android:textColor="#FFFFFF" />
</android.support.v7.widget.Toolbar>
2.Add fake_action_bar to my content view
<?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:orientation="vertical"
>
<include
android:id="#+id/fake_action_bar"
layout="#layout/fake_action_bar" />
</LinearLayout>
3.Find the action_bar in AppCompatDialogFragment and init it.
public class EditTextFragment extends AppCompatDialogFragment {
ViewGroup mRootView;
Toolbar toolbar;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_NoTitleBar_Blue);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = (ViewGroup) inflater.inflate(R.layout.et_content, null);
etContent = (EditText) mRootView.findViewById(R.id.et_content);
toolbar = (Toolbar) mRootView.findViewById(R.id.fake_action_bar);
toolbar.inflateMenu(R.menu.menu_add);
toolbar.setNavigationOnClickListener(v -> dismiss());
toolbar.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case android.R.id.home:
dismiss();
return true;
case R.id.item_test:
if (textEditedListener != null) {
textEditedListener.onTextEdited(etContent.getText().toString());
}
dismiss();
return true;
}
return true;
});
}
return mRootView;
}
}

Android TextView setText results in empty text

I try to set the text of a TextView programmatically within a Fragment.
The code for the classes is as follows:
public abstract class AbstractFragment extends Fragment
{
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
{
this.setFileName();
}
protected abstract void setFileName();
}
public class ImplementingFragment extends AbstractFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View fragment = inflater.inflate(
R.layout.fragment_layout, container, false);
return fragment;
}
#Override
protected void setFileName()
{
String fileName = "Test.txt";
TextView textView = ((TextView) getActivity().findViewById(
R.id.text_file_name));
textView.setText(fileName);
}
}
The Layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/text_pdf_filename_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_pdf_filename"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text_pdf_file_name"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The weird thing is: within one Fragment it works, in another it does not (same parent Activity). Another fact is that after I set the text, I get it via textView.getText(). If I try to get the text in later code, I just get an empty string.
Additionally, if I debug the code, I see the text a view milliseconds, before it disappears.
Does anyone has an solution how to fix that behaviour?
Inside fragment use:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textView = ((TextView) rootView.findViewById(
R.id.text_file_name));
textView.setText(fileName);
return rootView;
}

Linkify in Android Fragment

I am trying to use linkify ina Fragment and I can't get it to actually linkify and I can't find my documentation on how to use it correctly. If someone could help that would be great!
ContactFragment.java:
public class ContactFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group,
Bundle saved) {
View V = inflater.inflate(R.layout.activity_contact_fragment, group, false);
TextView myEmail= (TextView) V.findViewById(R.id.textView1);
myEmail.setText("info#komodostudios.com");
Linkify.addLinks(myEmail, Linkify.EMAIL_ADDRESSES);
return inflater.inflate(R.layout.activity_contact_fragment, group, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
activity_contact_fragment.xml:
<RelativeLayout 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"
tools:context=".ContactFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="info#komodostudios.com" />
</RelativeLayout>
You are returning the wrong View from onCreateView method. It should be return V; instead of return inflater.inflate(R.layout.activity_contact_fragment, group, false);

Categories

Resources