Access fragment controls from FragmentActivity - android

I have a FragmentActivity that should hold 2 Fragments. Each fragment has it's own layout with a table in it. I need to access the tables from the fragments' layouts from within the FragmentActivity to put some data that I read from an XML file. My problem is that when I use
tableOrders = (TableLayout)findViewById(R.id.tabel1);
it returns null. Now, I know that items from layouts can only be accessed before they have been inflated, and I've done that in the FragmentActivity before trying to call findViewById(). Here is the code for what I've tried:
The onCreate of the FragmentHolder.java that is derived from FragmentActivity:
private TableLayout tableOrders = null;
private TableLayout tableExecutions = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_holder);
initialisePaging();
//View orderFragment = (View)findViewById(R.layout.fragment_orders);
tableOrders = (TableLayout)/*orderFragment.*/findViewById(R.id.tabel1);
//View execFragment = (View)findViewById(R.layout.executions_fragment);
tableExecutions = (TableLayout)/*execFragment.*/findViewById(R.id.tabel2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null)
logedUser = (User) bundle.getSerializable("user");
AssetManager am = getApplicationContext().getAssets();
String xmlContentOrders = XMLfunctions.getXml(am, "ordersData.xml");
populateOrdersTable(xmlContentOrders);
String xmlContentExecutions = XMLfunctions.getXml(am, "executionsData.xml");
populateExecutionsTable(xmlContentExecutions);
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
And the .xml file for the fragment:
<?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" >
<TableLayout
android:id="#+id/tabel1header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor" >
<TableRow >
<TextView
android:id="#+id/ordersHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="#string/orders"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/ordersTable"
style="#style/HeaderRow" >
<TextView
android:id="#+id/textSymbol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/symbol"
style="#style/HeaderText" />
<TextView
android:id="#+id/textSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/side"
style="#style/HeaderText" />
<TextView
android:id="#+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/price"
style="#style/HeaderText" />
<TextView
android:id="#+id/textQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/quantity"
style="#style/HeaderText"/>
<TextView
android:id="#+id/textRemaining"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/remanent"
style="#style/HeaderText" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="#+id/tabel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor">
</TableLayout>
</ScrollView>
</LinearLayout>
With that said hope someone has a solution for this (probably it's something easy that I missed).
Edit: The code I use to put the Fragments on the FragmentActivity:
private void initialisePaging() {
OrdersFragment fragmentOne = new OrdersFragment();
ExecutionsFragment fragmentTwo= new ExecutionsFragment();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentTwo);
ViewPager viewPager = (ViewPager) super.findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
}

Related

How to use CircleImageView library ( this : 'de.hdodenhof:circleimageview:3.1.0' ) inside fragment?

This is the error I'm gettingI'm trying to use this library in fragment inside onViewCreated() method, but i'm getting this casting error: - java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView cannot be cast to de.hdodenhof.circleimageview.CircleImageView. I don't know how to solve this error. can anybody please help me?
FragmentProfile.java :
public class FragmentProfile extends Fragment implements View.OnClickListener {
Button sign_out;
TextView user_number;
private CircleImageView userImage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sign_out = getActivity().findViewById(R.id.user_logout);
userImage = getActivity().findViewById(R.id.user_display_name);
user_number = getActivity().findViewById(R.id.phone_number);
sign_out.setOnClickListener(this);
}
#Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getActivity(), Login.class));
getActivity().finish();
}
}
fragment_profile.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">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/dp_180"
android:scaleType="fitXY"
android:src="#drawable/money" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="#dimen/dp_95"
android:layout_height="#dimen/dp_95"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_140"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher"
app:civ_border_color="#color/textColor"
app:civ_border_width="#dimen/dp_2" />
<TextView
android:id="#+id/user_display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_4"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="#string/username"
android:textColor="#color/border"
android:textSize="#dimen/sp_22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/user_logout"
android:layout_below="#id/user_display_name"
android:layout_margin="#dimen/dp_12"
android:orientation="vertical">
<TextView
android:id="#+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp_12"
android:layout_marginTop="#dimen/dp_30"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="+91-987654321"
android:textColor="#android:color/black"
android:textSize="#dimen/dp_18" />
</LinearLayout>
<Button
android:id="#+id/user_logout"
android:layout_width="#dimen/dp_200"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/dp_30"
android:background="#drawable/button_shape"
android:text="#string/sign_out"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textStyle="bold" />
</RelativeLayout>
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// ... rest of the codes.
// Comment out code below.
// userImage = getActivity().findViewById(R.id.user_display_name);
// Replace it with code below.
userImage = getActivity().findViewById(R.id.profile_image);
// ... rest of the codes.
}
Here you are parsing a textview id to the CircleImageView. You should change your userImage = getActivity().findViewById(R.id.user_display_name); to userImage = getActivity().findViewById(R.id.profile_image);. That's why you are getting java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView exception.
You issue is here:
private CircleImageView userImage;
userImage = getActivity().findViewById(R.id.user_display_name);
and in your layout:
<TextView
android:id="#+id/user_display_name"
You can not cast a TextView into a CircleImageView.

