cannot open a webpage through imagebutton - android studio - java

the following is my java file for one of the fragments that am using to have multiple image buttons which open multiple weebpages.
please help.
treat me as a noob.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.menu8_layout, container, false);
return rootview;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button bt1 = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://google.com"));
startActivity(myWebLink);
});
}
}
here is the related xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt1"
android:clickable="true"
android:autoLink="web"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="goToSo"
android:text="some" />

Related

How can i call fragment to activity?

I created a progress dialog into a fragment by using this effect. Now i must call it in different activities. When I call the effect the buttons at the background must not work. How can I do that?
First i need to learn how to call the fragment in a different activity. Then i must make the buttons at the background unclickable but there are many of them so ‘setclickabla(false)’ would be a tiring choice.
Progress Dialog Fragment 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProgressDialogFragment">
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<ImageView
android:id="#+id/centerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
</com.skyfishjy.library.RippleBackground>
ProgressDialogFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
return view;
}
For the buttons at the background to respond to clicks, you have to implement the onClick listener of the various buttons.
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<Button
android:id="#+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
In your layout above, you are making use of ImageView which I changed to centerButton and you can make it respond to clicks this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
Button button=(Button)view.findViewById(R.id.centerButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your respond to clicks
}
});
return view;
}
Call your fragment from your activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment(); // your fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}

Xml button field value is always null

I searched many threads but couldn't understand the problem in my code. I am trying to add setOnClickListener functionality on a button from a fragment. It gives me
nullpointer exception at allNews.setOnClickListener(new View.OnClickListener().
Upon debugging I understand that the variable allNews is null.
Below is my layout code.
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id = "#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
And here is my java code:
public class CategoriesFragment extends Fragment {
public Button allNews;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
return rootView;
}
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I am unable to understand what am I missing. There is a scrollview inside the same xml. Should we handle such layouts/fields seperately ?
Any help in this regard is highly appreciated.
Thanks
Satya
SOLVED: I renamed the xml with activity_news_category and then the variable is not null anymore. Prev the xml was named as activity_news_menu. Thnx for all who attempted to help.
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
return rootView;
}
Use set onclicklistener in OncreateView method.Hope this helps
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews = (Button) view.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
UPDATE:
instead of rootView it should be view
Add allNews = (Button) rootView.findViewById(R.id.b_all); in onViewCreated() and remove it from onCreateView()
1) Close the LinearLayout properly.
2) Remove the space between the android:id = "#+id/b_all", so you have:
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
</LinearLayout>
Then rebuild the project.

Start a game loop with button push

I have created an activity PlayActivity that starts a new thread and a game loop. When I have this class as my launcher activity (MainActivity) it works perfectly. But I obviously do not want my app to start with the game loop! In my MainActivity I have different tabs as fragments, in my Home tab, I have a button. I want the game to start with the button clicked. Here is my PlayActivity:
public class PLayActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constants.SCREEN_WIDTH = dm.widthPixels;
Constants.SCREEN_HEIGHT = dm.heightPixels;
try {
InputStream is = getAssets().open("test 3.xml");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(new PlayPanel(this, is));
} catch(IOException e) {e.printStackTrace();}
}
}
Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View inflatedView = getLayoutInflater().inflate(R.layout.home, null);
basic = (Button) inflatedView.findViewById(R.id.basic);
intermediate = (Button) inflatedView.findViewById((R.id.intermidiate));
basic.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg) {
// Start NewActivity.class
Intent basicIntent = new Intent(MainActivity.this, PLayActivity.class);
startActivity(basicIntent);
}
});
Home.java:
public class Home extends Fragment {
Button basic;
Button intermediate;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.home, container, false);
return view;
}
}
and home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:text="Basic"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/basic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Intermidiate"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/intermidiate"
android:layout_below="#+id/basic"
android:layout_centerHorizontal="true" />
<Button
android:text="Advanced"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/advanced"
android:layout_below="#+id/intermidiate"
android:layout_centerHorizontal="true" />
</RelativeLayout>
When I click the button nothing happens! What should I do?

android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView

Good afternoon,
I am trying to create a fragment with a RecyclerView including items.
Above those items, I need to put a header with some filters.
Here is the fragment's xml (fragment_products_list) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12sp"
android:id="#+id/filters_frame">
<include layout="#layout/expandable_filters"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Here is the OnCreateView in my Fragment java file (ListFragment.java) :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
expandFiltersButton = (ImageButton) getView().findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) getView().findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return rv;
}
I have this error code when I am trying to run the app :
android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView
What is the problem and how can I solve it?
Thanks for your help!
Try this:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
expandFiltersButton = (ImageButton) view.findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) view.findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return view;
}

DialogFragment: button.setOnClickListener throws NullPointerException

I have an Activity with a button like this:
<RelativeLayout 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">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClickListener"
android:text="Show Fragment" />
</RelativeLayout>
And my onButtonClickListener method from xml is(here I create the DialogFragment and set onClickListener for a clear button from dialog):
public void onButtonClickListener(View view) {
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
final EditText messageText = (EditText) findViewById(R.id.editText);
Button clearButton = (Button) findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
}
My problem is that when DialogFragment shows it throws NullPointerException:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cristi.dialogfragmenttest2.MainActivity.onButtonClickListener(MainActivity.java:51)
At this line of code: clearButton.setOnClickListener(new View.OnClickListener() {
My xml for DialogFragment is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLength="200" />
<Button
android:id="#+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
What could be the problem with the setOnClickListener? Why does it says that my button from DialogFragment is null?
Any reference or advice is welcome! Thanks in advance!
Edit:
-MyDialogFragment code:
public class MyDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
return rootView;
}
You must have refernece to the root view where is your button button_clear, then call rootView.findViewById(...) to get the button reference...
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
Button clearButton = (Button) rootView.findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
return rootView;
}

Categories