how to set switch button to manual mode in android - android

My application contains two activities :
First activity contains :
1.different types of modes
2.intensity
3.CCT
Inside of the if condition not going control.if am selecting the seekbar the it should return true.if it is true means should move to next activity.
can any one help me
mColorTemp = (SeekBar) findViewById(R.id.intensity1);
mScheduler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean swichAction=false;
if(mColorTemp.isSelected()==true){
swichAction=true;
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.putExtra("swichAction",swichAction);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
Along with this i have button named as Scheduler.
Now am not selecting anyone from first activity and press on the scheduler then it will move to the second activity.In the second activity should show labeled switch on to auto.
If am selecting anyone from first activity then should turn to Manual
Second Activity contains :
Labeled switch in that text contains Auto/Manual
Can any one please help me how to do it.

You can't use mColorTemp.isSelected() to do that. Instead, you has to plug a listener on value changed. If value is changed by user, manual mode can be activated.
mColorTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mManualActivated = true;
}
}
Then, you can use Extras to pass variables from an Activity to another.
How to "put"
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
intent.putExtra("extra_mode", mManualActivated);
startActivity(intent);
Then, to retrieve the value, add this in the SchedulerActivity.onCreate() :
Boolean manualActivated = false;
Bundle extras = getIntent().getExtras();
if(extras != null) {
manualActivated = extras.getBoolean("extra_mode");
}

Related

How to automatically switch to another activity after animation ends

I am trying to do a splash screen in Android Studio. I have an image that I want to fade out (animation). Then, after the animation ends, I want the app to automatically switch to the Main Activity. With my current code, the Main Activity is displaying directly, without going through the animation first. And I don't understand why. I have updated the Android Manifest to specify that I want my Splash Activity to be launched. Still not working:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
fade();
}
public void fade() {
ImageView logo = (ImageView)findViewById(R.id.logo);
logo.animate().alpha(0f).setDuration(1700);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
However, if I remove the last 2 lines (about the Intent), then my animation displays. So it's as if the Intent makes Android bypass my animation altogether.
When you start an animation, that does not cause your code to stop until the animation completes. Instead, each frame of the animation are scheduled over time.
animate() returns a ViewPropertyAnimator, which you should use to register an AnimatorListener using setListener(). When the listener triggers onAnimationEnd(), then call startActivity to proceed.
logo.animate().alpha(0f).setDuration(1700).setListener(new AnimatorListener() {
// implement all the method with empty bodies, but this one is important:
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
};
It looks like you can just set an animation listener like this:
public void fade() {
ImageView logo = (ImageView)findViewById(R.id.logo);
ViewPropertyAnimator anim = logo.animate();
anim.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
anim.alpha(0f).setDuration(1700).start();
}
However, note that it's frowned upon to make dedicated Splash activities like this.
For the "correct" way to do it, see here....
ObjectAnimator fade = ObjectAnimator.ofFloat(logo, View.ALPHA, 0);
fade.setDuration(1700);
fade.addListener(new AnimatorListener() {
...
#Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
...
});
You can try this in your SplashActivity onCreate() method :
ImageView logo = (ImageView)findViewById(R.id.logo);
logo.animate().alpha(0f).setDuration(1700);
new Handler(Looper.getMainLooper()).postDelayed(new Runnable()
{ #Override
public void run()
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
},2000) ;
This will show your animation and after 2 seconds, will navigate to the MainActivity.
Also, since the introduction of MaterialTheme, you can use the Branded Launch Screen instead of creating the SplashScreen. It is very simple to implement and you may refer to this article by Antonio Leiva.

When I switch one activity to another activity then inApp purchase dosen't work

////// in first Activity
#Override
public void onClick(View v) {
if(v==btn){
Intent i= new Intent(ChatActivity.this ,MainActivity.class);
i.putExtra("name",0);
startActivity(i);
}
}
//////////// in second activity oncreate method
Intent i=getIntent();
int name=i.getIntExtra("name",10);
demo(name);
////// in demo method
private void demo(int name) {
if(name==0){
purchase();
}
}
////// in purchase method
public void purchase(){
bp.purchase(MainActivity.this,PRODUCT_ID);
showToast("in purchase Method !!!");
}
inApp purchase all classes include in my application but first statement not working and second is working.If I separate run second class without switch work properly.plz help.
Change your onClick() method as shown below:
#Override
public void onClick(View v) {
if(v.getId()== btn){// this can be also used as v.getId() == R.id.btn
Intent i= new Intent(ChatActivity.this ,MainActivity.class);
i.putExtra("name",0);
startActivity(i);
}
}
Hope this helps.

How to save activity state after navigating?

I have an app with 3 activities,home,calculationResult and help.What I'm trying to do is save the details of the calculations on calculationResult when the user navigates to help.So when the user is in help and presses the action bar back icon,the results of the calculation will still be there in calculationResult.
I have tried to implement this so far by following this guide: Recreating an activity,But when I implemented it the variable I'm wanting to store is not recognized when using with savedInstanceState.Below is how I have tried to do this in the result class.Can someone point out where I have gone wrong with this or if this is the correct way to accomplish saving the activity state?
public class CalcResult extends Activity implements OnClickListener{
TextView result1;
static final String MARK1 = "marking1";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
if (savedInstanceState != null) {
// Restore value of members from saved state
//not recognizing this variable mark1 which I'm setting to the variable that stores the result of the calculation.
mark1 = savedInstanceState.getDouble(MARK1);
}
final Intent intent1=new Intent(this,AboutActivity.class);
final Intent intent2=new Intent(this,MainActivity.class);
final Intent intent3=new Intent(this,MainActivity.class);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.a,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final Button actionBarHome = (Button) findViewById(R.id.action_bar_title);
actionBarHome.setBackgroundResource(R.drawable.ic_action_back);
actionBarHome.setOnClickListener(this);
actionBarHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent2);
}
});
final Button actionBarInfo = (Button) findViewById(R.id.action_bar_staff);
actionBarInfo.setBackgroundResource(R.drawable.ic_action_help);
actionBarInfo.setOnClickListener(this);
actionBarInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent1);
}
});
final Button actionBarHoome = (Button) findViewById(R.id.action_bar_home);
actionBarHoome.setBackgroundResource(R.drawable.appicon);
actionBarHoome.setOnClickListener(this);
actionBarHoome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(intent3);
}
});
result1 = (TextView)findViewById(R.id.markOne);
Intent intent = getIntent();
double markOne = intent.getDoubleExtra("number1", 0);
DecimalFormat df = new DecimalFormat("#.##");
result1.setText(String.valueOf(df.format(markOne)+"mm"));
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
//Also doesn't recognise markOne here ->
savedInstanceState.putDouble(MARK1, this.markOne);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Instead of this
double markOne = intent.getDoubleExtra("number1", 0);
You should write
markOne = intent.getDoubleExtra("number1", 0);
by declaring it again you are not assigning the value to the class level markOne
Also you can try setting the lauchmode of your calculateResult activity as singleTop
android:launchMode="singleTop"
This will use the same instance of the activity that already exist on the top of the stack so will have the same state as before.
Try calling finish in your Help activity when you move to CalculationResult activity.
for ex:
StartActivity(<Intent>);
finish();
onRestoreInstanceState() is called when Activity was killed by the OS. "Such situation happen when:
•orientation of the device changes (your activity is destroyed and recreated)
•there is another activity in front of yours and at some point the OS kills your activity in order to free memory.
Next time when you start your activity onRestoreInstanceState() will be called."
But in your case, this might not happen.
Approach i followed
I set a global varibale to act as a flag if i am launching this activity for the first time. If the global varibale is the same as what i had set, i leave the editText untouched. (in your case, result1). If the value is changed, i set the editText for this value. If the user clicks the editText even once, i track the change and store the value. When you think, the mark1 is no longer needed, you can set the value of flag again as "FIRSTENTRY". This would work.
Kindly try and let us know if you still face issues.
Step 1
Created a class to store a static Global variable.
public class Constants {
public static String sFlag= "FIRSTENTRY";
}
Step 2
Add this piece of code after "setContentView(R.layout.result);" line in your oncreate method. Instead of TextView, i have declared result1 as EditText.
if(!Constants.sFlag.equalsIgnoreCase("FIRSTENTRY"))
{
result1.setText(Constants.sFlag);
}
result1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
Constants.sFlag = result1.getText().toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Constants.sFlag = result1.getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Constants.sFlag = result1.getText().toString();
}
});

