I'm developing an android app in which I want to apply button visibility functionality in activity_2 and that visibility should depend on button click from activity_1
Ex. In activity_2 I have :
<Button android:id="#+id/button1"
android:text="ABC"
android:visibility="gone"/>
It should visible on button click from activity_1
Activity_1 :
<Button android:id="#+id/button1"
android:text="ABC"/>
Please suggest me, I'm a beginner
Pass the visibility you want from Activity A to Activity B in a Bundle.
Passing a Bundle on startActivity()?
Try this:
On clicking the button in the first activity,send a value to second activity through intent.
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("button","clicked");
startActivity(intent);
And then get this value in the onCreate of second activity like this:
String value = getIntent().getStringExtra("button");
then check the value with if statement
if(value.equalsIgnoreCase("clicked")){
//make your button visible here
}
else{
//button not visible
}
Related
I have an activity which consists of an EditText. The problem is I want to open this activity with a pre-written EditText.
Here is the example:
I want when I open this activity from the MainActivity, the text of EditText will always be set to "Tùng".
very very simple just put it in your XML
<EditText
android:id="#+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tùng"/>
or programattically you can set it oncreate of activity as below
edt.setText("Tùng");
Use editText.setText("Text you want to show"); in your activity's onCreate/onStart/onResume method (depending on the expected behavior). Also make sure you first have a reference to your editText by calling findViewById(R.id.yourEditTextId) before calling setText to this element.
As far as I understand you need to transfer data from one activity to antoher. You should use Intents:
Intent intent = new Intent(MainActivity.this, YourOtherActivity.class);
intent.putExtra("TUNG_ID", "Tung");
startActivity(intent);
And on the other activity. In onCreate method:
String tungString = getIntent().getStringExtra("TUNG_ID");
Log.d("ApplicationTag", tungString); //it's gonna print "Tung"
EditText et = findViewById(R.id.youredittext); //find your edittext to write text
et.setText(tungString); //This will populate received string into edittext
I have an Activity A, which opens a DialogFragment. In this dialog, a button opens an Activity B.
I would like this Activity B to open below the DialogFragment (which remains open), and I don't want the dialog to be recreated.
How can I achieve this? Is there a way to change the DialogFragment's parent Activity?
Cheers.
You can use a transition to do this.
public void Trans(View v){
Intent intent = new Intent(this,SecondActivity.class);
String transitionName = getString(R.string.transition_album_cover);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(HomeActivity.this,
albumCoverImageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(HomeActivity.this, intent, options.toBundle());
}
In the layout of HomeActivity:
<ImageView
android:layout_height="200dp"
android:layout_width="200dp"
android:src="#drawable/pic"
android:id="#+id/transPic"
android:onClick="Trans"
android:transitionName="#string/transition_album_cover" />
And in the layout of SecondActivity
<ImageView
android:layout_height="300dp"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:id="#+id/transPic"
android:src="#drawable/pic"
android:transitionName="#string/transition_album_cover" />
Here I used an ImageView as a common element in both HomeActivity and Second Activity. I believe you can try something of this sort for the dialogue box too. I have not done it for Dialog box myself though.
EDIT: Changing the parent of the dialogue is not something that is achievable this way. I didn't read that part when I was posting the answer. But this is still worth a try I guess, if you are not obliged to use a Dialog itself.
Is that's possible when user open my app to lunch Activity depends on something or lunch another activity not mainActivity if some happened.
My problem is that I have tow activities LogInActivity and BrowseDataActivity inside browseData I have Viewpager uses fragments which means that I can't use fragments instead of activities because you can't have fragments inside fragment.
if user is logged in then start BrowseDataActivity other wise LogInActivity is that possible?
I think I saw some code working around
Useing java script even I could load my views depending on.... but I don't wanna do that or use java scripts I could work around but useing stupid way.
Thanks
Set LoginActivity as main activity and in LoginActivity.onCreate start the other activity if the user is already logged in
OR
Create an other activity (let's call it SplashScreenActivity) with something like this in onCreate :
setContentView(R.layout.myview);
boolean loggedIn = ...;
Intent i;
if(loggedIn)
i = new Intent(this, yourActivity.class);
else
i = new Intent(this, LogInActivity.class);
startActivity(i);
Sorry I cant add code tags because i am on my mobile
Just a modification of the above code it seems you want only two activities only. Lets get the requirements
Only two activites
Show BrowseActivity only if logged in
if not logged in show LoginActivity
Simple
In Browse Activity.
public boolean isLoggedin = prefs.getBoolean("isLoggedIn",false);
...
onCreate(){
if (isLoggedIn)
setContentView(R.layout.browseActivity)
else{
intent i = new intent(this,LoginActivity);
startActivity(i);
}
Now in LoginActivity.
onCreate(){
//Code for loggin in possible conncecting to a server etc... whatever your implementation is
//If successful
SharedPreferences sp = getSharedPreferences("user_Data",MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean("isLoggedIn", true);
ed.commit();
//As the user is now logged in as they got this far...take them to the browse activity!
Intent i = new intent(this,BrowseActivity.class);
startActivity(i);
//else they are not logged in so show a "try again dialog"
}
This complements all of the requirements.
Hope that helps
well i know no one answer this question, but i want to answer my own question to help anyone facing same problem.
Problem
i have LogInActivity and BrowseDataActivity "with Swipe Tabs"
we can write code in LogInActivity check if user loggedIn then show BrowseDataActivity remove LogInActivity from Stack else load LogInActivity views but problem is that Activity Window Animation when user is loggedIn first LogInActivity Window Animation then BrowseDataActivity Window Animation if you could remove this animation every thing is cool but i will solve this problem by using Fragments.
solution
we will have one Activity called MainActivity something like that
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/fragment_container"
android:visibility="gone"></FrameLayout>
</FrameLayout>
,we will have 1 Fragment LogInFragment what ever it's looks like
and BrowseDataActivity's Swipe Tabs as you can see we have viewPager
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isUserLoggedIn()){
setUpSwaipTabs();// setUp Tab and Viewpager
}else{
showLogInFragment(); // load your Fragemtn
//in FrameLayout id=fragment_container and show
// and hide viewpager.
}
}
Here is my question,my app flow is screen1,screen2,screen3.and their content views are like this:
screen1.java-->screen1.xml
screen2.java-->screen3.xml
screen3.java-->screen3.xml
here in screen1 if user clicks on toggle button to is going to screen2 then screen3 in screen3 payment success then only screen1 toggle button should change,how to achieve this,didn't get any idea,plz help me,Thanks.
You can save the state of toggle button in Shared preferences.
Check this : Shared Preferences Android and Example
Hope this helps.
Use a static global array list and save the pressed states at their positions in that list. In the next activities use that array list to set the toggle status of your buttons
This is similarly like saving the checked states of a checkbox in a listview
You can pass button state using Bundle between activities like following
Start activity 2
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);
Get that value in activity 2 like
#Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}
same like above you can pass is to Activity 3.
Or
You can make a static variable in you Activity 1 and then access that from Activity 3.
You can make that toggle button static and then you can change its state from any activity.
static ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggle_btn);
but you need to be careful.
You could also pass the toggle state via intents and onActivityResult().
Here is a video tutorial on intents
Here is some a tutorial on onActivityResult
I'm making a button in xml, like this:
<Button
android:id="#+id/buttondp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thisisstringtext" />
and I want it to direct it to another page coded in xml. Can anyone help me out?
Make another activity and use
setContentView(R.layout.your_other_layout);
inside of it.
Then in the onClickListener for your button put this:
Intent i = new Intent(YourActivity.this, YourOtherActivity.class);
startActivity(i);
You can add the onclick listener to your button, so: android:onclick="method_in_your_activity".
In your activity added the method (method_in_your_activity) and add startActivity(NewActivity).
If you dynamically want to change the Content of your activity you can always call setContentView(my_layout) and change the content. However; its best practice to use another Activity.
You can use different activities for different layout. But if you want to use same activity for different layout then you should go for ViewFlipper . You can also get some animation when switching from one view to another. Tutorial regarding the same can be found here.
using this code in java file you will click button to redirect next page
public void onClick(View v)
{
Intent ia=new Intent(getApplicationContext(),second.class);
startActivity(ia);
}
In your button XML, add :
android:onCLick="myRedirectFunction"
In your MyMainActivity.java, add function named myRedirectFunction and inside that function :
Intent homepage = new Intent(MyMainActivity.this, MySubActivity.class);
startActivity(homepage);