I am trying to perform the makeSceneTransitionAnimation in my code but it doesn' t seem to work (it tells me that the method cannot be resolved). here is my code:
public class MainActivity extends Activity {
GridView gridview_id;
RelativeLayout second_activity_layout;
public int[] image_id = {R.drawable.banana, R.drawable.kiwi, R.drawable.oatmeal, R.drawable.coconut};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview_id = (GridView) findViewById(R.id.gridview_id);
second_activity_layout = (RelativeLayout) findViewById(R.id.second_activity_layout);
gridview_id.setAdapter(new objectAdapter(this));
gridview_id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent (view.getContext(), SecondActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,(image_id[position]) , getString(R.string.banana_transition));
startActivity(intent, options.toBundle());
}
else{
startActivity(intent);
}
}
});
}
}
to make things a little more clear: I have a GridView in my MainActivity that contains clickable images, when an image is pressed, I want that particular image to display in my SecondActivity.
Please let me know if you have the solution to this, it would really help me out.
-Vidal
You could use the "compat" variant of this method:
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this,(image_id[position]) , getString(R.string.banana_transition));
ActivityCompat.startActivity(MainActivity.this, intent, options.toBundle());
Related
I'm making my first android app using android studio. In this APP I have a listview with 12 classes(12 items). After clicking on one class, it goes into a tabbed activity with 10 items of this class. On each tab page I have a rating bar to let people rate the item.
I set an activity for the listview, and 12 independent activities for those 12 tabbed activities. The code from listview to each tabbed activity is like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0){
Intent intent = new Intent(ListViewActivity.this, TabbedActivity1.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
startActivity(intent);
}
else if(i==1){
Intent intent = new Intent(ListViewActivity.this, TabbedActivity2.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
startActivity(intent);
}
...... // skip the other 10 tabbed activities.
}
Now the problem is: after I finish rating on the tabbed activities, I return to the ListView activity and click into each tabbed activity again, the ratings are gone.
I guess the reason is that in my code, each time I click on the item it opens a new tabbed activity, although same layout, the contents are not saved.
So I was wondering whether I should do something on the ListView activity to save the ratings. I have searched for relevant questions, but I found in their scenarios, each list item is just a simple ratingbar. But here, my list item is a tabbed activity with 10 ratingbars.
Therefore, I have no idea how to do it. I have no experience in android studio, so I don't know where to start to solve the problem. Any idea is appreciated! Thanks a lot in advance!!
First of all if all your tab activities are similar you can just create one activity instead of that many in your case 12 and pass the specific content and states via intent.
The basic approach to your question is would be store rating states in your main activity and when you open your tab activity each time you click the list items send the rate of the relevant activity with intent. Then in your tab activity update the rate with it.
To achieve this we are going to use startActivityForResult instead of startActivity because we need tab activity to return last state of rating bars.
You can see the basic example shown below here:
public class ListViewActivity extends AppCompatActivity {
private static final int REQUEST_RATE = 1;
private SparseIntArray rates = new SparseIntArray();
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ListViewActivity.this, TabActivity.class);
intent.putExtra("styleName", STYLE_NAMES[i]);
intent.putExtra("styleExample", STYLE_EXAMPLES[i]);
intent.putExtra("position", i);
intent.putExtra("rating", rates.get(i, 0));
startActivityForResult(intent, REQUEST_RATE);
}
}
}
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_RATE:
if(resultCode == RESULT_OK) {
//retrieve and save rates
Bundle extras = data.getExtras();
int position = extras.getInt("position");
int rating = extras.getInt("rating");
rates.put(position, rating);
}
break;
}
}
}
public class TabActivity extends AppCompatActivity {
private RatingBar ratingBar;
private int position;
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Bundle extras = getIntent().getExtras();
position = extras.getInt("position");
int rating = extras.getInt("rating");
ratingBar.setRating(rating);
}
#Override protected void onDestroy() {
//send current rating to list activity before we leave
setResult();
super.onDestroy();
}
private void setResult() {
Intent intent = new Intent();
intent.putExtra("position", position);
intent.putExtra("rating", ratingBar.getRating());
setResult(RESULT_OK, intent);
}
}
I'm very new to developing android apps and my programing experience is also lacking a bit.
But I hope some of you can still help me.
Let's get to my problem.
I created a list with images. If I click on one item, I get a Toast saying "you clicked xy". But I want to include a master detail flow. I looked up the internet for some help but all I could find were tutorials on how to integrate a website...
This is my onCreate method, I hope someone could help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
}
});
There are two approaches - using fragments or using activities. Fragments are the preferred way, but harder. Seeing as you are new to android you should probably learn how to do it with activities first. Before you can do that you need to learn about intents.
What you need to do is use the intent to start the detail activity when your item is selected.
Master activity
protected void onCreate(Bundle savedInstanceState) {
...
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//you should make the key string a constant in your code
//im assuming for this example your item is something like a drawable resource id
intent.putExtra("ImageId", getItem(position));
startActivity(intent);
}
});
Detail activity
protected void onCreate(Bundle savedInstanceState) {
...
ImageView iv = (ImageView) findViewById(...);
int imageRes = getIntent().getIntExtra("ImageId", -1);
if (imgRes > 0){
iv.setImageResource(imageRes);
}
}
I've implemented a 'home screen' for my application, which consists of a gridview containing icons and text. This works fine, and I can add an OnItemClickListener so that tapping an icon will create a toast, for example. But I'm not sure how to call startActivityForResult() from here. I could pass in the application context and use this to create the intent etc, but this doesn't feel like the right way of doing it.
My code looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
GridView gridview = (GridView) findViewById(R.id.icons_gridview);
gridview.setAdapter(new HomeScreenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position){
case 0:
//need to start new activity 1 from here
break;
case 1:
//need to start new activity 2 from here
break;
}
}
});
Thanks for any help,
TLB
Method 1 (my prefered method)
Passing ActivityName.this as context is the way I do it. For example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
GridView gridview = (GridView) findViewById(R.id.icons_gridview);
gridview.setAdapter(new HomeScreenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position){
case 0:
Intent intent = new Intent(MyActivity.this, NextActivity.class);
startActivityForResult(intent, 0);
break;
case 1:
//need to start new activity 2 from here
break;
}
}
});
Method 2
You could pass getApplicationContext() as the context;
Method 3
Having a Context mContext field is a common method. Set it at the start of your onCreate then use mContext to start your activities.
private Context mContext;
then
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
mContext = this;
...
}
Then you can start a new activity using mContext as the context parameter
Intent intent = new Intent(mContext, NextActivity.class);
startActivityForResult(intent, 0);
Just use
startActivityForResult(new Intent(MainActivity.this, Activity1.class));
and everything is easy
The context should be the context of the current activity. 'this' would work admirably.
i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.
I've been able to pass a selected image from grid view to a new full screen activity. I am now trying to capture the EXIF data from the image and pass it into a new activity.
The first activity of passing the int from grid view seems to be working fine.
public class test extends Activity {
public static int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thumb);
GridView gridview = (GridView) findViewById(R.id.thumbgridview);
gridview.setAdapter(new tImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(test.this,test2.class);
pos=position;
intent.putExtra("pos", pos);
startActivity(intent);
finish();
}
});}
}
The second activity which displays the full image seems to be working fine.
public class test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full);
Bundle bundle= getIntent().getExtras();
ImageView image = (ImageView) findViewById(R.id.imagefull);
int pos = bundle.getInt("pos");
bundle.getFloat(ExifInterface.TAG_MAKE);
tImageAdapter obj = new tImageAdapter(this);
image.setImageResource(obj.tThumbIds[pos]);
Button bDIR = (Button) findViewById(R.id.bDIR);
bDIR.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(test2.this,Direct.class);
intent.putExtra(ExifInterface.TAG_MAKE, 0);
startActivity(intent);
finish();
}
});
Now when I proceed to the final activity all I am seeing in the text view is the word Make.
public class Direct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newthumb);
Bundle bundle= getIntent().getExtras();
bundle.getFloat(ExifInterface.TAG_MAKE);
TextView textview = (TextView) findViewById(R.id.dirtext);
textview.setText(ExifInterface.TAG_MAKE);
}
}
I am not getting any errors in debug and there hasn't been a single force close issue. Is there something I am missing? I've only been working with java for a couple weeks but this type of activity seems like it should be doable. (or I'm just an idiot)
Thanks!
bundle.getFloat(ExifInterface.TAG_MAKE); does not read anything. You are nowhere actually reading the Exif data from the image file. You simply are showing in the TextView the content of the static String named ExifInterface.TAG_MAKE.
The documentation is available: ExifInterface. You will need to do something like:
ExifInterface exifReader = new ExifInterface(filename);
textview.setText(exifReader.getAttribute(ExifInterface.TAG_MAKE));