Call a fragment from an activity with a fragmenttransaction - java

what I am trying to do is call fragment from an activity with a FragmentTransaction, at that point it works for me, the problem is that when the activity calls the fragment, the two are presented on the screen.
Activity java code :
public class ElectricalCalculators extends AppCompatActivity implements View.OnClickListener {
Button kwbutton , evbutton ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_electrical_calculators);
findViewById(R.id.menu_back).setOnClickListener(this);
findViewById(R.id.menu_home).setOnClickListener(this);
Button kwbutton = (Button) findViewById(R.id.ampsBoutton);
kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , AmpsConversion.class);
startActivityForResult(intent,0);
}
});
Button evbutton = (Button) findViewById(R.id.evBoutton);
evbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , ElectronVoltsConversion.class);
startActivityForResult(intent,0);
}
});
Button jbutton = (Button) findViewById(R.id.joulesBoutton);
jbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , JoulesConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwbutton = (Button) findViewById(R.id.kwBoutton);
Kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwhbutton = (Button) findViewById(R.id.kwhBoutton);
Kwhbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwhConversion.class);
startActivityForResult(intent,0);
}
});
}
#Override
public void onClick(View v) {
Class clazz = null;
FragmentManager fragmentManager = getSupportFragmentManager();
if (v.getId() == R.id.menu_back){
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
}
/** switch (v.getId()) {
case R.id.menu_back:
//clazz = Fragmen2.class;
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
break;
case R.id.menu_home:
clazz = MainActivity.class;
break;
/**case R.id.five_tabs_changing_colors:
clazz = FiveColorChangingTabsActivity.class;
break;*/
//}
// startActivity(new Intent(this, clazz));
}
}
Fragment code :
public class Fragmen2 extends Fragment {
Button Tboton,
Lboton,
Pboton,
Dboton,
wboton,
eboton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragmen2, container, false);
Tboton = (Button)view.findViewById(R.id.boton1fragmen2); /**boton que conecta el fragment 2 con la actividad temperatura */
Tboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Tboton = new Intent( getActivity() ,Temperatura.class);
startActivity(Tboton);
}
});
Lboton =(Button)view.findViewById(R.id.boton2fragmen2);
Lboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Lboton = new Intent(getActivity() , Dimensions.class);
startActivity(Lboton);
}
});
View v = inflater.inflate(R.layout.fragment_fragmen1, container, false);
/**Pboton = (Button)view.findViewById(R.id.boton3fragmen2);
Pboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});*/
Dboton = (Button)view.findViewById(R.id.boton4fragmen2);
Dboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Dboton = new Intent(getActivity(),Dimensions.class);
startActivity(Dboton);
}
});
wboton = (Button)view.findViewById(R.id.boton3fragmen2);
wboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent wboton = new Intent(getActivity(),Weight.class);
startActivity(wboton);
}
});
eboton = (Button)view.findViewById(R.id.boton4fragmen2);
eboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent eboton = new Intent(getActivity(),ElectricalCalculators.class);
startActivity(eboton);
}
});
return view;
}
}

Related

How to make Bottom Sheet Dialog that shows one time only

I want to create a Bottom Sheet Dialog. It looks like the image below
enter image description here
public class MainActivity extends AppCompatActivity {
//this is Button that open dialog
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});
}
}
I have tried many methods such as SharedPreferences. But it not working
SharedPreferences preference = getSharedPreferences("mPreference", MODE_PRIVATE);
boolean isUserAgreed = preference.getBoolean("isUserAgreed", false);
if (!isUserAgreed){
// show bottomSheetDialog
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("mPreference", MODE_PRIVATE).edit();
editor.putBoolean("isUserAgreed", true);
editor.apply();
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});

createDialog for onBackPressed but my app showing error close app

I have created a new layout name dialog_exit for onBackPressed but when I install and open, my application can not open and showing error close app
Please review my whole code and guide me how can I solve this problem
Here is my main activity code
public class MainActivity extends AppCompatActivity {
public Dialog mDialog;
public Button mDialogyes, mDialogno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDialog();
}
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) findViewById(R.id.yes);
mDialogno = (Button) findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
});
}
#Override
public void onBackPressed() {
mDialog.show();
}
}
Here is my layout code as screenshot because
stackoverflow not allow me to add more code that why sharing image
Updated code of createDialog function
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
try this code
mDialogyes = (Button)mDialogyes. findViewById(R.id.yes);
mDialogno = (Button)mDialogyes. findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
You have to do like this
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);

Updated ImageView gets lost when I switch back to the fragment after switching to another fragment

