path.reset with menu item onclick - android

I'm making an paint application just to learn how to work with eclipse.
I would like to clear my (paint)view whenever I click the menu item 'Reset'
The menu item 'Reset' is in the XML file. Now i know i have to use path.reset but don't know how where to write it. Hope someone is able to help me out.
<item android:id="#+id/ResetAction"
android:orderInCategory="100"
android:title="Reset"
android:showAsAction="never"
android:menuCategory="container"
></item>
a
public class SingleTouchActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SingleTouchEventView(this, null));
}
#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.main_fragment, container,
false);
return rootView;
}
}
}

Path.reset() is a method used to reuse a Path object.
For example, if you use a Path object to draw a shape that changes at each frame, at the beginning of the frame drawing (in the draw(Canvas) or onDraw(Canvas) methods) you can reset reset the path, add the new shape to the path and then draw the Path (canvas.drawPath())

Related

Inflating only a specific action bar menu

I have an action bar menu defined in my main activity. Now, one of the fragments I am using in that activity has its own action bar menu but when I click on the menu option, I get both the fragment's as well as the activity's menu items. How to ensure that only that fragment's menu item is displayed?
My java code for that fragment is:
public class ProfileD extends Fragment {
TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd;
ImageView imageView_dp;
public ProfileD() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false);
tv_named = (TextView)rootView.findViewById(R.id.tv_named);
tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd);
tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd);
tv_aged = (TextView)rootView.findViewById(R.id.tv_aged);
tv_biod = (TextView)rootView.findViewById(R.id.tv_biod);
tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd);
imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp);
setHasOptionsMenu(true);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.edit_profile) {
Intent intent = new Intent(getContext(),PersonalDetails.class);
intent.putExtra("Edit",1);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
ActionBar actionBar = appCompatActivity.getSupportActionBar();
actionBar.setTitle("Profile");
}
}
The xml file for the menu is:
<?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/edit_profile"
android:orderInCategory="100"
android:title="Edit"
app:showAsAction="never" />
</menu>
I found a solution to my problem. I am posting it here so if anyone else is facing the same issue, they can benefit from this:
I created a static menu object and stored the activity's action bar menu in that object. I then called that object from my fragment and cleared it thereby clearing the activity's menu and leaving me with the fragment's menu
Here is the updated java code for the fragment
public class ProfileD extends Fragment {
TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd;
ImageView imageView_dp;
public ProfileD() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false);
tv_named = (TextView)rootView.findViewById(R.id.tv_named);
tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd);
tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd);
tv_aged = (TextView)rootView.findViewById(R.id.tv_aged);
tv_biod = (TextView)rootView.findViewById(R.id.tv_biod);
tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd);
imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp);
setHasOptionsMenu(true);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
HomePage.menu_home.clear();
getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.edit_profile) {
Intent intent = new Intent(getContext(),PersonalDetails.class);
intent.putExtra("Edit",1);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
ActionBar actionBar = appCompatActivity.getSupportActionBar();
actionBar.setTitle("Profile");
}
}

Error No suitable method found for add(int, Fragment)

I was trying to inflate a fragment in an Activity and here is the code.
public class DetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container2, new DetailFragment())
.commit(); //Line with Error
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is my DetailFragment class
/**
* A placeholder fragment containing a simple view.
*/
public class DetailFragment extends Fragment {
public DetailFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
return rootView;
}
}
Here goes the layout for fragment_detail
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="18sp"
android:id="#+id/fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The error I am facing is cannot resolve method 'add(int,com.example...DetailFragment)'. As, I am beginner to android, please also guide me on how should I go about, tackling such type of errors.
Your DetailFragment need to extends from android.support.v4.app.Fragment instead of Fragment. Something like this
public class DetailFragment extends android.support.v4.app.Fragment {
......
}
Make sure your DetailFragment uses the fragment from the version 4 support library and
import the library below into your fragment class. The problem is that you are using android.app.fragment instead of android.support.v4.app.Fragment.
import android.support.v4.app.Fragment;
public class DetailFragment extends Fragment{
}

Android add icon in toolbar inside a fragment

I implement a toolbar and replace it with my actionbar according to my requirments. Now I want to add an icon in toolbar inside a fragment. I tried onCreateOptionMenu() and pass my menu xml to it. But it wont work. I also tried some other things and googling but nothing works so far. Does anybody have any idea about this. Here is my fragment code
public class Fragment_FavouriteLocations extends Fragment {
public Fragment_FavouriteLocations() {
// 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.fragment_main_fav_location_row_item, container, false);
}
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fav_location_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.edit_button) {
Toast.makeText(getActivity(), "", Toast.LENGTH_LONG).show();
return true;
}
//
// if(id == R.id.action_search){
// Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
Here is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mayApp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/edit_button"
mayApp:icon="#android:drawable/ic_menu_edit"
android:title="Edit"
mayApp:showAsAction="always" />
</menu>
Thanks in advance
In your Fragment, in the onCreateView() method, you have to add:
setHasOptionsMenu(true);
Otherwise the onCreateOptionsMenu() is never called.
you may need to use invalidateOptionsMenu() to tell the fragment to redraw the menu actions like this ...
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().invalidateOptionsMenu();
setHasOptionsMenu(true);
}

Super not called exception in android?

This is my code
#Override
public void onCreate(Bundle SavedInstanceState){
//Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.forecastfragment, menu);
}
public boolean onOptionItemSelected(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_refresh){
FetchWeatherTask weatherTask=new FetchWeatherTask();
weatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
When I execute this I'm getting an " app.SuperNotCalledException" I saw answers for similar questions but I don't get it Pls all helps appreciated. Thank you.
You have to call super method from your parent class before overriding it.
#Override
public void onCreate(Bundle SavedInstanceState){
super.onCreate(savedInstanceState);
//Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
Hope this will be useful for you.

Android app close with null exception [duplicate]

This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
I have a very simple android app being built. I have 2 buttons and 1 textview. I build them graphically in fragment_main.xml. Below is my full codes.
public class MainActivity extends ActionBarActivity {
TextView tvOut;
Button btnOk;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
View.OnClickListener oclBtnOk = new View.OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
};
btnOk.setOnClickListener(oclBtnOk);
}
#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;
}
}
}
If I comment this part of the codes it works well.
View.OnClickListener oclBtnOk = new View.OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
};
btnOk.setOnClickListener(oclBtnOk);
Even though I use setContentView(R.layout.main_activity); all my button and text view appears and quite puzzle why even with main_activity my buttons and text view appears well.
You never assign values to your views. Use findViewById to get references to the views with the id's you gave them in your XML layout.
setContentView(R.layout.fragment_main);
// get the views
btnOk = (BUtton) findViewById(R.id.your_button_id);
tvOut = (TextView) findViewById(R.id.another_id);
btnOk.setOnClickListener(...);

Categories

Resources