Start a game loop with button push - java

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?

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();
}
}

How to create a transparent toolbar and status bar and overlay an image for this class?

I have an xml layout file called fragment_about_sl.xml and the class associated to that called aboutSLFragment.java which is extended to fragment.java. Anyway all I want to do is make the toolbar transparent and overlay the image for this class only.I am new to android, so please help.
fragment_about_sl.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/aboutslscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.navigationdrawerfragments.aboutSLFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="250dp" />
<LinearLayout
android:id="#+id/SliderDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/viewPager"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
aboutSlFragment.java
public class aboutSLFragment extends Fragment{
ViewPager viewPager;
LinearLayout sliderDotspanel;
private int dotscount;
private ImageView[] dots;
TextView aboutsltext;
Button readmorebtn;
public aboutSLFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_sl, container,
false);
viewPager = (ViewPager) rootView .findViewById(R.id.viewPager);
sliderDotspanel = (LinearLayout)rootView.findViewById(R.id.SliderDots);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getContext());
viewPager.setAdapter(viewPagerAdapter);
dotscount = viewPagerAdapter.getCount();
dots = new ImageView[dotscount];
for(int i = 0; i < dotscount; i++){
dots[i] = new ImageView(getContext());
dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(),
R.drawable.non_active_dot));
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderDotspanel.addView(dots[i], params);
}
dots[0].setImageDrawable(ContextCompat.getDrawable(getContext(),
R.drawable.active_dot));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for(int i = 0; i< dotscount; i++){
dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(),
R.drawable.non_active_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getContext(),
R.drawable.active_dot));
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
aboutsltext=(TextView)rootView.findViewById(R.id.aboutsltext);
readmorebtn=(Button)rootView.findViewById(R.id.readmorebtn);
readmorebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (readmorebtn.getText().toString().equalsIgnoreCase("Read More"))
{
aboutsltext.setMaxLines(Integer.MAX_VALUE);//your TextView
readmorebtn.setText("Read Less");
}
else
{
aboutsltext.setMaxLines(3);//your TextView
readmorebtn.setText("Read More");
}
}
});
// Inflate the layout for this fragment
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments
different titles
getActivity().setTitle("About Sri Lanka");
}
I want my app to be like this
You can do that in onResume of that fragment:
#Override
public void onResume() {
super.onResume();
AppCompatActivity activity = (AppCompatActivity) getActivity();
ActionBar actionBar = activity.getSupportActionBar();
actionBar.hide();
}

Android scaled Button not clickable

I created a fragment which contains a FrameLayout containing a few buttons:
<FrameLayout android:id="#+id/menu_view"
android:layout_width="490dp"
android:layout_height="490dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="-200dp"
android:layout_marginEnd="-200dp"
android:layout_marginBottom="-200dp"
android:background="#drawable/menu_view_bg">
<Button android:id="#+id/menu_btn1"
android:layout_width="76dp"
android:layout_height="76dp"
android:paddingTop="15dp"
android:paddingBottom="10dp"
android:layout_marginLeft="185dp"
android:layout_marginStart="185dp"
android:layout_marginTop="107dp"
android:background="#drawable/menu_btn_bg"
android:drawableTop="#drawable/menu_item0"
android:textColor="#color/colorPrimaryDark"
android:textAllCaps="false"
android:text="#string/menu1"/>
<Button android:id="#+id/menu_btn2"
android:layout_width="76dp"
android:layout_height="76dp"
android:paddingTop="15dp"
android:paddingBottom="10dp"
android:layout_marginLeft="200dp"
android:layout_marginStart="200dp"
android:layout_marginTop="15dp"
android:background="#drawable/menu_btn_bg"
android:drawableTop="#drawable/menu_item1"
android:textColor="#color/colorPrimaryDark"
android:textAllCaps="false"
android:text="#string/menu2"/>
...
</FrameLayout>
In my fragment class, I scale the menu on a click on a Button outside of FrameLayout but when I try to click on buttons inside it, it won't work. If I don't scale the view at start, it works.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FrameLayout menuView = (FrameLayout) getView().findViewById(R.id.menu_view);
menuView.setScaleX(0.1f);
menuView.setScaleY(0.1f);
Button btn = (Button) getView().findViewById(R.id.menu_main_btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleMenu();
}
});
Button hotelBtn = (Button) getView().findViewById(R.id.menu_btn2);
hotelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("fef", "onClick: dodkoz");
}
});
}
Specifically, if I comment these two lines, it will work (I set the onclick in my fragment class, not in XML):
menuView.setScaleX(0.1f);
menuView.setScaleY(0.1f);
I guess this has something to do with hit area but I can't find what exactly. I'm fairly new to Android dev so I haven't come up with a workaround. Thanks!
you can use this.
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.yourframelayout, container, false);
Button btn = (Button) view.findViewById(R.id.menu_main_btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// toggleMenu();
}
});
Button hotelBtn = (Button) view.findViewById(R.id.menu_btn2);
hotelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("fef", "onClick: dodkoz");
}
});
}

cannot open a webpage through imagebutton - android studio

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" />

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