public class FullscreenActivity extends AppCompatActivity {
private ImageButton act;
private ImageButton sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
act = (ImageButton) findViewById(R.id.act);
sat = (ImageButton) findViewById(R.id.sat);
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
}}
What am I doing worng. I have a main UI with six imagebuttons, each one linking to a different activity. How can I link then all in the main activity which is called fullscreen activity
I think you want to bring the FullScreenActivity to front so you only have to finish the front Activity because you didn't finish it
just do finish(); on your sat.class or act.class
or....
You can do this too on your sat.class or act.class:
Intent i = new Intent(this, FullScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
why is the above part inside some other button's click listener? Move it outside the anonymous inner method like
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
}
});
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
Related
SubActivity cannot INVISIBLE the button in MainActivity I tried to use intent, but I'm not sure.
it SubActivity java code
try to INSIBLE the resetB button in Sub Activity
public class SubActivity extends Activity {
Button resetH, resetH2;
Button resetB, miC;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ImageButton back = (ImageButton) findViewById(R.id.back);
resetH = (Button) findViewById(R.id.resetH);
resetH2 = (Button) findViewById(R.id.resetH2);
resetB = findViewById(R.id.resetB);
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.VISIBLE);
}
});
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
I used intent, but I can't move the button
How to limit my main activity to not go back on the last page of my Fragment once return from the last page of Fragment to Main activity like I have One Home activity and that home activity leads to 3 more activities on clicking the button and in which one activity has fragments. The last page of the fragment is leading to home activity but when I back pressed on the home activity it again leads me to the last page of a fragment from where I've come and then I press the back button again then my application shuts
MY MAIN ACTIVITY CLASS
public class Home extends AppCompatActivity {
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
public void Consultation_Form(View view) {
startActivity(new Intent(getApplicationContext(), ConsultationForm.class));
finish();
}
public void Registration_Form(View view) {
startActivity(new Intent(getApplicationContext(), RegistrationForm.class));
finish();
}
public void Assessment_Form(View view) {
startActivity(new Intent(getApplicationContext(), AssessmentForn.class));
finish();
}
public void Booking_Form(View view) {
startActivity(new Intent(getApplicationContext(), BookingForm.class));
finish();
}
}
MY FRAGMENT CLASS OF VIEWPAGER
public class PC_v16 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pc_v16, container, false);
TextView tv = (TextView) view.findViewById(R.id.tvReferral);
Button btBackHome = view.findViewById(R.id.btBackHome);
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
}
});
return view;
}
}
You just need to finish your Viewpager Activity before moving to Home Activity.
In your Fragment
Change this
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
}
});
To
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
getActivity().finish();
}
});
From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
just remove finish(); thats it
The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.
I am trying to make a mobile application,but when ever i try to press the button i get a crash. The button should take me to a new activity page, i have already connect the other pages with the button in a right way. i am just wondering if this code is correct or not:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
Button a1= (Button) findViewById(R.id.button);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
}
public void onButtonClick1(View c)
{
Button a1= (Button) findViewById(R.id.button2);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
}
public void onButtonClick2(View d)
{
Button a1= (Button) findViewById(R.id.button3);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
}
public void onButtonClick3(View f)
{
Button a1= (Button) findViewById(R.id.button4);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
i just learned java from some videos in the youtube,so i am not sure if i did the activity function well or not. Thank you
There can be few reasons after it
1) Make sure you defined activity in Manifest file
2) check for android:onClick="" in xml file..
OR
Try Binding Buttons in onCreate() method.
and You can simply use this for onClick
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
Remove onClick attribute in your activity_main.xml file and copy and past this code in your MainActivity.java file
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button);
b2= (Button) findViewById(R.id.button2);
b3= (Button) findViewById(R.id.button3);
b4= (Button) findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
If you are using the onclick attribute in layout XML then you doesn't need to set the onclick listener. If you are not using onclick then you need to set click listener. In your case your case, I think use are using onclick attribute in layout XML and also trying to set the click listener in using java code. so you need to either use onclick attribute in layout XML or use setOnCliickListener() in java code
When app starts at first, start Activity with two button: Create File and Settings. Then if I click on button Create File - start Activity where I write text and save in file.
If we have error when file saved - start activity with two button. Else start another Activity with three buttons: Look file, Edit File, Settings.
How organize correct transitions between this Activity and How start activity with three buttons if file already saved?
public class MainFirstActivity extends AppCompatActivity {
private Button createFile;
private Button settings;
private boolean start = true;
private static final String MY_SETTINGS = "my_settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_first);
createFile = (Button) findViewById(R.id.create_file);
settings = (Button) findViewById(R.id.settings);
createFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, CreateFileActivity.class);
startActivity(intent);
// MainFirstActivity.this.finish();
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory() && start) {
Intent intent = new Intent(MainFirstActivity.this, MainWithFileActivity.class);
startActivity(intent);
//start=false;
MainFirstActivity.this.finish();
Log.e("err", "intent");
}
}
}
MainWithFileActivity
public class MainWithFileActivity extends AppCompatActivity {
public static final String MY_SETTINGS = "MY_SETTINGS";
private Button lookFile;
private Button editFile;
private Button settings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_if_first);
lookFile = (Button) findViewById(R.id.look_file);
settings = (Button) findViewById(R.id.settings);
editFile = (Button) findViewById(R.id.edit_file);
lookFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, LookFileActivity.class);
startActivity(intent);
}
});
editFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this,CreateFileActivity.class);
startActivity(intent);
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
}
Simply check if the file exists. If it does not, hide the lookFile button
Add this in your MainWithFileActivity onCreate method
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory()) {
lookFile.setVisibility(View.GONE);
}