Set first visible item in gridview - java

I need to have gridview scrolling to the first item onResume of my activity. It has only the vertical scrolling.
I have tried the following, but it didn't work...
gridView.setSelection(0);
gridView.scrollY(0);
Can someone help please...

I would try gridView.scrollTo(0,0) or gridVIew.smoothScrollTo(0,0), depending on your preference.
Edit: Updated with fixed code.

I have solved my problem!. I have implemented onActivityResult and called the activity for adding a new item, with a known request code. Then upon return, and the result was OK; I use gridView.smoothScrollTo(0), which worked like charm! See code below...
Intent intent = new Intent(this, AddNewActivity.class);
startActivityForResult(intent, REQUEST_ADD_NEW);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_ADD_NEW_CLOTHING_ITEM)
{
gridView.smoothScrollToPosition(0);
}
}
}

Related

not able to make a button visible from another activity

I created an image gallery app.
My requirment:When I click on buttoncut in a activity(PhotosActivity.java), buttonpaste should become visible and it should remain visible when I go back to another activity(ImageGallery.java) so that I can use it for moving pictures to another folder.
What is happening: When I click on buttoncut(PhotosActivity.java), buttonpaste becomes visible but when I go back to any other activity(ImageGallery.java), it disappears.
I tried some code but its not working. How can I fix it ?
PhotosActivity.java
Intent intent = new Intent(PhotosActivity.this, ImageGallery.class);
intent.putExtra(EXTRA_IS_CORRECT, true);
startActivity(intent);
ImageGallery.java
Intent startingIntent = getIntent();
boolean isCorrect = startingIntent.getBooleanExtra(PhotosActivity.EXTRA_IS_CORRECT, false);
if(isCorrect) {
final ImageButton buttonpaste = (ImageButton) findViewById(R.id.buttonpaste);
buttonpaste.setVisibility(View.VISIBLE);
}
but when I go back to any other activity(ImageGallery.java), it disappears
Because you have a different Intent when you "go back". If you want to persist the visibility, you should probably be using SharedPreferences.
For example
ImageButton buttonpaste = (ImageButton) findViewById(R.id.buttonpaste);
SharedPreferences prefs = getSharedPreferences("prefs" Context.MODE_PRIVATE);
boolean isCorrect = getIntent().getBooleanExtra(PhotosActivity.EXTRA_IS_CORRECT, false);
SharedPreferences.Editor e = prefs.edit();
if(isCorrect || prefs.getBoolean(PhotosActivity.EXTRA_IS_CORRECT,false)) {
buttonpaste.setVisibility(View.VISIBLE);
e.putBoolean(PhotosActivity.EXTRA_IS_CORRECT, true);
e.apply();
}
In your main Activity use startActivityForResult instead of startActivity. This allows you to return a value from the new Activity to the Main Activity. For example:
In PhotosActivity
startActivityForResult(new Intent(PhotosActivity.this, ImageGallery.class), 3);
Here, number 3 is a number you define that allows you to identify the result later.
setResult(RESULT_OK); // or any result you want
The result is sent to the MainActivity, and you must override onActivityResult() to get the result.
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == 3) // here you match the number you sent in startActivityForResult
if (resultcode == RESULT_OK)
// do something
}

How to manage onBackPressed from settings activity?

I am calling Settings activity from another activity using the startActivityForResult method. When the back button is clicked, it goes back to the screen on my application. But this activity had already been loaded before calling the settings activity, so I want to be able to refresh the activity on back click on the settings activity. How do I do that?
You will want to implement the onActivityResult() method in your first activity. This method will be called any time an activity you've started with startActivityForResult() finishes.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == yourRequestCode) {
// your code here
}
}
In the case of the BACK button, the resultCode argument will be Activity.RESULT_CANCELED. This doesn't really change things, but lots of examples will include checking for resultCode == Activity.RESULT_OK and I just wanted to mention that it's perfectly fine to do things even when the result code is something else.
Just use
#Override
public void onResume(){
super.onResume();
// your code...
}
In the method implement what has to be needed to be updated.

Android - onCreate()

