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.
Related
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));
}
});
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);
Aim:
Create a activity that have two buttons, button 1 and button 2. When click, the fragment will alternate between the two fragments.
Background:
Fragmentone will take the edittext from the main activity and settext on the textview on the fragment.
Fragmenttwo will take display text to the tablelayout. (My ultimate goal is to use the input from the main activty and display it to one of the textview of the table, but it is not working, kindly advise on that)##issue 1
Problems:
The button1 that is supposed to display fragmentone is not working. After keying in the input into the edittext on the mainactivity, when i click the button, the fragmenttwo persist. FragmentOne did not come out. Place an toast in fragmentone and the toast display my input.
Special Thanks to:
Would like to extend a special thanks to helpful people in this community for guiding me this far. And will like to extend my thanks to Raghunandan who guide me through fragment advance I, II. Thanks you very much for assisting me through my learning journey. Hope my questions and raghunandan advice would help reduce the learning curve for those embraking on this journey.
Fragment Intermediate (I)
Fragment Intermediate(I):Getting input from edittext, set text in textview of a fragment
Fragment Intermediate (II)
Fragment Intermediate (II):Dynmically adding row in a fragment, Android
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}
else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
activity_main.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" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dis_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input" />
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow
android:id="#+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Button2" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Calculate" />
</TableRow>
</TableLayout>
</FrameLayout>
<fragment
android:name="com.example.sample.FragmentTwo"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
FragmentOne.java
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_one, container, false);
TextView warcraft= (TextView) view.findViewById(R.id.moargold);
EditText moargold = (EditText) getActivity().findViewById(R.id.input);
Double vespenegas = Double.parseDouble(moargold.getText().toString());
warcraft.setText(new Double(vespenegas).toString());
Toast toast = Toast.makeText(getActivity(),new Double(vespenegas).toString() , Toast.LENGTH_SHORT);
toast.show();
return view;
}
}
fragment_one.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" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dis_moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gold Count:" />
<TextView
android:id="#+id/moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
FragmentTwo.java
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_two, container, false);
EditText input = (EditText) getActivity().findViewById(R.id.input);
TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(getActivity());
textview1.setText("happy");
tr.addView(textview1);
TextView textview2 = new TextView(getActivity());
textview2.setText("unhappy");
//###############To insert text from editview to table
// Double buygas = Double.parseDouble(input.getText().toString());
// textview2.setText(new Double(buygas).toString());
tr.addView(textview2);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
return view;
}
}
fragment_two.xml
<?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"
android:background="#ffff00">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLayout">
<TableRow
android:id="#+id/infoRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="First"
android:id="#+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="Second"
android:id="#+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</TableRow>
</TableLayout>
</LinearLayout>
Just have the FrameLayout and EditText in activity_main.xml. FrameLayout is a container (ViewGroup) to which you add or replace fragments. Your TableLayout can be in fragment layout.
You are adding/replacing fragment programatically so what is the need to have the same in xml.
Have this in activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<FrameLayout
android:id="#+id/container" // id is container
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:layout_below="#+id/input"
>
</FrameLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/input"
android:onClick="selectFrag"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:onClick="selectFrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/input"
android:layout_alignParentBottom="true"
android:text="Button2" />
</RelativeLayout>
Then in MainActivity
public class MainActivity extends Activity {
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}
else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.commit();
}
}
FragmentOne
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.frag1, container, false);
TextView warcraft= (TextView) view.findViewById(R.id.moargold);
EditText moargold = (EditText) getActivity().findViewById(R.id.input);
Double vespenegas = Double.parseDouble(moargold.getText().toString());
warcraft.setText(String.valueOf(vespenegas));
Toast toast = Toast.makeText(getActivity(),String.valueOf(vespenegas) , Toast.LENGTH_SHORT);
toast.show();
return view;
}
}
frag1.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" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dis_moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gold Count:" />
<TextView
android:id="#+id/moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
FragmentTwo
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.frag2, container, false);
EditText input = (EditText) getActivity().findViewById(R.id.input);
TableLayout tl=(TableLayout) view.findViewById(R.id.TableLayout01);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(getActivity());
textview1.setText("happy");
tr.addView(textview1);
TextView textview2 = new TextView(getActivity());
textview2.setText("unhappy");
//###############To insert text from editview to table
// Double buygas = Double.parseDouble(input.getText().toString());
// textview2.setText(new Double(buygas).toString());
tr.addView(textview2);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
return view;
}
}
frag2.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" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dis_moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gold Count:" />
<TextView
android:id="#+id/moargold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
Snap
While you add your Fragments dynamically, you keep your FragmentTwo inside your layout. Try to remove this in your layout:
<fragment
android:name="com.example.sample.FragmentTwo"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Also, in your code, you add dynamically a Fragment and with replace method, you set the container as id fragment_place:
fragmentTransaction.replace(R.id.fragment_place, fr);
However, this id is using by your fragment (FragmentTwo, the first piece of code above) and you cannot replace a fragment which is not added dynamically. You need to host your fragment inside a FrameLayout but yours has no id, try to add this:
<FrameLayout
android:id="#+id/fragment_place" /// this line to create and id
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
Then, you will be able to host, add and replace your fragments into your framelayout as you want.
I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout 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" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>