So far I managed to get the ImageView in my Fragment to update when it's selected in an Activity. But now when I switch from my Fragment PeopleFragment to another fragment like TaskFragment and then back, the ImageView I selected is no longer there, but rather the default ImageView that loads when the Fragment is first initialized.
PeopleFragment.java
public class PeopleFragment extends Fragment {
ImageButton profileImage;
private FirebaseAuth mAuth;
TextView fullName;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_people2, container,
false);
profileImage = (ImageButton) view.findViewById(R.id.imageButton4);
profileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent settingsClick = new Intent(getActivity(),
ProfileSettingsActivity.class);
startActivityForResult(settingsClick,0);
}
});
fullName = (TextView) view.findViewById(R.id.userProfileFullName);
mAuth = FirebaseAuth.getInstance();
return view;
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
/**
* Updates the view according to the authentication status.
* #param user the current FirebaseUser
*/
private void updateUI(FirebaseUser user) {
if (user != null) {
fullName.setText(user.getDisplayName());
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED) return;
//Getting the Avatar Image we show to our users
ImageView avatarImage =
(ImageView)getView().findViewById(R.id.imageButton4);
//Figuring out the correct image
String drawableName = "profile1";
switch (data.getIntExtra("imageID",R.id.teamid00)) {
case R.id.teamid00:
drawableName = "profile1";
break;
case R.id.teamid01:
drawableName = "profile2";
break;
case R.id.teamid02:
drawableName = "profile3";
break;
case R.id.teamid03:
drawableName = "profile4";
break;
case R.id.teamid04:
drawableName = "profile5";
break;
case R.id.teamid05:
drawableName = "profile6";
break;
default:
drawableName = "profile1";
break;
}
int resID = getResources().getIdentifier(drawableName, "drawable",
getActivity().getPackageName());
avatarImage.setImageResource(resID);
}
}
ProfileActivity.java
public class ProfileSettingsActivity extends AppCompatActivity {
//ImageButton targetImage;
Button loadButton;
ImageView profileImage1;
ImageView profileImage2;
ImageView profileImage3;
ImageView profileImage4;
ImageView profileImage5;
ImageView profileImage6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_settings);
profileImage1 = (ImageView) findViewById(R.id.teamid00);
profileImage2 = (ImageView) findViewById(R.id.teamid01);
profileImage3 = (ImageView) findViewById(R.id.teamid02);
profileImage4 = (ImageView) findViewById(R.id.teamid03);
profileImage5 = (ImageView) findViewById(R.id.teamid04);
profileImage6 = (ImageView) findViewById(R.id.teamid05);
// Makes the arrow image act as a back button.
ImageView backButton = (ImageView) findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
profileImage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
}
public void setProfilePicture(View view) {
//Creating a Return intent to pass to the Main Activity
Intent returnIntent = new Intent();
//Figuring out which image was clicked
ImageView selectedImage = (ImageView) view;
//Adding stuff to the return intent
returnIntent.putExtra("imageID", selectedImage.getId());
setResult(RESULT_OK, returnIntent);
//Finishing Activity and return to main screen!
finish();
}

Get certain intents

So lets say i have 4 buttons , and each button contains an intent which navigates to an activity.They all navigate to a single same activity . When i click the first button i want that new activity to show "Hi" , When i click the second button i want it to show "Bye" and so on . How do i do that ?
Here is a simple code to start with
public class Intentt extends Activity {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
}
Just pass some data through intent extras, or intent extra Bundle:
Intent i=new Intent(context,MainActivity.class);
i.putExtra(id, 453);
context.startActivity(i);
Example:
Button b1, b2, b3, b4;
Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
b4 = (Button) findViewById(R.id.button5);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_1);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_2);
startActivity(i);
}
});
}
And then in that activity you can switch on that values getting them like that:
String id = intent.getStringExtra("id");
int id = intent.getIntExtra("id",-1);
and then use the if-elseif-else or switch statement(s) to change the actions based on the passed action, so you can display your messages
To properly recieve them you need to set activity's launchMode to "standard" because of this bug in AndroidManifest.xml and create
public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int id = getIntent().getIntExtra(ID_ACTION, -1);
if (id == ACTION_NULL) {
Log.d("TAG", "id is null");
Toast.makeText(MainActivity.this, "id is null!", Toast.LENGTH_SHORT).show();
} else if (id == ACTION_1) {
Log.i("TAG", "ALLOHA! from button 1");
Toast.makeText(MainActivity.this, "Aloha from button 1!", Toast.LENGTH_LONG).show();
} else if (id == ACTION_2) {
Log.i("TAG", "Hello from button 2");
Toast.makeText(MainActivity.thi,"Hello from button 2!", Toast.LENGTH_LONG).show();
}
}
In each intent you could pass parameterts
Intent i=new Intent(context,SendMessage.class);
i.putExtra("KEY_MESSAGE", "Hola amigo");
In the other activity
Intent i = getIntent();
String message = i.getStringExtra("KEY_MESSAGE")
Toast.make(this,message,Toast.LENGTH_SHORT).show()
Re write the button click listener as follows :
b1.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hi");
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hello");
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Bye");
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","See you");
startActivity(i);
}
});
In your MainActivity class onCreate Method, you can access the passed data by :
String passedData = getIntent().getStringExtra("Data");
you can use this passedData to display on screen.
Do it like this:
public class Intentt extends Activity implements View.OnClickListener {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Hi");
startActivity(i)
break;
case R.id.button3:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Bye");
startActivity(i)
break;
case R.id.button4:
break;
.
.
.
}
}
}
After that in your mainActivity retrieve string that you sent with:
Bundle extras = getIntent().getExtras();
String s = extras.getString("STRING_I_NEED");

Pass a XML resource from one activity to another activity using java code in Android Studio?

I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}

Categories

Resources