Stop the sliding screen at the middle - java

HomeActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btnopen = (Button)findViewById(R.id.btnWindowAnimation);
btnopen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent slideactivity = new Intent(HomeActivity.this, CartActivity.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity, bndlanimation);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.home, menu);
return true;
}
HomeActivity has a button and it will help to slide the 1st screen and bring the second screen.
animation.xml
android:fromXDelta="100%p"
android:toXDelta="25%p"
android:duration="5000"
This animation code is helping me to slide my screen after pressing the button. As shown in this video
https://www.youtube.com/watch?v=EzgGGWpRES0
I want to stop the new screen at the middle without covering the 1st screen completely.
Any suggestions? I have used another activity called cart( i haven't used fragments here)

Sounds like you want a NavigationDrawer.
You can find out more information on the Android developer site:
https://developer.android.com/design/patterns/navigation-drawer.html
https://developer.android.com/training/implementing-navigation/nav-drawer.htmlA

Your Requirement is to partially Display an Overlapping Activity on an Existing Activity. This is Possible with Sliding Menu Library (Which does not mean to technically create a floating Activity, but to create a Floating View integrated within the Sliding Menu that will be displayed partially on top on an Activity).
In case, if you are regarding this as some sort of Default Menu then please take a look at this Image
If this is your actual Requirement, then here are the details for you.
Sliding Menu Library is an OpenSource Library, you can use it for displaying a partially visible Sliding Menu on top of an Activity.
Here is the link of Sliding Menu Lib :
https://github.com/jfeinstein10/SlidingMenu
You can navigate to the example directory present in this Project for the demonstration of it's Usage.
I hope thie helps :)

Related

animation on layout with change activity

What I want, when I click on the login button of the first activity the yellow part slides down and the next activity opens. when I click on the signup button of the second screen(login screen) the yellow part of the second screen slides up and the first activity (sign up Activity)opens. I have used slide-down animation on the linear layout on the first screen it works but not working smoothly. Any help??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mSignUpButton = findViewById(R.id.btnSigUp);
linearLayout=findViewById(R.id.linearLayout1);
mGotoLoginActivityButton=findViewById(R.id.btnLoginSignUpActivity);
slideDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
//listener for Login button
mGotoLoginActivityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setValidation();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
linearLayout.startAnimation(slideDown);
}
});
}
you shouldn't use two separated Activities for this purpose, use one and login and create account views should be packed into Fragments. this way will be way easier to animate between two views/fragments in on Activity, but if you really must use Activity then use Transitions (probably with shared elements), not animations, as these are working in one Activity during its runtime when visible (and you are currently running new Activity, which cover old one)

android onclick () not responding

