Want to show webpage on clicking images in viewpager - android

I want to show webpage when we click an image in viewpager. Can you please help me in this. Also I want to know if multiple images are there in viewpager so how will I get the respective webpages of them.
Thanks in advance.
**MainActivity.java**
package com.example.viewimage;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// open the desired page
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.craftsvilla.com/anvi-s-classic-nawabi-earrings-studded-with-white-stones-and-emeralds.html"));
startActivity(browserIntent);
}
});
}
}

If you want to show this webpage inside your app, you would have to add a WebView. Set its visibility to View.GONE. Add OnClickListener to your viewPager. On click set the visibility of the webView to be View.VISIBLE. To go back, override the functionality of 'back' key - and on its click hide the webpage.
Or you can open other activity that holds the webView.
You can also open the webpage with android's browser.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
startActivity(browserIntent);
Edited :
Add to your xml before closing your RelativeLayout
<LinearLayout
android:id="#+id/cover_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"/>
instead of 'viewPager.setOnClickListener' write
(findViewById(R.id.cover_layout))

Related

Previous activity visible under the new fragment

When I call fragment from activity then fragment calling successfully but some part of previous activity is on the top of in fragment, anyone help to correct the code
package com.example.newproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import static android.view.View.GONE;
public class ThankYouPageActivity extends AppCompatActivity {
Button thank_continue_btn,thank_track_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thank_you_page);
getSupportActionBar().setTitle("Confirmation");
thank_continue_btn = findViewById(R.id.thank_continue_btn);
thank_track_btn = findViewById(R.id.thank_track_btn);
findViewById(R.id.thank_continue_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
HomeFragment fragment = new HomeFragment();
fm.beginTransaction().replace(R.id.thanku_container, fragment).addToBackStack(null).commit();
set background to white in the root tag of your fragment's xml
background = "#FFFFFF"
The container of your Fragment must be overlapped by your Activity layouts. Please attach the layouts of your Activity and Fragment so that I can help you find your issue.
I had same issues , i fixed it by removing previous fragment while setting new fragment in oncreateview
container.removeAllViews(); before replacing the fragment
R.id.thanku_container - I think it is FrameLayout?
Make it full match_parent. Or set background color like activity.

android public textbox

I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html

Android: Actionbar with ListView and GridView

I'm trying to develop an actionbar with three tabs. In the first tab I wanna have a gridview with images and in the other two tabs I wanna have a listview. How can I make this? Is there a code example in the internet? (I haven't find one)
first you need to create TabBar app with Three Tabs then go for List
and Grid view on Tabs.
So I have already created some tutorials to do this.
First Create Three Tabs:
Check this for Tab Bar : TabWidget in Android
For List View: ListView in Android
For Grid View: Grid View Demo With Images
Below is the code how you can create Tabs in Android
ActivityTabWidget.java
package com.rdc;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class ActivityTabWidget extends TabActivity {
private TabHost mTabHost = null;
private Intent ihome, imusic, iabout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create tab host to add tabs
mTabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.main,
mTabHost.getTabContentView(), true);
// create intents to load another page on Tabs
ihome = new Intent(ActivityTabWidget.this, ActivityHome.class);
imusic = new Intent(ActivityTabWidget.this, ActivityMusic.class);
iabout= new Intent(ActivityTabWidget.this,ActivityAboutMe.class);
// create tabs and add to TabHost
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setIndicator(" Home ")
.setContent(ihome));
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator(" Music ")
.setContent(imusic));
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator(" About Me ")
.setContent(iabout));
// set default selected tab
mTabHost.setCurrentTab(0);
}
}
Then you need to create Three Activities
ActivityHome.class
ActivityMusic.class
ActivityAboutMe.class
so the output will be like this..

other way to make start up menu instead using button

i am thinking of doing a start up menu before going into the real page like the picture below.
i think i can do it with button menu. so, the pikpok, all-star, etc i made it using button widget. Is there any other way to make this?
anyway, when user click on all-star button, it appear to the next xml file. but then the activity seems not working out.
this is my code. this code shows the start up activity.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.about);
}
});
button = (Button) findViewById(R.id.signin);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.report);
}
});
}
}
report.xml and about.xml will have their own activity named ReportActivity and AboutActivity but it does not work.
is there a better way to the start up menu instead of using button. also it would be nice if the menu could have animation to it.
For rather complex "Buttons" you can define your own View by using layouts/images/other view
check
http://developer.android.com/guide/topics/ui/custom-components.html
for more information.
To call another activity, you have to define an activity class for that activity first, which calls the layout etc. On how to start another activity read
http://developer.android.com/guide/components/activities.html#StartingAnActivity

Webview buttons stop working after website loads

I have created a simple webview with two buttons below it. If I press one of the button the view seems to create a new view called "web" then the page loads and I still have my buttons below but they don't function. If I use the back button on the phone it takes me to the opening view blank page and the buttons work again? Sorry I am new..
I just want it to load in the original view and have the buttons continue to function.
Do I have to suppress the creation of a new view somehow?
Kind Regards,
-Mike
** and I am not sure why my code always has extra crap when I post it because it does't when I copy it to the clipboard. **
Webscreen Class
package com.example.cam;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Webscreen <URL> extends Activity {
WebView webview1;
public static final String URL = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String turl = getIntent().getStringExtra(URL);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.clearCache(true);
webview1.loadUrl(turl);
}
}
cam Class
package com.example.cam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class cam extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.button1);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.example.cam.Webscreen.URL,
"http://m.google.com");
startActivity(k);
break;
}
}
}
I don't know why are you not using Button class and using View Class.
Use
Button b1=(Button)findViewById(R.id.button1);
instead of
View b1=findViewById(R.id.button1);
Use relative layout for placement of buttons
On button click you are recreating your activity with the intent of displaying the web page/s.
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
it is only creating new activity on top of the new activity
you need to load the web page on button click instead.
//on button1 click
mWebView.loadUrl("http://m.yahoo.com");

Categories

Resources