Menu is not showing in custom toolbar in fragments - android

I have created a custom toolbar for the fragment that I have created and it is supposed to look like this:
The toolbar shown on the left side is from my fragment_purchase_item_list and the right side is for my fragment_purchase_list.
I have tried checking some other posts from here which I followed but it will not show up the menu that I have created.
When I run the code, it will look like this:
The search icon (left), scan and new icon (right) will not show up. Here are my codes:
PurchaseItemListFragment.java
package com.example.devcash.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.devcash.R;
/**
* A simple {#link Fragment} subclass.
*/
public class PurchaseItemListFragment extends Fragment implements SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
Toolbar itemListToolbar;
Spinner itemListSpinner;
public PurchaseItemListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate the menu
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_purchase_item_list, container, false);
itemListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaseitemlist);
itemListSpinner = (Spinner) view.findViewById(R.id.spinner_allcategories);
///
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.custom_spinner_item,
getResources().getStringArray(R.array.dropdownitempurchase));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemListSpinner.setAdapter(myAdapter);
itemListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),
itemListSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
//handles the search menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.searchmenu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("Search..");
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
}
PurchaseListFragment.java
package com.example.devcash.Fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.devcash.R;
/**
* A simple {#link Fragment} subclass.
*/
public class PurchaseListFragment extends Fragment {
Toolbar purchaseListToolbar;
Spinner purchaseListSpinner;
public PurchaseListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_purchase_list, container, false);
setHasOptionsMenu(true);
purchaseListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaselist);
purchaseListSpinner = (Spinner) view.findViewById(R.id.spinner_customertype);
///
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.customer_type));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
purchaseListSpinner.setAdapter(myAdapter);
purchaseListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),
purchaseListSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//set adapter
//set click listener
//show no data found text when listview is empty
// button click listener
Button btnpay = view.findViewById(R.id.btn_paypurchasetransaction);
btnpay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment chargeFragment = new ChargeFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.customersales_content, chargeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.purchaselist_menu, menu);
}
}
fragment_purchase_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.PurchaseItemListFragment"
android:orientation="vertical"
android:gravity="center_horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_purchaseitemlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
style="#style/PrimaryHeaderBar"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner_allcategories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_cry_face"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_items"
android:gravity="center"
android:layout_marginTop="#dimen/text_padding"/>
<Button
android:layout_width="150dp"
android:layout_height="30dp"
android:text="#string/go_inventory"
android:layout_gravity="center"
android:layout_marginTop="#dimen/text_padding"
android:background="#color/colorPrimary"
android:textColor="#color/whiteBG"
android:textSize="#dimen/small_title"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
fragment_purchase_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.PurchaseListFragment"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_purchaselist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Light"
style="#style/HeaderBar"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner_customertype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#color/light_gray"
android:padding="#dimen/text_padding"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_delete"
android:tint="#color/dark_text"
android:layout_gravity="left"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item_count_title"
android:gravity="center"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/opencurly"
android:paddingLeft="#dimen/text_inputs"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/count"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/closecurly"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical"
android:gravity="bottom"
android:padding="#dimen/text_padding">
<Button
android:id="#+id/btn_paypurchasetransaction"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorPrimary"
android:text="#string/charge"
android:textColor="#color/whiteBG"/>
</LinearLayout>
</LinearLayout>
purchaselist_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_scan"
android:title="#string/scan_label"
android:icon="#drawable/ic_qr_code_scan"
app:showAsAction="always"
android:actionLayout="#layout/custom_purchaselist_layout"/>
<item
android:id="#+id/action_new"
android:title="#string/new_label"
android:icon="#drawable/ic_add"
app:showAsAction="always"
android:actionLayout="#layout/custom_purchaselist_add_layout"/>
</menu>
searchmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:title="#string/search_title"
android:orderInCategory="2"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
</menu>

Related

ViewPager2 main activit toolabar is not working

