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);
}
...
}
Related
I am currently trying to link up my xml textviews to my Profile Fragment. However, I get a null object reference error on the line
nameAge = getView().findViewById(R.id.nameAge);
Here is the Fragment.java
package com.example.helloworld2;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
public class ProfileFragment extends Fragment{
private TextView nameAge;
private TextView occupation;
private TextView description;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_profile, null);
nameAge = getView().findViewById(R.id.nameAge);
occupation = getView().findViewById(R.id.occupation);
description = getView().findViewById(R.id.description);
return view;
}
}
Here is the xml file
<?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">
<ImageView
android:id="#+id/profile_image"
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:src="#drawable/nero" />
<TextView
android:id="#+id/nameAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/occupation"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/description"
android:paddingTop="40dp"
android:textSize="30dp"/>
</LinearLayout>
Thank you
do this in onViewCreated() not in onCreateView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, null);
return view;;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
nameAge =getActivity().findViewById(R.id.nameAge);
occupation = getActivity().findViewById(R.id.occupation);
description =getActivity().findViewById(R.id.description);
}
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>
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;
}
}
I am new in android programming. Just trying to use fragment. I am using the following code :
public class SimpleFragActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_fragment);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simple,
container, false);
Button btnClickMeBlue = (Button)rootView.findViewById(R.id.btnsimplefrag);
return rootView;
}
}
}
XML code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.framentexample.SimpleFragActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scale="centercrop"
android:src="#drawable/royal_enfield_blue" />
<Button
android:id="#+id/btnsimplefrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:text="Click Me Blue" />
</RelativeLayout>
but it throws an error saying
btnsimplefrag could not be resolved
Can anybody help me out? I tried many things and could not resolve the issue.
i have a class which extends fragment class as below:
public class FragmentCreateGroup extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ImageView group=(ImageView)findViewById(R.id.group_image);//shows error here
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_creategroup, container,false);
}
}
which is associated to the following xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/cr_group_grpname_desc"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/group_image"
android:layout_width="139dp"
android:layout_height="144dp"
android:src="#drawable/background" />
<EditText
android:id="#+id/cr_group_grpname_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:contentDescription="#string/cr_group_grpname_desc"
android:ems="10"
android:hint="#string/cr_group_grpname_hint"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/cr_group_creategrp_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:background="#drawable/greenbutton"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/cr_group_creategrp_btn_label" />
</LinearLayout>
I tried to access the ImageView in the fragment layout using findViewById(). Since I didn't extend Activity class I was unable to perform this action. Is there any way to access the UI Elements from a non activity class? if any how to perform?
You need to get the view after you inflate it in onCreateView
public class FragmentCreateGroup extends Fragment {
private ImageView group;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_creategroup, container, false);
group = (ImageView) v.findViewById(R.id.group_image);
group.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
group.setImageDrawable(); // set image here
}
});
return v;
}
}