How to open a link using a CardView - android-studio

What I want to do is to click on a CardView send me directly to the browser with a link already inserted as I can achieve that would greatly appreciate your help :).

Use the following code. Replace it with the id of your cardview in place of your_cardview_id
CardView cardView;
cardView = findViewById(R.id.your_cardview_id);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});

Related

How can I change Snackbar background text color in Android Studio?

My Snackbar background displays the default background I've set on the Theme.xml. So, how do I change the background color of the SnackBar?
Any help would be much appreciated.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNewDeltioBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = binding.tabs;
tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = binding.fab;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar message = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null);
message.show();
}
});
}
Edit:
I've also tried creating a custom style, but that didn't work as expected. It changed the color of the SnackBar sides, not the background itself...
You might have to try message.setBackgroundColor(colorYouWant).
Also, if you have a colors.xml, I would say to make sure that you're correctly naming the colors.
For more information, you could also check out this answer.

Cannot create dialog inside a fragment

I am trying to create a dialog inside a fragment.when I am trying to press on the button and enter the dialog the app collaspe.
I guess the code is not right can you please help me with that?
Here is my code:
private void openDialog(){
Dialog dialog=new Dialog(getContext());
//AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater=this.getLayoutInflater();
View custom_dialog=getActivity().getLayoutInflater().inflate(R.layout.geo_dialog,null);
dialog.setContentView(custom_dialog);
// add_geofence_radius= custom_dialog.findViewById(R.id.radius_size);
save_btn=custom_dialog.findViewById(R.id.save_btn);
cancel_btn=custom_dialog.findViewById(R.id.cancel_btn);
/* save_btn.setOnClickListener(new View.OnClickListener() {
#Override
}
});
*/
/*cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
*/
// dialog.setTitle("hello");
dialog.show();
}
The best way to show dialog in android is to use "DialogFragments" since they are aware of the lifecycle of the view it is attached on (ie. fragments/activities).
Here is an examples provided in Android docs:
public class PurchaseConfirmationDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
.create();
}
public static String TAG = "PurchaseConfirmationDialog";
}
To show the dialog use:
new PurchaseConfirmationDialogFragment().show(
getChildFragmentManager(), PurchaseConfirmationDialog.TAG);
For more reference on dialogFragments, checkout : Create a DialogFragment

I want to make Edit text clickable so that when i click on it, it should show a text like one android studio

this is my code I want to add a text whenever I click on edit text.
`jet1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
jet1.setText("One");
}
});`
To make an editText clickable
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// do things you want
}
});

one tap to display edit text and keyboard android

I'm creating an Android app and i need to add text on a picture.
I need to do a snapchat-like to add my text : I managed to display an editText when you click on the picture but then i have to tap again on the EditText to display the keyboard.
But I need only one click to display the EditText AND the Keyboard.
Thanks for your help.
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
}
});
in case someone is in the same problem, here's the answer :
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
et.setFocusableInTouchMode(true);
imm.showSoftInput(et, 0);
}
});
And for the XML file set the EditText like this :
android:visibility="invisible"

Removing View from getView

I'm trying to hide a button inside getView method of an Adapter. Unfortunately, I can't do it.
private class AppListAdapter extends ArrayAdapter<Info> {
public AppListAdapter(Activity activity, List<Info> apps) {
super(activity, android.R.layout.simple_list_item_1, apps);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// if we weren't given a view, inflate one
if (null == convertView) {
convertView = getLayoutInflater()
.inflate(R.layout.activity_apps, null);
}
btnUninstall = (Button) convertView.findViewById(R.id.uninstallButton);
btnUninstall.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
btnUninstall.setVisibility(View.INVISIBLE);
}
}
);
return convertView;
}
}
Any help shall be appreciated?
Try changing this line
btnUninstall.setVisibility(View.INVISIBLE);
To this
v.setVisibility(View.INVISIBLE);
That's because in the adapter android passes the same view over and over again (recycling), try to set the visibility of the button to visible every time.
I checked the code and it works fine for me.!
Hey quick question though, have you declared your btnUninstall anywhere?
I cannot see it anywhere in the code you have provided, thats all.
Button btnUninstall;

Resources