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;
}
}
Related
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 want to call the LocationActivity when the user click on the button and get the location information and set it into my button.
xml file
<Button
android:id="#+id/locationTxt"
android:text="#string/setLocation"
android:textStyle="bold"
android:layout_weight="0.5"
android:layout_width="8dp"
android:layout_height="40dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:editable="false"
android:gravity="center"
android:background="#drawable/btn_shape" />
Java file
public class AddTask extends android.app.Fragment {
Button btnLocation;
public AddTask()
{
// Constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_task, container, false);
btnLocation = (Button) v.findViewById(R.id.locationTxt);
btnLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I can't use OnClickListener in this Fragment :
public class Note_Fragment extends Fragment{
ImageView btn_back;
private OnClickListener mclick = new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("test", "YEH");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Note_Fragment()
{
super();
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.note_fragment, container, false);
btn_back = (ImageView) rootView.findViewById(R.id.btn_back);
btn_back.setOnClickListener(mclick);
return rootView;
}
}
Notice :
This Fragment is a Fragment that drop on another Fragment .(Child Fragment)
Above code not work .
My xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Linear_BTNS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true" >
<com.gc.materialdesign.views.LayoutRipple
android:id="#+id/btn_back_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#1E88E5"
android:clickable="true" >
<ImageView
android:id="#+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:src="#drawable/ic_action_remove" />
</com.gc.materialdesign.views.LayoutRipple>
<com.gc.materialdesign.views.LayoutRipple
android:id="#+id/btn_save_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
android:clickable="true" >
<ImageView
android:id="#+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:src="#drawable/ic_action_save" />
</com.gc.materialdesign.views.LayoutRipple>
</LinearLayout>
<EditText
android:id="#+id/txtNote"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/Linear_BTNS"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="top|right"
android:inputType="textMultiLine"
android:background="#drawable/shape_border_note" >
</EditText>
</RelativeLayout>
Hmmm. ok . You are using from :
com.gc.materialdesign.views.LayoutRipple
in your xml , so you should do :
public class Note_Fragment extends Fragment{
com.gc.materialdesign.views.LayoutRipple btn_back_root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Note_Fragment()
{
super();
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.note_fragment, container, false);
btn_back_root = (com.gc.materialdesign.views.LayoutRipple) rootView.findViewById(R.id.btn_back_root);
btn_back_root.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("test", "YEH");
}
});
return rootView;
}
}
I have successfully set up the a FragmentActivity with FragementPagerAdapter associated with ViewPager to implement two tabbed application .
One of the Tabs namely "Wave" has a text view and a button . All I want is call textview.setText method via the onClick method of button described by its xml attribute .
I do not know where should I initialize my TextView or Button , how can I get the context of Wave tab and where should I write onclick method-
public class InformationShow extends FragmentActivity {
XMLdata dataObject;
ViewPager viewPager;
PagerAdapter adpt;
Fragment temp;
TextView tv;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
viewPager=(ViewPager)findViewById(R.id.pager);
adpt = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adpt);
// temp=adpt.fg.findFragmentById((int)adpt.getItemId(1));
tv=(TextView)findViewById(R.id.graphWaveTextView);
bt = (Button)findViewById(R.id.button1);
}
public void changeText(View v){
tv.setText("It worked ");
}
Adapter Class
public class PagerAdapter extends FragmentPagerAdapter {
int count = 2;
CharSequence namme[] = {"Temperature","Wave"};
XMLdata data;
FragmentManager fg;
public PagerAdapter(FragmentManager fragmentManager ){
super(fragmentManager);
this.fg = fragmentManager;
}
Context context;
#Override
public Fragment getItem(int arg0) {
switch (arg0){
case 0:{
TemperatureGraphFrag temp = new TemperatureGraphFrag();
return temp;
}
case 1:{WaveHeightGraphFrag wave = new WaveHeightGraphFrag();
return wave;
}
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return namme[position];
}
}
Fragments Class
public class TemperatureGraphFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.graph_t, container, false);
return view;
}
}
public class WaveHeightGraphFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.graph_sig_wave_height, container, false);
return view;
}
}
fragment_main XML implemented by FragmentActicity
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textColor="#65C2C9"
android:scrollbarSize="5dp"/>
</android.support.v4.view.ViewPager>
Tab 2 Fragment XML graph_sig_wave_height
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/graphWaveTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_gravity="center"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:onClick="changeText"/>
</LinearLayout>
Tab 1 fragment layout XML graph_t
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearTemp"
>
<TextView
android:id="#+id/graphTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center"
/>
</LinearLayout>
Add the following method to your WaveHeightGraphFrag class:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
final TextView t = (TextView) view.findViewById(R.id.graphWaveTextView);
Button b = (Button) view.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
t.setText("It worked ");
}
});
}
This is what you want.
If I try to inflate a view within a fragment I am getting NULL.. For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Here I will inflate my view using the layout ID & return.
return view;
}
Whenever a button is clicked I need to create a dynamic view e.g.: button & add to the LinearLayout. I would like to perform this operation inside my fragment class like this:
public void addPlaces() {
Button button = new Button(null);
button.setText("button name");
// e.g. like adding button to enter code here linear layout
linearLayout.addView(button);
}
So, if I get inflate LinearLayout inside onCreateView and use it in add class, I'm getting NULL. How to achieve?
Declare the variable as a instance variable and then initialize Linear Layout
LinearLayout linearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
return rootView;
}
Then
public void addPlaces() {
Button button = new Button(getActivity());
// needs activity context
// fragment hosted by a activity. use getActivity() to get the context of the hosting activity.
button.setText("button name");
linearlayout.addView(button);
}
Example: Modify the below according to your requirement.
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearlayout"
android:layout_height="fill_parent"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPlaces();
}
});
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}
Snap shot of my emulator
Edit :
activity-main.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"
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=".MainActivity" >
<fragment android:name="com.example.fragments.Myfragment"
android:id="#+id/frag"
android:layout_above="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
Button b;
Myfragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new Myfragment();
fragmentTransaction.add(R.id.frag, fragment);
fragmentTransaction.commit();
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
fragment.addPlaces();
}
});
}
}
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}