Is there a way to access to variables on onCreate(), to reuse them in another class?
String testSubCa="";
String prixe="";
String testWiifi="";
Double testPrice=0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_srdata);
testSubCa = getIntent().getStringExtra(SearchRestau.txxtSub);
prixe = getIntent().getStringExtra(SearchRestau.txtprix);
testWiifi = getIntent().getStringExtra(SearchRestau.txxtWifi);
testPrice=Double.parseDouble(prixe);
refresh = (ImageButton) findViewById(R.id.imgRefresh);
refresh.setOnClickListener(this);
}
I need to use these data :/
Since you didn't really specify what exactly your plan is, it's a bit of a challenge getting a proper solution.
I'm just going to assume what you want to do is to start an activity, let the user do something inside this activity and then go back to where it was started from with the ability to use the values created inside the activity.
The easiest way to that is by not starting the activity via startActivity(Intent) but rather by using startActivityForResult(Intent, RequestCode);
In the same activity you then need to handle the event of actually getting a result. To do this you need to implement the method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
//Your code to retrieve the data here!
}
}
The variables should be stored inside the Intent data and can be grabbed by using
data.getStringExtra();
To place the values inside the result you need to place this code in the activity you actually create them:
Intent intent = new Intent();
intent.putExtra("value_id", value);
setResult(RESULT_OK, intent);
finish();
I hope that helps with your problem!
GL

how to destroy activity and show fragment?

I have a MainActivity which has a navigation drawer and a framelayout container to show different fragments. On my navigation drawer there is an option which launches another activity(lockpattern activity) to show a lockpattern (had to use activity cause the library doesn't support fragments yet. Link to Library).Once the user has set up his pattern or canceled the procedure,i want the lockpattern activity to get destroyed and show the previous mainActivity and the same fragment what was there in the container before the lockpattern activity was launched.The problem im facing is that once the backbutton is pressed or even if i call the finish() function,it doesn't show the previous activity(ie Main activity) but instead relaunches the lockpattern activity.i have even tried super.onBackPressed(); and it doesn't seem to work.Any help or ideas to get around this is gratefully accepted.
Code of Lockpattern
public class Create_Pattern extends Activity {
private static final int REQ_CREATE_PATTERN = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// This is your preferred flag
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(this);
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getApplicationContext(), "Pattern Recorded",
Toast.LENGTH_LONG).show();
finish();
}
if (resultCode == RESULT_CANCELED) {
finish();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
You should create a new Intent that points to the Activity you want to navigate to next, start that activity by calling startActivity(intent). Then you can call finish() to destroy the current Activity.
Another approach, (which I have not tried yet) is to create an 'up button' hierarchy and manually call NavUtils.navigateUpFromSameTask(this);
Documentation:
http://developer.android.com/training/implementing-navigation/ancestral.html
http://developer.android.com/reference/android/support/v4/app/NavUtils.html

Activity on users click in Bluetooth discoverability dialog

I am quite new to Java and would appreciate if someone could explain show me
how can I implement startActivityForResult(Intent, int) and
onActivityResult() in Bluetooth discoverability stated
here.
What I want to achieve is: on button Enable BTDisco click
(if(bttn.getId() == R.id.bt_disco) my program calls
BTDiscoverable() method:
public void BTDiscoverable() {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
BT_ENABLE_TIME);
startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
}
Now, the dialog pops up. If user clicks no, the program should not
continue. If user click yes, Enable BTDisco button should become
unavailable and another button, lets call it Start, becomes available. I
wrote onActivityResult which would make Start button available but I
doubt it is done right. Snippet here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
if (requestCode == REQUEST_ENABLE_BT)
{
if(resultCode == RESULT_OK)
{
((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
BTCountDown = new BTTimer(BT_TIME_BTTIME,BT_TIME_BTTIME);
BTCountDown.start();
}
}
}
startActivityForResult is called in BTDiscoverable method but it does
not provide what I wanted. I have followed this but as I am quite
new here, I have no idea how to implement this in such problem as mine.
The onActivityResult method is implemented class that extends Activity.
There are no errors while compiling. Long story, short: R.id.bt_server_start stays disabled as initially programmed.
Could really use some help.
Just put this
if(resultCode != RESULT_CANCELED)
Documentation says the following
Your activity will then receive a call to the onActivityResult()) callback, with the result code equal to the duration that the device is discoverable. If the user responded "No" or if an error occurred, the result code will be RESULT_CANCELED.
So if result code is not RESULT_CANCELED, then you are okay to enable your button.

Categories