How to switch to another activity using imageview in reycleview? - java

So, I am basically building an expense tracker app. I have successfully implemented Recycleview which contains Transactions' history. Now, what I am trying to achieve is to use an arrow imageview in the recycleview which will forward me to another activity when it is clicked. Any help will be appreciated on it.

in your adapter class inside onBindViewHolder method add
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, OtherActivity.class);
context.startActivity(intent);
}
});

Related

How to open different url in single web view android studio

How do I open different different URL stores in different different buttons. I'm just creating a separate activity that contains web view .
For example, one button contains links of Google. Another is containing links of Facebook, and another is Gmail.
Now I don't want to create separate activity for each click - I just want to create a activity that contains web view and when someone clicks the button they get inside this activity and the provided link will open with custom loading view
Help would be appreciated.
just create the method in the activity in which u have to show webView, which takes a string as a input and use this string in place of URL of webView,
call this function in the onClick of each button and pass the URL of desired website as parameter, do this for all the buttons
You can send the url of the website to the webview activity using intent and receive them accordingly!on each button click add you
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(this, webview.class);
intent.putExtra("URL","your url");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(this, webview.class);
intent.putExtra("URL","your url2");
startActivity(intent);
}
});
on your weview activity recive these urls and fire your webview as follows
String url=getIntent().getExtras().getString("URL");

can write OnClickListener into parceable?

Hello I have a trouble with OnClickListener
View.OnClickListener listener= new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(Conversations.this, "click on MSG",
Toast.LENGTH_SHORT).show();
}
};
========================================================================
I Need put this listener into Fragment which will parceable.
I not parceableing listener but after readvalue is null and when click app crasher because it's reference on null object.
now I need find some methods which can use for save and read object (View.OnClickListener) if parceable or something similar.
Without this I need rebuild my project :(
Please Help me.
Thanks
_______________________________________________________--
I haven't have fragment in fragment I want all fragment put into "extra" and in other activity read from extra..
Intent i= new Intent(Conversations.this,MessagesSingleFragmentActivity.cla‌​ss); i.putExtra("Fragment1", (Parcelable) recycleMessage); startActivity(i);
and in other activity have
Intent extras = getIntent(); Fragment fragment = (Fragment) extras.getParcelableExtra("Fragment1"); fm.beginTransaction().replace(R.id.fragment_container, fragment).commit();
No you cannot pass OnClickListener as Parcelable but , if you want to observe click from one fragment to another , then you have to implement the onclick in second fragment.
FragA.java:// who have onClickListener
Runnable run;
public void setClick(Runnable run){
this.run = run;
}
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// do something in onclick
if(run!=null){run.run();}
}
});
FragB.java:
((FragA)getParentFragment()).setClick(
new Runnable() {
#Override
public void run() {
// do something in other fragment
}
}
)
Updated:
If you want to perform something onClick of ActivityA on ActivityB , then EventBus are best options for that use Otto or EventBus by GreenBot
It is recommended to interact between fragments via Activity.. or more generally by interface which Activity conforms to.
One more thing: If you have common functionality in both fragments, maybe it is better to extract it to Activity?

Removing Activity from BackStack (Flag_Activity_no_history) does not work

I've a Start button in my MainActivity. If I click on this button I go to the next Activity (InfoActivity). Now, I want to remove the MainActivity from the BackStack, if I click on the button. I've tried this:
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
};
But that removes not the MainActivity from the BackStack, it removes the InfoActivity from the BackStack.
I know that I could insert the flag into the AndroidManifest. But that is not possible for me, because if I go to the PreferencesActivity from the MainActivity and I using the flag in the AndroidManifest, the MainActivity is removed if I return from the PreferencesActivity back to the MainActivity.
So I want to remove the MainActivity from the BackStack, if I click on the button Start.
Add finish();
Like this :
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
this.finish();
}
};
As said you should use Intent.FLAG_ACTIVITY_NO_HISTORY on the activity you dont want to track in your case on MainActivity.
Other way to do it is Intent.FLAG_ACTIVITY_CLEAR_TOP on your Info activity this will clear whole history (even things before MainActivity)

How do I get a button to open another activity?

I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?
A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="#string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});
Using an OnClickListener
Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.
Button yourButton = (Button) findViewById(R.id.your_buttons_id);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
});
This is probably most developers preferred method. However, there is a common alternative.
Using onClick in XML
Alternatively you can use the android:onClick="yourMethodName" to declare the method name in your Activity which is called when you click your Button, and then declare your method like so;
public void yourMethodName(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
Also, don't forget to declare your new Activity in your manifest.xml. I hope this helps.
References;
Starting Another Activity (Official API Guide)
If you declared your button in the xml file similar to this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity"
android:onClick="goToActivity2"
/>
then you can use it to change the activity by putting this at the java file:
public void goToActivity2 (View view){
Intent intent = new Intent (this, Main2Activity.class);
startActivity(intent);
}
Note that my second activity is called "Main2Activity"
Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
startActivity(i);
}
});
Write code on xml file.
<Button android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/button"
android:text="Click"/>
Write Code in your java file
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Secondclass.class));
/* if you want to finish the first activity then just call
finish(); */
}
});
use the following code to have a button, in android studio, open an already existing activity.
Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);
StartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
}
});
I did the same that user9876226 answered.
The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"
open is the function, that is bound to the button.
Then just create the function open() in your activity class. When you click on the button, this function will be called :)
Also, I think this way is more confortable than using the listener.
Apply the following steps:
insert new layout xml in folder layout
rename window2
add new button and add this line: android:onClick="window2"
mainactivity.java
public void openWindow2(View v) {
//call window2
setContentView(R.layout.window2);
}
}
Use following steps to add the new activity (Manifest file will be automatically update)
File > New > Activity > Empty Activity
In your MainActivity.java file add the following code inside protected void onCreate(Bundle savedInstanceState).
Make sure you call finish(); function at the end. So when you tap the Back button, it will not go back to the previous activity.
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
finish();
}
});

Why overridePendingTransition does not take effect when I jump from Fragment to a new Activity?

The first activity contains a Fragment, and there's a button inside the fragment, and when I click this button, I wish to jump to a new Activity with different animation. So I do it like this in the Fragment:
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), NewActivity.class);
getActivity().startActivity(intent);
getActivity().overridePendingTransition(
R.anim.forward_activity_move_in,
R.anim.forward_activity_move_out);
});
However the new animation does not take effect when moves to the new Activity, so I wonder maybe I do it wrong. I hope someone can help me, thx a lot:)
Thanks, I solved this problem, You can find solutions at Change activity transition when inside a TabHost
Well the problem is not the case of Fragment, instead the TabActivity causes the problem.

Categories