Access int data of View Class from Activity - java

I have a Drawscreen.class file which the main activity and Drawthegraph.class which extends view. I have a method in Drawthegraph.class which I need to call from Drawscreen.class.How can I do that?
Drawscreen.class-
public class Drawscreen extends ActionBarActivity
{
//LinearLayout linear=(LinearLayout)findViewById(R.id.main_layout);
//draw=(Drawthegraph)findViewById(R.id.main_layout);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
ActionBar actionbar=getSupportActionBar();
actionbar.show();
View drawthegraph=new Drawthegraph(this);
setContentView(drawthegraph);
drawthegraph.setBackgroundColor(color.Ivory);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drawscreen, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
case R.id.undo:/*Call method of view here*/
break;
}
return super.onOptionsItemSelected(item);
}
}
Drawthegraph.class
public class Drawthegraph extends View
{
private int lines;
----
----
public void decrease_lines() /*Call this function from Drawscreen*/
{
if(lines>0)
{
lines--;
}
}

Use your drawthegraph variable as an instance field:
public class Drawscreen extends ActionBarActivity
{
//LinearLayout linear=(LinearLayout)findViewById(R.id.main_layout);
//draw=(Drawthegraph)findViewById(R.id.main_layout);
private Drawthegraph drawthegraph;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
ActionBar actionbar=getSupportActionBar();
actionbar.show();
this.drawthegraph=new Drawthegraph(this);
setContentView(drawthegraph);
drawthegraph.setBackgroundColor(color.Ivory);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drawscreen, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
case R.id.undo:/*Call method of view here*/
drawthegraph.decrease_lines();
break;
}
return super.onOptionsItemSelected(item);
}
}

Your Drawthegraph object must be a field of your Activity:
public class Drawscreen extends ActionBarActivity {
Drawthegraph drawthegraph;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
drawthegraph = new Drawthegraph(this);
setContentView(drawthegraph);
drawthegraph.setBackgroundColor(color.Ivory);
}
...
then you can call wherever you want in your Drawscreen:
drawthegraph.decrease_lines();

Related

ArrayList in Android AsynTask

How can I Create a class AsynkTask in which the class DataContainer is filled. Continue to fill the class DataContainer with synthetic data. After each new one Record should pause the AsynkTask class for a short time (200ms).?
public class DataContainer {
public ArrayList<String> mGlobalDataStore1 = initializeData();
static ArrayList<String> initializeData(){
ArrayList<String> data = new ArrayList<String>();
for (Integer i=0; i<100; i++){
data.add("Item " + i.toString());
}
return data;
}
}
public class TestTask extends AsyncTask<Character, String, ArrayList<String>> {
#Override
protected ArrayList<String> doInBackground(Character... integers) {
return null;
}
#Override
protected void onProgressUpdate(String... values) {
}
#Override
protected void onPostExecute(ArrayList<String> s) {
}
}
how can I implement the class DataContainer in class AsyncTask?
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private AsyncTask asyncTask;
private RecyclerView recyclerView;
private DataContainer fragment;
private TextView textView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.refresh, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(this,"erfolgreich",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this," nicht erfolgreich",Toast.LENGTH_SHORT).show();
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=findViewById(R.id.progressBar);
textView=findViewById(R.id.textView);
}
I understand that this is not the answer you are looking for. But the AsyncTask api is deprecated from Android 11. Read this link for more info

How to call the menu objects from another class?

These are codes, how to pass the menu object from one class to another?
What's wrong with my code?
This is my MainActivity class.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
String msg = " ";
switch (item.getItemId()){
case R.id.action_settings:
msg = "Settings";
break;
case R.id.action_report:
msg= "Report";
break;
}
Toast.makeText(this, msg + "Checked", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
This is my SecondActivity class
public class Income extends AppCompatActivity{
View_Expenses v = new View_Expenses();
#Override
protected void onCreate(Bundle savedInstanceState) {
v.onCreateOptionsMenu(R.menu.main_menu); //Here have problem
}
Copy the same code you used in the first activity in the second one,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
not the code you tried in onCreate. If you want the menu item responses to be the same, copy the onOptionsItemSelected method too.

onSupportNavigateUp() is not fired anymore after I override onOptionsItemSelected(MenuItem item)

(Sorry for changing the whole question as I found the actual problem after troubleshooting)I have my activity as followed:
public class MyAppActivity extends AppCompatActivity{
#Override
protected void onCreate(...)
{
//...
this.setContentView(contentViewResID);
Toolbar toolbar = (Toolbar) this.findViewById(toolbarResID);
this.setSupportActionBar(toolbar);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
protected void onPause() {...}
#Override
protected void onResume() {...}
#Override
public boolean onSupportNavigateUp() {
Log.d("onSupportNavigateUp()", "pressed");
finish();
return super.onSupportNavigateUp();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {...}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {...}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item == menuAddFood) {
Intent intent = new Intent(this, ActivityAddFood.class);
this.startActivity(intent);
}
if (item == menuSettings){
Intent intent = new Intent(this, ActivitySetting.class);
this.startActivity(intent);
}
if(item == menuUsername){
}
return true;
}
}
EDIT:
After my troubleshooting, I found out after override the onOptionsItemSelected(), onSupportNavigateUp() no longer fired anymore when I pressed the top left BACK / UP / HOME button. I want to do finish() inside onSupportNavigateUp() How to solve this?
You have to override the method in your current Activity and call the super:
public class ActivityExample extends MyAppActivity {
Context context = this;
AppCompatActivity activity = this;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.createMyView(R.layout.activity_log_in, R.id.toolbar);
// ...
}
#Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
}
Also, if you override onOptionsItemSelected, then onSupportNavigateUp will not be called.
//DONT OVERRIDE THIS METHOD
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
break;
}
return true;
}

onCreateOptionsMenu not getting called inside the Fragment class

public class FragmentClass extends android.support.v4.app.Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
inflater.inflate(R.menu.menuItem, menu);
}
}
onCreateOptionsMenu method is never called even though i have placed setHasOptionsMenu(true) inside my onCreate Method.
This is how my Activity class looks like.
https://github.com/jfeinstein10/SlidingMenu/blob/master/example/src/com/slidingmenu/example/fragments/FragmentChangeActivity.java
More update: This is my method inside Fragment Class.
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater){
inflater.inflate(R.menu.facesheet, menu);
super.onCreateOptionsMenu(menu,inflater);
}
This is inside the BaseActivity class.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
I think you are not overriding the right method.
Try this code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menuItem, menu);
return true;
}
Source link.
Additional try to call setMenuVisibility(true);

Some Serious looking bug in instnaceof or App package

I made 2 activities and an Intent extended class named SomeSpecialIntent, when you press on the first activity's textview you go to the second using new SomeSpecialIntent class instnace, but something strange goes with it on the way, because the phrase getIntent() instnaceof SomeSpecialIntent returns false on the second activity!
whats up with that?
the code for the check i made:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new SomeSpecialIntent(MainActivity.this,SomeActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class SomeActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Check",""+(getIntent() instanceof SomeSpecialIntent));//returns false!!!!
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class SomeSpecialIntent extends Intent {
public SomeSpecialIntent(Context context,
Class<?> class1) {
super(context,class1);
}
}

Categories