Change a particuar view in Fragment - android

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);

Related

How can i add this popup.xml to this fragment?

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

Fragment Intermediate(III): Creating a activity that alternate between fragments onclick of respective button

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.

Button at end of ActionBar powered LinearLayout with ListView seems to be overriden by android navigation buttons

I've been struggling like crazy to add a button below a ListView of a ListFragment, I've followed most suggestions of the questions How to show a button at the end of an Android ListView, Android Layout with ListView and Buttons and Displaying the button at the end of the ListView with Linear Layout. I just havent tried yet the ListView#addFooterView() yet as I would prefer not to add the button programmatically.
I have the impression that the reason why theirs solutions work for them and not for me is due to the fact that my layout refers to a fragment layout under an activity that has a tab action bar.
As you can see in the picture, the shadow of the linear layout in which my button is part of can be seen overridden by the navigation bar. And the amount overridden resembles the height of the tabs
My fragment layout is the one below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/service_notif_list_fragment"
>
<TextView
android:id="#+id/serviceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2.0"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No data"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="#android:style/ButtonBar">
<Button
android:id="#+id/delServiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|left"
android:text="Delete Service" />
</LinearLayout>
</LinearLayout>
And my fragment code does not do anything exceptional, except using Loaders to load the list data from the db
public class ServiceSpecifNotListFragment extends ListFragment implements
LoaderCallbacks<Cursor> {
Button deleteServiceButton = null;
String serviceUri;
String servName;
TextView serviceName = null;
private NotificationCursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] uiBindFrom = { ...};
int[] uiBindTo = { ... };
getLoaderManager().initLoader(Constants.SERVICE_SPECIFIC_LIST_LOADER, null, this);
adapter = new NotificationCursorAdapter(
getActivity().getApplicationContext(), R.layout.service_specifc_notif_row,
null, uiBindFrom, uiBindTo,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.service_notif_list_fragment, container, false);
serviceName = (TextView) rootView.findViewById(R.id.serviceName);
serviceName.setText(servName); // the name is set in the onCreateLoader
deleteServiceButton = (Button) rootView.findViewById(R.id.delServiceButton);
deleteServiceButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO
}
});
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle bundleArg) {
String[] projection = { ...};
Bundle arg = this.getArguments();
CursorLoader cursorLoader = new CursorLoader(getActivity(),
NotificationContentProvider.CONTENT_URI, projection, selection, selectionArgs, NotificationData.SERVER_TIME + " DESC");
return cursorLoader;
}
else{
Log.d(TAG, "failed to retrieve service name");
return null;
//TODO: find a destroy self
}
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
#Override
public void onDestroyView(){
super.onDestroyView();
Log.d(TAG, "on destroy view");
}
#Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG, "on destroy");
}
}
If needed, I can post the code of the main activity, and on...
Try a RelativeLayout, where you can set the ListView to fill the space between the TextView and LinearLayout containing the button(s):
<?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"
android:id="#+id/service_notif_list_fragment" >
<TextView
android:id="#+id/serviceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/layoutButton"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="#android:style/ButtonBar">
<Button
android:id="#+id/delServiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|left"
android:text="Delete Service" />
</LinearLayout>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/layoutButton"
android:layout_below="#id/serviceName"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
Finally what worked here was to set different layout weights for the different elements in the linear layout, but having a weightSum on the top layout as shown below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/service_notif_list_fragment"
android:weightSum="1.0">
<TextView
android:id="#+id/serviceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No data"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/transparent"
style="#android:style/ButtonBar">
<Button
android:id="#+id/delServiceButton"
android:layout_weight="0.05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|left"
android:text="Delete Service" />
</LinearLayout>
</LinearLayout>

binary xml file line #7 error inflating class Button

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.

problem in TextView in android

hi all i m making a sample app. for update List.
i have a list class and its xml is like.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent">
</TextView>
</LinearLayout>
now i have set TouchListener of text View and added the following code in it
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId())
{
case R.id.more:
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return false;
}
You see on the 3rd line of switch statement i have added
more.setText("Click to view More..");
line but when my list is updated . The text View is no longer shows in the bottom .
Please Guide my why this is happening to me and whats the solution??
you can use list footer in that case.
add this code in ur java file. and add footer dynamically in your list.
TextView more = new TextView(this);
more.setText("Click to view more...");
more.setClickable(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, Height);
more.setLayoutParams(params);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
});
ListView listView = (ListView) findViewById(R.id.ListView01);
listView.addFooterView(more);
this can help you.
try this ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_height="fill_parent" android:layout_above="#+id/more"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true">
</TextView>
</RelativeLayout>
try android:layout_height="fill_parent" android:layout_above="#+id/more" in the ListView. Now even if ur ListView grows it wont hide the TextView.
UPDATE
<RelativeLayout android:id="#+id/relativeList"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearBottom3">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/chathlist"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/LinearBottom3"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"><!--Make it as center-->
<TextView android:id="#+id/more" android:layout_height="wrap_content"
android:text="Click to view more..." android:textStyle="normal|bold" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"/>
</ReletiveLayout>
try this..
footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:textSize="30dip" android:gravity="center_horizontal"
android:text="Click to view More.." android:layout_width="fill_parent"
android:id="#+id/more"></TextView>
</LinearLayout>
in class file
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout footer = (LinearLayout)inflater.inflate(
R.layout.footer, null);
TextView more= (TextView)footer.findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//List Update Code
}
});
use the following code..
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
and use the following in code:
public class listactivty extends ListActivity{
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
use seperate XML file for ListView..
Thanks
Shahjahan Ahmad

Categories

Resources