Why is my TextView not centerInParent in my ViewPager fragment?

My TextView has layout_centerInParent="true" and looks good in the layout preview in Android Studio, but when I compile and run the "Let's Get Started" text ends up near the bottom as shown below. Why is this happening?
Fragment 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"
android:id="#+id/intro_background"
android:background="#color/primary">
<TextView
android:id="#+id/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxWidth="240sp"
android:gravity="center"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#color/white"
android:text="Let\'s Get Started!"
/>
<Button
android:id="#+id/btn_add_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_not_now"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#drawable/btn_primary_with_white"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="10dp"
android:text="Add My First Course"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/tv_not_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:maxWidth="280sp"
android:layout_gravity="center_horizontal"
android:gravity="left"
android:textStyle="italic"
android:textSize="16sp"
android:textColor="#color/white"
android:text="#string/intro_not_now"
/>
</RelativeLayout>
Activity 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/nav_header1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="3dp"
android:layout_marginTop="-25dp"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
Activity:
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new Adapter_Intro(getSupportFragmentManager()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
}
Fragment:
public class IntroFragment extends Fragment {
private static final String BACKGROUND_COLOR = "backgroundColor";
private static final String PAGE = "page";
private int mBackgroundColor, mPage;
public static IntroFragment newInstance(int backgroundColor, int page) {
IntroFragment frag = new IntroFragment();
Bundle b = new Bundle();
b.putInt(BACKGROUND_COLOR, backgroundColor);
b.putInt(PAGE, page);
frag.setArguments(b);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!getArguments().containsKey(BACKGROUND_COLOR))
throw new RuntimeException("Fragment must contain a \"" + BACKGROUND_COLOR + "\" argument!");
mBackgroundColor = getArguments().getInt(BACKGROUND_COLOR);
if (!getArguments().containsKey(PAGE))
throw new RuntimeException("Fragment must contain a \"" + PAGE + "\" argument!");
mPage = getArguments().getInt(PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Select a layout based on the current page
int layoutResId;
switch (mPage) {
case 0:
layoutResId = R.layout.fragment_intro_welcome;
break;
default:
layoutResId = R.layout.fragment_intro_start;
}
// Inflate the layout resource file
View view = getActivity().getLayoutInflater().inflate(layoutResId, container, false);
// Set the current page index as the View's tag (useful in the PageTransformer)
view.setTag(mPage);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set the background color of the root view to the color specified in newInstance()
View background = view.findViewById(R.id.intro_background);
background.setBackgroundColor(mBackgroundColor);
}
}
It seems that Android Studio is not accurate while rendering your layout.
I "measured" your screenshot using Gimp - as you can see, TextView is positioned exactly to the center of it parent, between top picture and bottom:
Your TextView is well centered inside the fragment, but not inside the activity.
Because in your activity, there is the ImageView that takes some height in the top.

Android - Call Method With A Button From XML

I've got an app based on Sliding Tabs layout. I want to call the addMedicine method in the PageFragment class from an XML layout with a Button. The issue is that i cannot assign the method to the Button onClick event.
Here's the PageFragment class:
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
// TEMP VARIABLES //
ListView ItemsLst;
String[] Items = {"Medicine 1", "Medicine 2", "Medicine 3"};
TextView output;
EditText nameFld;
EditText formatFld;
EditText amountFld;
EditText exp_dateFld;
EditText timeFld;
DBManager dbAdapt = new DBManager(getActivity());
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
switch(mPage)
{
case 1:
view = inflater.inflate(R.layout.layout_sqltest, container, false);
nameFld = (EditText) view.findViewById(R.id.nameFld);
formatFld = (EditText) view.findViewById(R.id.formatFld);
amountFld = (EditText) view.findViewById(R.id.amountFld);
exp_dateFld = (EditText) view.findViewById(R.id.exp_dateFld);
timeFld = (EditText) view.findViewById(R.id.timeFld);
return view;
case 2:
view = inflater.inflate(R.layout.layout_settings, container, false);
return view;
case 3:
view = inflater.inflate(R.layout.layout_info, container, false);
return view;
}
return view;
}
public void addMedicine() // The method i want to call
{
String name = nameFld.getText().toString();
String format = formatFld.getText().toString();
int amount = Integer.parseInt(amountFld.getText().toString());
String exp_date = exp_dateFld.getText().toString();
String time = timeFld.getText().toString();
long id = dbAdapt.addMedicine(name, format, amount, exp_date, time);
}
}
And here is the layout_sqltest 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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/nameLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nameFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Format"
android:id="#+id/formatLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/formatFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Amount"
android:id="#+id/amountLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/amountFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Expiration Date"
android:id="#+id/exp_dateLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/exp_dateFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time"
android:id="#+id/timeLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/timeFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Medicine"
android:id="#+id/addMedicineBtn"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The addMedicineBtn is the button i want to call the method with.
Someone can help me? I really need that!
Thank you in advance!
EDIT: I tried giving to addMedicine method the View param, but it still doesn't appear in the available methods for the addMedicineBtn in the onClick properties (I'm using Android Studio).
Try to use onClickListener
view.findViewById(R.id.addMedicineBtn).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addMedicine();
}
});
You must add tools:context="PageFragment" line to the container (LinearLayout) of PagerFragment's layout file (layout_sqltest). Then onClick() method will find methods in PagerFragment class.

