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.
Related
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());
Here's my code
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(this,lastview.class);
startActivity(i);
}
});
the "this" is a ListActivity,but I want the next activity is a normal activity
and my code is wrong in this line
Intent i = new Intent(this,lastview.class);
the wrong message is
The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<lastview>) is undefined
how can I fix it ?
change this line
Intent i = new Intent(this,lastview.class);
like this, change your activity to MyListActivity
Intent i = new Intent( MyListActivity.this, lastview.class);
Here is the sample code .This will help you.
public class YourListActivity extends ListActivity {
private Context context;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
context =this;
.....
......
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(context,lastview.class);
// else you can use the code like below
//Intent i = new Intent(YourListActivity.this ,lastview.class);
startActivity(i);
}
});
}
You can also write it like :
startActivity(new Intent(Yourclass_name.this,lastview.class));
I have created an android application. In that application when I click the listview item it should display in the another listview in the same layout.
Is this possible in android?
Well, quick snippet:
public Activity1 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
// ListView event
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedString", listView.getItemAtPosition(position));
startActivity(intent);
}
});
}
}
public Activity2 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
String valueFromActivity1 = getIntent().getString("SelectedString");
// ok now, u've got value from Activity1, do whatever w/ it
}
}
No you have to make intent and pass the variables of the current selected item of the listview to that intent and display the dynamic listview for that Item
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));