i want to press my custom toolbar button in view activity but i can't do it
how can i bring my custom toolbar on ViewActivity?
pls help me
here is my activity_view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:elevation="0dp"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/alarmToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center"
app:contentInsetStart="0dp"
app:menu="#menu/alarm_title">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="미운 오리 새끼"
android:fontFamily="#font/applesdgothicneoeb"
android:textColor="#color/black"
android:textSize="25sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
here is my ViewActivity
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import java.util.ArrayList;
import java.util.List;
public class ViewActivity extends AppCompatActivity {
private ViewPager2 pager;
private FragmentStateAdapter pagerAdapter;
private ZoomOutPageTransformer pageTransformer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
Toolbar toolbar = findViewById(R.id.alarmToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
List<Fragment> fragments = new ArrayList<>();
fragments.add(new Duck1Fragment());
fragments.add(new Duck2Fragment());
fragments.add(new Duck3Fragment());
pager = findViewById(R.id.pager);
pagerAdapter = new PagerAdapter(this, fragments);
pager.setAdapter(pagerAdapter);
pager.setPageTransformer(pageTransformer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.alarm_title, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.close:
finish();
return true;
default:
return super .onOptionsItemSelected(item);
}
}
}
here is my Duck1Activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="70dp"
android:src="#drawable/tale_character"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="150dp"
android:background="#drawable/background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="161dp"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/ducktale_image"
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_gravity="center"
tools:srcCompat="#tools:sample/avatars" />
</FrameLayout>
<FrameLayout
android:layout_width="300dp"
android:layout_height="175dp"
android:layout_marginBottom="50dp"
android:background="#drawable/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/duck_tale"
android:layout_width="275dp"
android:layout_height="150dp"
android:scrollbars="vertical"
android:layout_gravity="center"
android:fontFamily="#font/applesdgothicneoeb"
android:text="안녕하세요 이것은 테스트입니다."
android:textAlignment="center"
android:textSize="25sp" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
here is my duckFragment
package com.example.test;
import static android.speech.tts.TextToSpeech.ERROR;
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import java.util.Locale;
public class Duck1Fragment extends Fragment {
private TextToSpeech tts;
private TextView duck;
Context ct;
public Duck1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_duck1, container, false);
ct = getActivity().getApplication();
ImageView duck_image = (ImageView) rootView.findViewById(R.id.ducktale_image);
TextView duck = (TextView) rootView.findViewById(R.id.duck_tale);
duck.setText(
);
duck.setMovementMethod(new ScrollingMovementMethod());
tts = new TextToSpeech(ct, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != ERROR) {
tts.setLanguage(Locale.KOREAN);
}
}
});
duck_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tts.setSpeechRate(1.0f);
tts.speak(duck.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
return rootView;
}
}
here is my PageAdapter
package com.example.test;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.List;
public class PagerAdapter extends FragmentStateAdapter {
private List<Fragment> fragments;
private static final int NUM_PAGES = 3;
public PagerAdapter(FragmentActivity fragmentActivity, List<Fragment> fragments) {
super(fragmentActivity);
this.fragments = fragments;
}
#NonNull
#Override
public Fragment createFragment(int position) { [tag:tag-name]
if (position == 0) return new Duck1Fragment();
else if (position == 1) return new Duck2Fragment();
else return new Duck3Fragment();
}
#Override
public int getItemCount() {
return NUM_PAGES;
}
}
i want to press my custom button on toolbar..
so u guys can help me?
i serched for google and chatgpt but i cant solve it

android.view.inflate exception.Binary XML file line #11.Error inflating class fragment

I am a beginner in Android. I am trying to inflate a list fragment into my activity and i am getting the "android.view.inflate exception.Binary XML file line #11.Error inflating class fragment.I am attaching the codes below..
fragment class
package com.example.fragmentrough;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MenuFragment extends ListFragment {
String[] AndroidOS = new String[] { "Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb","Ice Cream SandWich","Jelly Bean","KitKat" };
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.list_fragment, container, true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, AndroidOS);
setListAdapter(adapter);
return view;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), ""+position, 4000).show();
getListView().setSelector(android.R.color.holo_blue_dark);
}
}
Main Activity
package com.example.fragmentrough;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
MenuFragment fragment=new MenuFragment();
transaction.add(R.id.fragment1,fragment);
transaction.commit();
}
}
activity_main.xml
<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"
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="com.example.fragmentrough.MainActivity" >
<fragment
android:id="#+id/fragment1"
class=com.example.fragmentrough.MenuFragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp" />
</LinearLayout>
**
list_fragment.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" >
<ListView
android:id="#android:id/list"
android:layout_width="233dp"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Try this
<fragment
android:id="#+id/fragment1"
android:name="com.example.fragmentrough.MenuFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp" />

Android TextView is failing with null pointer exception