Android: Start Activity after 5 or more clicks

I have a onClick listener which starts an activity using intent, but how to make the listener to fire the Activity intent only when the user click five times or more?
public boolean onClick(View v) {
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
return false;
}
Here I am going to login Activity. How can I get back to previous activity after login successful?
public void onClick(View v) {
String username = Username.getText().toString();
String password = Password.getText().toString();
if(username.equals("guest") && password.equals("guest")) {
lResult.setText("Login successful.");
} else {
lResult.setText("Login failed");
}
}
Have a static variable in program which will increment on each click.
When you click count reach 5 then trigger code to start LoginActivity.
static int i = 0;
#override
public void onClick(View view) {
i++;
if (i == 5) {
i = 0;
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
}
You can try to build a counter which count the clicks and from the 5th clicks let him go forward
To turn back to the previous activity just call
finish();
Add a static counter to your activity.
static int clickCount;
In your onClick:
if(clickCount++<5){return;}
For the fist question just a a counter variable on the class and increment in on onClick() and check it its >= 5 before starting the intent.
int clickCounter;
public boolean onClick(View v) {
clickCounter++;
if (clickCounter >= 5) {
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
return false;
}
For the second question you must take into account whether previous Activity must keep exactly the same aspect or update with user data. Take a look at Activity.startActivityForResult (Intent intent, int requestCode) for calling an activity and get a result value from it.
-

Click issue in android

In my application i am displaying rows dynamically.
I am using the below code so as to focus the clicked row even if the user press back key.
But the issue is like if the row is not focussed we need two clicka to navigate.First to make it focussable and then next click to navigate.I need all at once any suggestions.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
flag=v.getId();
if(v.getId()==1)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==3)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==5)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==7)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==100)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
protected void onResume() {
super.onResume();
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
tr[flag].requestFocus();
tr[flag].setFocusableInTouchMode(true);
tr[flag].setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
});
}
#Override
public void onPause() {
super.onPause();
}
Please let me know your valuable suggestions.
Thanks in advance.
If your only need for focus listener is to change background colors, you would do better setting their background to a drawable that has different backgrounds for focused and normal states, then you don't need to control it in code at all. As for requiring two clicks to navigate, I don't see anything in your code about it. It seems to start an activity as soon as a view is clicked once.

Categories

Resources