Followed a tutorial here to create a navigation drawer and worked fine. The webview also loads when the app starts.
My Landing page loads when the app starts:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.activity_main_webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("http://192.168.43.105/public_html/central/updates.php");
myWebView.setWebViewClient(new WebViewClient());}
Url should open when a navigation item was selected:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_search) {
myWebView.loadUrl("http://192.168.43.105/public_html/central/search.php");
return true;
}
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
//return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
Drawer Items is located at res>menu>navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="HardcodedText">
<item
android:id="#+id/nav_search"
android:title="Search" />
</menu>
Tried:
String url = "http://192.168.43.105/public_html/central/search.php";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
And followed this from developer.android.com:
switch (item.getItemId()) {
case R.id.nav_search:
String url="http://192.168.43.105/public_html/central/search.php";
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
return true;
default:
return super.onOptionsItemSelected(item);
}
I didn't get any error when starting the app. Any help would be greatly appreciated!
For external URL try this:
Uri uri = Uri.parse("https://www.google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
If you want to web browser open in your activity try this:
WebView webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webView.loadUrl(URL);
and If you want to use the zoom control in your browser then add this also:
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
Related
When searching for an item, I'm able to retrieve the Title, Artist, and Image for the respective Songs. However, the first 2 items displayed in the ListView when clicked on keeps returning the last song in my list instead of the correct song that is being displayed.
E.g, When clicking on "Life is Good" or "Good as hell", the song "First Time" will be displayed instead. However "Perfect", "Lemon", and "Autopilot" are working fine. These are the only codes used to search! SearchByTitle and GetTitle() are taken from the files where the songs are hardcoded in!
My Activity:
ListView listView;
ArrayList<String> stringArrayList = new ArrayList<>();
ArrayAdapter<String> adapter;
private SongCollection songCollection = new SongCollection();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listView = findViewById(R.id.list_view);
stringArrayList.add("Life is Good");
stringArrayList.add("Good As Hell");
stringArrayList.add("Perfect");
stringArrayList.add("Lemon");
stringArrayList.add("Autopilot");
stringArrayList.add("First Time");
adapter = new ArrayAdapter<>(SearchActivity.this, android.R.layout.simple_list_item_1, stringArrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),
adapter.getItem(i), Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String value = listView.getItemAtPosition(i).toString();
Song selectedSong = songCollection.searchByTitle(value);
AppUtil.popMessage(view.getContext(), "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
XML to my Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
tools:context=".SearchActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#404040">
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#FFFFFF"
android:divider="#color/white"
android:dividerHeight="2dp"
tools:listitem="#android:layout/simple_list_item_1">
</ListView>
</LinearLayout>
</RelativeLayout>
It happens because you need to create a new list which would contain the search results, you don't have to do the search operation with your original list which isstringArrayList
Instead create a new list which is mylist in this case, and store the search results and call that list in onQueryTextChange
Create this method, I modified the code according to your code
private void search(String str) {
ArrayList<String> myList = new ArrayList<>();
for (Quote object : stringArrayList) {
if (object.GetTitle().toLowerCase().contains(str.toLowerCase())) {
myList.add(object);
}
}
AdapterClass adapterClass = new AdapterClass(this, myList);//, swiper
recyclerView.setAdapter(adapterClass);
}
Now, call this method under search setOnQueryTextListener, like this
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
OPTIONAL
And one more modification which you can do is you can check setOnQueryTextListener when ever there is some text in it, like this
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(s);
return true;
}
});
}
If my answer solves your problem you can give me an up-vote and mark my answer as correct, by clicking on the gray tick button beside the starting of my answer, which will then turn green!
I am currently implementing a back button action for navigating webview history. My webview has been working(including handling opening links within the webview). However, after adding the back button action it crashes whenever I try to use the back button. This is my Main Activity Code:
public class MainActivity extends Activity {
WebView mWebView;
//Back Button Code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(amapps.com.uhss.R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(false);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://uhsswordandshield.com/");
mWebView.getSettings().setSupportMultipleWindows(true);
}
Could someone please tell me what is wrong with the code. I believe it has to do with my
WebView mWebView;
declaration before I initialize the webview. But I am not sure of any way to handle webview history navigation. Also, I am unsure of how to post my log cat and what to post so if someone could please tell me which part of the logcat I need to post that would be great. Thanks!
try this code:
if((keyCode==KeyEvent.ACTION_DOWN)){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
This is my CameraView.java (I got it from http://blog.rhesoft.com/)
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera){
super(context);
mCamera = camera;
mCamera.setDisplayOrientation(90);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
if(mHolder.getSurface() == null)
return;
try{
mCamera.stopPreview();
} catch (Exception e){
}
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}}
and this is my MainActivity.java.
public class MainActivity extends AppCompatActivity {
private Camera mCamera = null;
private CameraView mCameraView = null;
private FrameLayout camera_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
mCamera = Camera.open();
//you can use open(int) to use different cameras
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if(mCamera != null) {
mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
//btn to close the application
final ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
final ImageButton capImg = (ImageButton) findViewById(R.id.imgCapture);
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imgClose.setVisibility(View.INVISIBLE);
capImg.setVisibility(View.VISIBLE);
mCamera.startPreview();
}
});
capImg.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
mCamera.stopPreview();
imgClose.setVisibility(View.VISIBLE);
capImg.setVisibility(View.INVISIBLE);
}
});
}
#Override
public void onBackPressed(){
System.exit(0);
}}
and this my activity_main.xml.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgClose"
android:layout_gravity="right|top"
android:background="#android:drawable/ic_menu_close_clear_cancel"
android:padding="20dp"
android:visibility="invisible" />
<ImageButton
android:layout_width="98dp"
android:layout_height="98dp"
android:id="#+id/imgCapture"
android:layout_gravity="center_horizontal|bottom"
android:background="#android:drawable/ic_menu_camera"
android:padding="20dp"/>
Can I capture this FrameLayout preview as image or do some programing with that preview like delete red color? Can you give me some clue?
So if I understand correctly, you wish to get the image data that is shown when you stop the preview? If you so you can mCamera.takePicture() method. It takes 3 parameters, all of which are useful callbacks. Here is something I recently did to show you.
btn_Capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mCamera == null)
return;
mCamera.takePicture(null, null, mPicture);
}
});
This is my button click listener which is a floating image button (any button will work just fine). The third parameter is a callback that returns an array of pixels that you can convert into a bitmap.
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
preview.removeView(mPreview);
img_Captured.setImageBitmap(bMap);
}
};
This is the callback which I passed in the takePicture() method. byte[] data is the image that you are trying to get. As you can see I converted it into a bitmap and displayed it to an ImageView after removing the surfaceview (which holds the camera preview). Just a note, the takePicture() method stops the preview automatically so don't stop the preview before taking the photo. You can do it how I did it in the callback. Also, if you want to take another photo, you can start the preview again.
I hope this helps!! Let me know if I left anything out! By the way, it is all documented on the Android Developer site.
http://developer.android.com/training/camera/cameradirect.html#TaskTakePicture
I'm working with fragments (API 4.1) and I want to let the user press a button, access his/her gallery, select an image, and have that image display an imageview on the original fragment where the button appeared. I'm using the following code:
public class FillBox1Frag extends Fragment {
Button addPics, placeBox;
ImageView imgView;
Bitmap b;
Uri photoUri;
LinearLayout fillBoxLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
fillBoxLayout = (LinearLayout) inflater.inflate(R.layout.fillbox1_frag,
container, false);
newBin = new Bin();
addPics = (Button) fillBoxLayout.findViewById(R.id.bPics);
imgView = (ImageView) fillBoxLayout.findViewById(R.id.imageView1);
addPics.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent picChooser = new Intent(Intent.ACTION_GET_CONTENT,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
picChooser.setType("image/*");
startActivityForResult(picChooser, 12345);
}
});
return fillBoxLayout;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 12345:
if (resultCode == 12345) {
photoUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = FillBox1Frag.this.getActivity()
.getContentResolver()
.query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
b = BitmapFactory.decodeFile(filePath);
imgView.setImageBitmap(b);
}
}
}
I access the Gallery, but upon selection, it just returns to the original fragment and doesn't display the image. Here's my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fillbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="fillbox" >
<Button
android:id="#+id/bPics"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp"
android:background="#drawable/buttonpics" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I've found plenty of answers dealing with a similar question, but nothing that has been able to help me with how to create this process using fragments. Any ideas on how to get my selected image to appear in the ImageView? Thanks for the help!
In my application I am implementing 5 different Custom App Themes.
I am using 5 different fonts for these themes for which I have created CustomTextview which extends a Textview. Following is the format in which it is created.
public class CustomFontTextView extends TextView {
private static final String CUSTOM_FONT = "Custom-Regular.ttf";
private static final String TAG = CustomFontTextView.class.getName();
public CustomFontTextView(Context context) {
super(context);
if (android.os.Build.VERSION.SDK_INT < 14) {
setCustomFont(context, CUSTOM_FONT);
}
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (android.os.Build.VERSION.SDK_INT < 14) {
setCustomFont(context, CUSTOM_FONT);
}
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (android.os.Build.VERSION.SDK_INT < 14) {
setCustomFont(context, CUSTOM_FONT);
}
}
public boolean setCustomFont(Context ctx, String fontFile) {
try {
setTypeface(Typeface.createFromAsset(ctx.getAssets(), fontFile));
return true;
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
}
}
I was using the custom textview in the XML layouts.
<com.myapp.android.fonts.CustomFontTextView
android:id="#+id/Text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_text_content"
android:textColor="#android:color/white"
android:textSize="22sp" />
But for implementing the different themes I have to find some methods which would change all my textviews according to the themes selected.
Is there any way by which I can set the fonts within my theme,
<style name="MyCustomTheme" parent="android:style/Theme">
.............. ......................
........... ...................
<item name="android:textColorPrimary">#000000</item>
<item name="android:buttonStyle">#style/MyCustomButton</item>
</style>
I want to implement this without disturbing the code as there are a lot of UI components involved.
Create a custom attribute for the font and include it in the style. In your CustomView class, access this attribute and set the font accordingly.
Check Creating a View Class for more details.