This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns NULL when using Fragment
(7 answers)
Closed 1 year ago.
Actually I saw a yt video of how to intent fragment and acticity and that is what I tried but don"t know how the error is coming.The error which I am having is ** java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.photoframeapp.HomeFragment.onCreateView(HomeFragment.java:31)**.I tried to sort out but I am not able to .
this is my HomeFragment.java class
public class HomeFragment extends Fragment {
TextView beginners;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});
return view;
}
}
this is my mainpage.java file
public class mainpage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
}
}
fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<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=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="387dp"
android:alpha="0.4"
android:background="#color/Purple" />
<TextView
android:id="#+id/beginners"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-27dp"
android:layout_marginBottom="433dp"
android:freezesText="true"
android:text=" BEGINNERS CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="195dp"
android:alpha="0.4"
android:background="#color/DeepPink" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-9dp"
android:layout_marginBottom="67dp"
android:text=" ADVANCED CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:alpha="0.4"
android:background="#color/DarkBlue" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-8dp"
android:layout_marginBottom="261dp"
android:freezesText="true"
android:text=" INTERMEDIATE CALLIGRAPHY"
android:textSize="25dp" />
</RelativeLayout>
You have to initialize textView before using it.
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners = view.findViewById(R.id.beginners)
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});
Related
I want to change an textView's text via an Activity, but when I change by this code:
public class MainActivity extends AppCompatActivity {
private TextView txtSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSave = (TextView)findViewById(R.id.txt_save);;
}
public void onClick(View v) {
TextView txtSave;
txtSave = (TextView)findViewById(R.id.txt_save);
txtSave.setText("something");
}
}
and I got this error:
05-14 13:47:46.835 21949-21949/stv.wordspower E/AndroidRuntime: FATAL EXCEPTION: main
Process: stv.wordspower, PID: 21949
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Can somebody add me help how can I change dynamically?
Fragment code:
public class ProgressFragment extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_progress, container, false);
return rootView;
}
public static ProgressFragment newInstance() {
Bundle args = new Bundle();
ProgressFragment fragment = new ProgressFragment();
fragment.setArguments(args);
return fragment;
}
}
The textView is on the ProgressFragment. The ProgressFragment is on the MainActivity's FrameLayout.
I just want to modify the TextView's values but I can't. I have tried these solutions, but one did not work.
other informations:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="stv.wordspower.MainActivity">
<TextView
android:text="#string/txtView_welcome"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<Button
android:text="#string/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn_exit"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_settings"
android:layout_above="#+id/btn_contact"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_contact"
android:layout_above="#+id/btn_exit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:text="#string/btn_progress" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_progress"
android:layout_above="#+id/btn_settings"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/mainActivityFragmentLayout">
</FrameLayout>
<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"
tools:context="stv.wordspower.ProgressFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/txt_progresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/txt_progresses"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/txt_empty"
android:textAlignment="center"
android:textSize="30dp"
android:onClick="onClick"
android:clickable="true"/>
In onClick method, you can get fragment as following:
public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
}
and in fragment, create a method to update the textView, so that we can call it using fragment object as following:
public void update(String s){
textView.setText(s);
}
Note: This textView should be defined in your fragment.
and call this from main activity as following:
public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragment.update("foo");
}
And yes! if you are not using support library, just replace getSupportFragmentManager() by getFragmentManager().
Hello i want to add a popup window to a fragment i tried a lot of things still nothing i have. If someone helps me it will be great.
Here is my Fragment code;
public class Info extends Fragment {
public Info() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info, container, false);
}
Here is my fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:src="#drawable/pozitif"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:text="dfdfdsfs"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
You can use a DialogFragment.
Instead of extending your Activity with Fragment, use DialogFragment
In on createView of the DailogFragment Activity inflate your layout
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Find your View elements
TextView Message = (TextView) v.findViewById(R.id.text_view1);
To call your DailogFragment use this
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
For more info on this you can check out this link.
https://developer.android.com/reference/android/app/DialogFragment.html
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();
I am new to android developing and I am currently stumped at this situation.
Is there a way to change a particular child view of a fragment?
For example, I have a TextView and Button in a Fragment. Now after inflating this fragment and committing it, I would like to only change the text of the TextView based on the user input.
EDIT: The same layout is used to create multiple fragments. I would like to just change the TextView of a particular Fragment.
MainActivity Code:
public class MainActivity extends Activity {
Button bt;
EditText et;
LinearLayout ly;
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.badd);
ly=(LinearLayout) findViewById(R.id.lay_ver);
et=(EditText) findViewById(R.id.etadd);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// multiple fragments are created
MyFragment f=new MyFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction t=fm.beginTransaction();
t.add(R.id.sv1, f, "hi");
t.commit();
et.setText("");
}
});
//would like to change the text of the textview of the fragment
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Fragment Code:
public class MyFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.frag_lay, null);
return v;
}
}
activity_main XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lay_ver"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/lay_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/etadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:hint="Enter Subject"
android:paddingTop="20dp" />
<Button
android:id="#+id/badd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Add" />
</LinearLayout>
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/sv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.53"
android:text="HiHow"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
You can add a list of Fragments in your activity and each fragment to the list when you create them. Then you can get the fragment you want to change and do as cdblades said :
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);
But why are you adding the fragments in the same container?
The base View class has the findById() method that you can use to get a reference to child views like so:
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);
I write a program with FrameLayout, when i clicked on the first button, the program show introduce.xml but when i clicked on the second button, the program Force Class. i think because i use Button in product.xml file because when i changed button to imageView, the program runs good. Why? How can solve this?
Runtime Error:
binary xml file line #7 error inflating class Button in android
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:id="#+id/logoText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/dark_blue"
android:text="#string/logotxt"/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:layout_gravity="bottom">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_weight="1"/>
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b1"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="#string/btn1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b2"
android:layout_toRightOf="#id/btn1"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn2"
/>
<Button android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b3"
android:layout_toRightOf="#id/btn2"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn3"
/>
<Button android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b4"
android:layout_toRightOf="#id/btn3"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn4"
/>
</RelativeLayout>
</LinearLayout>
product.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Buton
android:id="#+id/android_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/red"
android:text="#string/android"/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity {
public String fonts="BZar.ttf";
TextView logoText;
Button btnIntroduce;
Button btnProduct;
Button btnContact;
Button btnMore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setFont();
btnIntroduce.setOnClickListener(onClickListener);
btnProduct.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
switch(v.getId()){
case R.id.btn4:
Log.e("button", "4click");
//ft.add(R.id.frameLayout, new Introduce());
ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
ft.replace(R.id.frameLayout, new Introduce());
ft.commit();
break;
case R.id.btn3:
Log.e("button", "3Click");
ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
ft.replace(R.id.frameLayout, new Product());
ft.commit();
break;
}
}
};
Product.java:
public class Product extends Fragment{
public String fonts="BZar.ttf";
Typeface face;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.product, container, false);
face=Typeface.createFromAsset(getActivity().getAssets(), "font/"+fonts+"");
/*Button btnAndroid=(Button)view.findViewById(R.id.android_btn);
btnAndroid.setTypeface(face);
String android=(String)btnAndroid.getText().toString();
btnAndroid.setText(PersianReshape.reshape(android));*/
return view;
}
}
Its a typo. product.xml only has 1 T in Button.