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);
}
}
Related
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
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.
(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;
}
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();
I have set the Image for my ImageButton in XML but trying to change the image in the code below does nothing. Any ideas why?
public class Test extends Activity implements OnClickListener {
ImageButton myImageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_farm_main);
myImageButton = (ImageButton)findViewById(R.id.button);
myImageButton.setBackgroundResource(R.drawable.image1);
myImageButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
You just make sure your image is available in all drawable folder you will create a common drawable folder in your res and then paste your image at that folder then you will try his concept and or otherwise you will try like this..You just Set the image source it will give the answer
public class MainActivity extends Activity {
private ImageButton imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =(ImageButton) findViewById(R.id.imageButton1);
imageView.setImageResource(R.drawable.ic_launcher);
}
}
I guess you want change the image not background.
Substitute
myImageButton.setBackgroundResource(R.drawable.image1);
with
myImageButton.setImageResource(R.drawable.image1);
This code below works perfectly fine if you have a imagebutton in your layout file and a drawable name ic_launcher in drawable directory.
public class MainActivity extends Activity implements OnClickListener{
ImageButton myImageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageButton = (ImageButton) findViewById(R.id.button);
myImageButton.setImageResource(R.drawable.ic_launcher);
myImageButton.setOnClickListener(this);
}
#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 void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Comment below if that's not what you want.