I am new to android programming and trying to develop TIC TAC TOE game. I have created gameLogic() method and the problem is this that it is not working as it is expected to do, means on click of ImageView none of the images is getting displayed. Any help will be highly appreciated.
This is my code:
public class MainActivity extends AppCompatActivity {
public void gameLogic(View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public void onClick (View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500)
Try changing the class name to onClick and go to XML the page where you designs the button and declare onClick method in the image view button.
All you have to do is let the design page know where the code is by providing a common name to look for which is on click.
Also you have to add an onClick listener which listens for the button to click as the code should know which button it is linked to.
So declare a setonclicklistener method on the button Id and you're set to go.
Also write the code within the override after the setting of the layout.
Hope you understood :)
If you're new try head first Android it'll be really helpful I know how struggling it is during the initial days.

Where should I put my ShowCaseView builder in this case?

I have this in my MainActivity.java:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupHomeScreen();
}
public void setupHomeScreen() {
File latestPic = getMostRecentSnappiePicture();
if(latestPic != null){
//display pic
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
else{
layout.setBackgroundDrawable(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
//hide tutorial
findViewById(R.id.howitworks).setVisibility(View.INVISIBLE);
}
else{
//show tutorial
findViewById(R.id.howitworks).setVisibility(View.VISIBLE);
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
}
}
}
As you can see, in onCreate, it calls setupHomeScreen and checks if a file exists. If it doesn't exist, it displays a tutorial "howitworks" layout image as well as building a showcase view.
So this all works fine and well. The only issue comes when trying to leave the activity while the showcaseView is still there, OR sometimes even after you exit the showcase view and try and launch the new activity, this error comes up: ShowcaseView - width and height must be > 0
As you can see in the answers, the solution is to only create the showcase view in the callback after the original view has been created like so:
someView.post(new Runnable() {
#Override
public void run() {
// my ShowcaseView builder here
}
});
The only thing is, I have no idea where to put this, since my showcase view should only show up if the file from getMostRecentSnappiePicture() is null. So how can I put the view creation callback inside of my logic to check that that file is null first?
it looks like you're highlighting the HOME button instead of the 'howitworks' view. Try switching the line
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
with
.setTarget(new ViewTarget(R.id.howitworks,this));
Also, the following video might help. It's 20 minute tutorial on how to use ShowCaseView inside an activity with 3 buttons. He is declaring an onClickListener where he changes programmatically the TargetView highlighted by the showCaseView
https://www.youtube.com/watch?v=3zdeFSBplps
The video is in spanish, but at least you'll be able to follow the steps, since he's writing the code from scratch.

How to change color of background Android Java in Eclipse ADT

I am very new to mobile development, this isn't homework I am working ahead of my class, I developed a simple app that has a button and when it's pressed it shows a message "Hello Android". I would like to build on this and change the color of the background when the onClickListener is called, I will post my code below, I am asking for the best approach to achieve my goal (change background). I want to iterate that this code below works, and that I am not asking for anything to do with the code I have presented, I want to add to it to change the background color (it's currently white, I'm assuming by default). Oh and I have never worked with Java before (very difficult course teaching android/iOS/WinMobile in 1 class). Thank you.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupMessageButton();
}
private void setupMessageButton() {
// 1. Get a reference to the button
final Button messageButton = (Button) findViewById(R.id.helloDroidButton);
//Set the click listener to run my code.
//Code will run when user clicks button.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Anonymous class? --> not sure what he means
Log.i("DemoButtonApp", "Hello Android!");
Toast.makeText(
MainActivity.this,
"Hello Android!",
Toast.LENGTH_LONG
).show();
}
});
}
Android support feature called Selector , that help you to change the background of any view in each state of it like pressed , forces and so one , take look on this useful tutorial and feed me back in any not obvious point
http://www.mkyong.com/android/android-imagebutton-selector-example/
hope it help you

Android: switch to a new fragment inside a fragment?

I have an Android application with a MainActivity and I do this to create navigational tabs in onCreate:
ActionBar myActionBar = getActionBar();
myActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = myActionBar.newTab().setText("FirstTab").setIcon(R.drawable.first_tab)
.setTabListener(new TabListener<FirstTabFragment>(this, "FirstTab",
FirstTabFragment.class));
... more tabs ...
The TabListener, I use this from developer.android.com:
http://developer.android.com/reference/android/app/ActionBar.html#newTab():
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
...
public TabListener(Activity activity, String tag, Class<T> clz) { ... }
}
So if a user clicks on the first tab, my fragment FirstTabFragment is called and executes onCreateView.
My problem is, if a user now clicks a button I do the following and I do not know how to switch to the next fragment:
private OnClickListener firstTabListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v == button1) {
Intent intent = new Intent(???, MyDetailsFragment.class);
startActivity(intent);
} else if (...) {
...
}
}
};
The MyDetailsFragmentshould show details to the selected item (button click), so I want to drill down to the details fragment and want to give some extra data to the new fragment, so the detail fragment knows which details of which selected item it should display.
The selected tab should not change and the back button should go back to the FirstTabFragment page.
I think I should start a new fragment, but it is not possible, the name of the method startActivity tells me that I start a new activity and not a fragment. So, I have to use the MainActivity and put into there the new fragment?
(I do not use the android-support-v4.jar library, because I only target Android 4 devices)
How can I solve this problem? Any ideas?
From the fragment, call a method on the hosting activity, telling it about the button click event. The activity can then do whatever is appropriate.
Personally, I think that totally replacing the contents of a tab is inappropriate in the vast majority of cases, but you are certainly welcome to do it.

Categories

Resources