Android TextView is failing with null pointer exception
and its failing at "disp.setText("0");"
inside the method "protected void onCreate(Bundle savedInstanceState) {"
I tried most of the suggestions provided in net. Still could not resolve it.
Please need help....
My Layout: "fragment_main.xml"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="num_Clicked"
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="com.ullas.Test1.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:gravity="right"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
and My Activity is : "MainActivity.java"
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class MainActivity extends ActionBarActivity {
TextView disp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
disp = (TextView)findViewById(R.id.textView1);
disp.setText("0");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
you tell your layout is "fragment_main.xml" and textview is in this xml.
in your code, you are setting a different layout xml:
setContentView(R.layout.activity_main);
disp = (TextView)findViewById(R.id.textView1);
you must set the layout you want to use, in order to be able to reach your textview:
setContentView(R.layout.fragment_main);
disp = (TextView)findViewById(R.id.textView1);
The view textView1 is not found in the layout file loaded. View hierarchy should be created before getting view from the layout.
Complete "MainActivity.java" is below
package com.example.test;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView disp;
disp = (TextView) findViewById(R.id.textView1);
disp.setText("0");
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
and Below is the Complete "fragment_main.xml"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="right"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Below is the "activity_main.xml"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" />
and ONE MORE THING , WHEN I CREATE A NEW PROJECT BY DEFAULT IT WILL GIVE ME THIS "Fragment_main.xml"

Using getFragmentManager on a ListActivity

I just want to know if this is possible since first. I have created a custom listView based on the tutorial I read from Sai Geetha. Well it works perfectly on my app except that it needs to extend ListActivity instead of FragmentActivity. Now I'm having a hard time configuring and adding a dialog for this since I need to apply a fragment dialog and I can't use the getFragmentManager() since I'm not working with the FragmentActivity. Is there's another way I can do to work on this without sacrificing the ListActivity? Thanks!
Here's my code so far
XML:
conversation_list_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#id/android:list"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
group_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/action_bar_separator"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Group Name"
android:id="#+id/txt_group_name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#color/dark_gray"
android:shadowColor="#color/dark_shadow"
android:shadowRadius="1"
android:shadowDy="1"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_back"
android:background="#drawable/btn_navigate_back"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_information"
android:background="#drawable/btn_information"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Conversations"
android:id="#+id/textView2"
android:textColor="#color/holo_light_blue"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="340dp"
>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.mark.exercise.ListViewFragment"
android:id="#+id/fragment"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:text="Ask something"
android:id="#+id/btn_ask_question"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
Java
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by pc on 9/24/13.
*/
public class GroupActivity extends FragmentActivity {
Button information, back, new_topic;
ListView conversations;
TextView group_name;
String name, group_description, group_administrator,image_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_screen);
Intent intent = getIntent();
name = intent.getStringExtra("group_name");
group_description = intent.getStringExtra("group_description");
group_administrator = intent.getStringExtra("group_administrator");
image_id = intent.getStringExtra("image_id");
information = (Button)findViewById(R.id.btn_information);
back = (Button)findViewById(R.id.btn_back);
new_topic = (Button)findViewById(R.id.btn_ask_question);
group_name = (TextView)findViewById(R.id.txt_group_name);
group_name.setText(name);
information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupActivity.this, GroupInformationActivity.class);
intent.putExtra("group_name",name);
intent.putExtra("group_description",group_description);
intent.putExtra("group_administrator",group_administrator);
intent.putExtra("image_id",image_id);
startActivity(intent);
GroupActivity.this.overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
});
new_topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCreateNewTopicDialog();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
private void showCreateNewTopicDialog() {
FragmentManager fm = getSupportFragmentManager();
DialogFragmentCreateGroup createGroup = new DialogFragmentCreateGroup();
createGroup.show(fm, "create_group");
}
#Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
}
}
List Fragment
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
/**
* Created by pc on 9/27/13.
*/
public class ListViewFragment extends ListFragment {
final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.conversations_list_view,
container, false);
setListView set_list = new setListView();
set_list.start();
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), ConversationActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
private class setListView extends Thread {
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
setConversations();
}
});
}
}
private void setConversations(){
list.clear();
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
list,
R.layout.custom_list_main_conversations,
new String[] {"message","date", "reply_count", "stars_count"},
new int[] {R.id.txt_conversation_message,R.id.txt_topic_date, R.id.txt_no_of_reply, R.id.txt_no_of_stars}
);
for(int ctr=0;ctr<=5;ctr++){
Random randomGenerator = new Random();
HashMap<String,String> item_list = new HashMap<String,String>();
item_list.put("message", "This is the conversation number "+(ctr+1)+" and this topic is just a dummy data.");
item_list.put("date", "0"+(ctr+1)+"/0"+(ctr+2)+"/2013 "+(ctr+1)+":00:am");
item_list.put("reply_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
item_list.put("stars_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
list.add(item_list);
}
android.app.ListFragment lf = new android.app.ListFragment();
lf.setListAdapter(adapter);
}
}
You can use ListFragment inside FragmentActivity instead of using a ListActivity.

Why does this only work inside a host class that extends FragmentActivity?

I have a fragment with the following layout: my_fragment.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/appName"
android:id="#+id/textView" android:layout_gravity="center"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"
android:id="#+id/button1"
android:layout_gravity="center"/>
and corresponding MyFragment.java
package com.example.appdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_bar, container, false);
Button button1 = (Button)view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "Button Pressed", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
and this is hosted inside MainActivity.java
package com.example.appdemo;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
with the following layout activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/fragment"
android:name="com.example.appdemo.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Now, my question is why does this only work when I extend FragmentActivity? I'd like to extend Activity. How do I accomplish that?
Why does this only work inside a host class that extends FragmentActivity?
Because you are inheriting from android.support.v4.app.Fragment instead of android.app.Fragment.

Categories

Resources