ImageButton onClick not called in RelativeLayout in a Fragment using a transparent ActionBar

any ideas why my ImageButton isn't working in my Fragment?
The ImageButton contains a Drawable and is transparent.
When I place it in my RelativeLayout below, its working...
<RelativeLayout
android:id="#+id/rl_newsdet_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bg"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_newsdet_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_marginTop="7dp"
android:textSize="20sp"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:id="#+id/btn_newsdet_back"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/bt_back"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#color/white"
android:gravity="left"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_newsdet_name"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textColor="#color/cyan"
android:background="#color/grey"
android:paddingLeft="1dp"
android:paddingRight="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_newsdet_time"
android:textColor="#color/white"
android:textSize="18sp"
android:layout_toRightOf="#+id/tv_newsdet_name"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:background="#color/grey"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_newsdet_text"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_below="#+id/tv_newsdet_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
My onClickListener with AndroidAnnotations
#ViewById
ImageButton btn_newsdet_back;
#Click(R.id.btn_newsdet_back)
void back() {
Log.i(TAG,"back");
mActivity.popFragments();
}
EDIT
onClick is working (called), when its below the TextView. Can anybody explain why?
If, as you said, everything works when you put the button inside the RelativeLayout below the reason should be that the second RelativeLayout has its parameters declared as:
android:layout_width="match_parent"
android:layout_height="match_parent"
Which means it is "filling" its parent, that is the original RelativeLayout containing the button. It's like you're creating a layer that covers the button and keeps any interaction with it from working. When you click on the button, you're actually clicking on the second RelativeLayout.
I didn't actually tried this, but you should consider setting:
android:layout_below="#id/btn_newsdet_back"
On the RelativeLayout below the button.
You can try this code:
This is code of Fragment: TestFragment.java
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_one, container, false);
ImageButton button = (ImageButton) view.findViewById(R.id.img2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Click on fragment ",
Toast.LENGTH_LONG).show();
}
});
return view;
}
}
This is layout of 'TestFragment`
<?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" >
<ImageButton
android:id="#+id/img2"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
This is code of MainActivity.java
public class MainActivity extends FragmentActivity {
ImageButton imgButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
FragmentManager fragmentManager = getSupportFragmentManager();
TestFragment fragment = (TestFragment) fragmentManager
.findFragmentById(R.id.fragment);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
// fragmentTransaction.add(fragment, "");
fragmentTransaction.commit();
}
}
This is layout of MainActivity
<?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" >
<fragment
android:id="#+id/fragment"
android:name="com.alarmdemo.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Here ImageButton is placed in a Fragment and it is working fine.
No need to add clickable in image button .Try following:
<ImageButton
android:id="#+id/btn_newsdet_back"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mymethod"
android:src="#drawable/bt_back"
/>
add in java
public void mymethod(View v)
{
//your method
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sample,
container, false);
addListenerOnButton();
}
private void addListenerOnButton() {
imageButton = (ImageButton) rootview.findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
I solved it.
The problem was I was using a transparent Actionbar which was the overlay I was looking for.
The following line in my onCreate() solved this:
getActivity().getActionBar().hide();

use fragment_container in replace transition

I'm trying to transit between 2 fragment from games.xml to levels.xml
its like angry bird game menu:
games: http://i.stack.imgur.com/9S7Ar.png
levels:http://i.stack.imgur.com/uB8rP.jpg
games.java:
public class Games extends Fragment implements OnClickListener{
public Games(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.games, container, false);
return rootView;
}
#Override
public void onClick(View v) {
Fragment f = null;
switch (v.getId()) {
case R.id.s4:{
f = new Levels();
break;
default:
break;
}
if (f != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.lvl_container, f);
transaction.addToBackStack(null);
transaction.commit();
}
}
level.java:
public class Levels extends Fragment {
public Levels(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.levels, container, false);
return rootView;
}
}
levels.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lvl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/s4"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLabel"
android:src="#drawable/lvlgray_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
games.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">
<TextView
android:id="#+id/lastgame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/games"/>
<ImageButton
android:id="#+id/s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lastgame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="#drawable/s4" />
the problem is when I click on imagebotton nothing happens (should go to levels), I think there is problem with these line
transaction.replace(R.id.lvl_container, f);
but I don't know what is it. Any ideas?
I've already seen this (developer.android.com/guide/components/fragments) but no help! What did I miss?
Is there any other way to transition between 2 fragments?
Thank you
Add this line in onCreateView at Games.java:
ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
im.setOnClickListener(this);
This make refrence to your imagebutton and set onClickListener on it to call your onClick method.

Categories

Resources