I have been trying for some months to get an activity transition to work. I have tried a number of approaches, using XML and styles, and code, to no avail. Below is my most recent attempt. Does anyone know why I can't get any transitions to appear?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.my_activity_layout);
....
}
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
You must start the activity with an ActivityOptions bundle created with makeSceneTransitionAnimation.
That type of transition also only works when in the same task (as of N).
Try to do like this,I am sure this will work for you.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.activity_main);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
}
Try adding the call to addSlideTransitions before super.onCreate.
#Override
protected void onCreate(Bundle savedInstanceState)
{
addSlideTransitions();
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
....
}
Related
I always get an error warning anytime i want to set find view by id
public class MainActivity extends AppCompatActivity {
View pdfView = findViewById(R.id.pdfView);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView.**fromAsset**("").load();
}
The error comes from "fromAsset." It always highlighted in red.
Does anyone have a solution to this?
Please initialize View inside onCreate() Method.
View pdfView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = findViewById(R.id.pdfView);
pdfView.**fromAsset**("").load();
}
I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}
To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!
You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}
Very strange error in code. The part right after the semicolon in setContentView(R.layout.activity_main); is underlined in red. everytime I try to put up code block there. I am pretty sure my brackets add up correctly and there is no syntax error in the xml as well. Then Why is the red underlined ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void function_name(View view){
/* code for the actual program including some declarations. */
}
I don't get it can someone help ?
You have not added } in onCreate method. You should define methods outside of onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void function_name(View view){
/* code for the actual program including some declarations. */
}
You forgot } in onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I have the following code
#SuppressLint("NewApi")
public class Happy_HourActivity extends SherlockActivity{
private ListView listaHP;
private Bundle bundle;
private List<HappyHourModel> listaHpModelResponse;
private HappyHourAdapter adapterHappyHour;
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_happy__hour);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
setSupportProgressBarIndeterminateVisibility(true);
getSupportActionBar().setTitle("Happy Hour");
listaHP = (ListView)findViewById(R.id.list_happy);
bundle = getIntent().getExtras();
and is called from an activity called "descriptionActivity" follows.
case R.id.happy:
intent = new Intent(DescriptionActivity.this, Happy_HourActivity.class);
intent.putExtra("_id", _bundle.getString("_id"));
intent.putExtra("_name", _bundle.getString("_name"));
startActivity(intent);
break;
layout:
but this is what show
This method is wrong for what you want and probably never called
public void OnCreate(Bundle savedInstanceState){
it should be
#Override
public void onCreate(Bundle savedInstanceState){
Small "o". If you would have had the #Override annotation there then your IDE probably would have yelled at you making it easier to diagnose before running.
I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}