making android views in runtime - java

I'm importing a windows pairs game (writen in java) to android
but when I run this I can see no buttons...
(currently my code does not seems nice cuz I shoud deliver it to uni fast)
what is going wrong here?
package mgh;
import mgh.mgh.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.RelativeLayout;
public class mghActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new mghActivity();
}
public int numberOfClickes=0;
public card continueCard=null;
public int killedCards=0;
public card selectedCard=null;
public long spentTime=0;
public card[][] card=new card[4][4];
public void mghActivity(){
setTitle("pairs");
//setVisible(true);
//setSize(500,500 );
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AbsoluteLayout lay=new AbsoluteLayout(this);
int[] clr=new int[8];
short[] timesUsed=new short[8];
for (int i=0;i<=7;i++)
timesUsed[i]=0;
for (int i=0;i<=7;i++)
clr[i]= Color.rgb(((int)(Math.random()*2))*255,
((int)(Math.random()*2))*255,
((int)(Math.random()*2))*255);
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++){
int rnd=0;
while (timesUsed[rnd]>=2)
rnd=(int)(Math.random()*8)%8;
timesUsed[rnd]++;
card[i][j]=new card(this,clr[rnd]);
//card[i][j].setEnabled(false);
//AbsoluteLayout.LayoutParams mparams=new AbsoluteLayout.LayoutParams
//(lay.getWidth()/4,lay.getHeight()/4,(lay.getWidth()/4)*i,(lay.getWidth()/4)*j);
int m=40;
AbsoluteLayout.LayoutParams mparams=new AbsoluteLayout.LayoutParams
(m,m,m*i,m*j);
lay.addView(card[i][j],mparams);
card[i][j].setOnClickListener(this);
}
setContentView(lay);
}
public void onClick(View arg0) {
}
}
class card extends Button {///not completed
public int color;
public card(Context context,int color){
super(context);
this.color=color;
setBackgroundColor(color);
}
}

In method onCreate, you just new mghActivity but did not call the the method mghActivity. And mghActivity is not a constructor. You shall remove the void before mghActivity. Then it will be a constructor and called automatically. Even so, I will not recommend creating an Activity like this.

Related

How to use new class wiith media player in main activity?

I'm trying to create new class, becouse I will have there many of sound file, but I don't know how to use it in main ativity. Here is my example code of sound.java:
package app.damian.komunikat_v1;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
public class sound extends MainActivity{
final MediaPlayer sound1 = MediaPlayer.create(this,R.raw.miau);
final MediaPlayer sound2 = MediaPlayer.create(this,R.raw.lol);
}
And now i'm trying to use sound1.start(); in main activity, but i don't know how. Any suggestions?
EDIT:
This is my MainActivity.java:
this is my MainActivity.java:
package app.damian.komunikat_v1;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button guzik = findViewById(R.id.button);
final MediaPlayer sound1 = MediaPlayer.create(this,R.raw.miau);
guzik.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound1.start();
};
});
}
}
I found a different solution. Instead of creating new class with list of sound file, I had created an array of sounds in main activity class.

Disable screenshots in React Native

I know that you can not 100% stop the user from taking a screenshot if he insists to. But I read that you can still stop manual screenshots by setting LayoutParams.FLAG_SECURE in Java.
I tried adding it to my MainApplication file but getWindow() kept on throwing errors no matter what I do. So I moved that line of code to the MainActivity file and it worked without any errors.
Problem is, I can still normally take screenshots.
MainApplication:
package com.testapp;
import android.app.Activity;
import com.reactnativenavigation.NavigationApplication;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import android.support.annotation.Nullable;
/* custom modules */
import com.oblador.vectoricons.VectorIconsPackage;
import org.pgsqlite.SQLitePluginPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends NavigationApplication {
#Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
#Nullable
#Override
public List<ReactPackage> createAdditionalReactPackages() {
return Arrays.<ReactPackage>asList(
new SQLitePluginPackage(),
new VectorIconsPackage(),
new RNDeviceInfo()
);
}
#Override
public void onCreate() {
super.onCreate();
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);
}
}
MainActivity:
package com.testapp;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends SplashActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
}
I did simply the following and it is working properly:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_main);
}
}

Which Mistake in Webview based Android Browser Application?

I'm making an Android Browser Application. Its splash image opens after loading but afterward it crashes. I haven't found what mistake I have in MainActivity.java code.
When i run the code, my default AVD tells me that my application has crashed.
MainActivity.java
package com.example.package;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity
extends Activity
{
private WebView a;
#SuppressLint({"InlinedApi"})
private BroadcastReceiver b = new a(this);
public void a()
{
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setTitle("Google");
localBuilder.setMessage("Are you sure you want to Exit?");
localBuilder.setIcon(2130837505);
localBuilder.setPositiveButton("YES", new d());
localBuilder.setNegativeButton("NO", new e());
localBuilder.show();
}
public void onBackPressed()
{
a();
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
requestWindowFeature(2);
setContentView(2130903042);
this.a = ((WebView)findViewById(2131230720));
this.a.getSettings().setJavaScriptEnabled(true);
this.a.setFocusableInTouchMode(true);
this.a.getSettings().setLoadWithOverviewMode(true);
this.a.getSettings().setUseWideViewPort(true);
this.a.getSettings().setLoadsImagesAutomatically(true);
this.a.loadUrl("http://www.google.com");
this.a.setWebChromeClient(new b(this));
this.a.setDownloadListener(new c());
this.a.setWebViewClient(new f());
}
public boolean onCreateOptionsMenu(Menu paramMenu)
{
super.onCreateOptionsMenu(paramMenu);
paramMenu.add(0, 1, 0, "Home").setIcon(2130837506);
paramMenu.add(0, 2, 0, "Downloads").setIcon(2130837504);
paramMenu.add(0, 3, 0, "Back").setIcon(2130837506);
paramMenu.add(0, 4, 0, "Forward").setIcon(2130837505);
paramMenu.add(0, 5, 0, "Refresh").setIcon(2130837509);
return true;
}
public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
{
if ((paramInt == 4) && (this.a.canGoBack()))
{
this.a.goBack();
return true;
}
return super.onKeyDown(paramInt, paramKeyEvent);
}
public boolean onOptionsItemSelected(MenuItem paramMenuItem)
{
super.onOptionsItemSelected(paramMenuItem);
switch (paramMenuItem.getItemId())
{
default:
return false;
case 1:
this.a.loadUrl("http://www.google.com");
return true;
case 3:
this.a.goBack();
return true;
case 5:
this.a.reload();
return true;
case 4:
this.a.goForward();
return true;
}
}
protected void onPause()
{
super.onPause();
unregisterReceiver(this.b);
}
#SuppressLint({"InlinedApi"})
protected void onResume()
{
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE");
registerReceiver(this.b, localIntentFilter);
super.onResume();
}
}
a.java
package com.example.package;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.Toast;
class a
extends BroadcastReceiver
{
a(MainActivity paramMainActivity) {}
public void onReceive(Context paramContext, Intent paramIntent)
{
Toast.makeText(paramContext, paramContext.getResources().getString(2131034114), 0).show();
}
public static void finish() {
// TODO Auto-generated method stub
}
public static void startActivity(Intent localIntent) {
// TODO Auto-generated method stub
}
}
g.java
package com.example.package;
import android.app.Activity;
import android.content.Intent;
import java.util.TimerTask;
class g
extends TimerTask
{
private Object a;
g(splash paramsplash) {}
public void run()
{
((Activity) this.a).finish();
Intent localIntent = new Intent();
((Activity) this.a).startActivity(localIntent);
}
}
Your problem is almost certainly the issue:
this.a = ((WebView)findViewById(2131230720));
The number "2131230720" is a static reference int to resources, in this case a layout view in your Activity. That number comes from R.java which is created when you "build" your project. Each time you perform a build, it will assign an integer to layout objects, strings, etc. that you define in XML in your res folder.
Using the integer value might work for a while (or is present in source code), but then you change your layouts (or other R objects like strings) or rebuild on a different machine, Android library, etc. and it doesn't work anymore;
That line should look like this:
this.a = ((WebView)findViewById(R.id.my_webview_layout_id));
where "my_webview_layout_id" is from your layout XML file. You should find the "id" of the XML defined object in an XML file in res/layout Use that #+id reference. That will keep track of changes to that static reference.
Then when you build or rebuild your Android app, it will create the R.java file with all of the appropriate references.

Enum's working with .setText in android/java

I am building a Android app in Java, use a Enum variable in a .setText scenario for a XML TextView, i fully understand my Enum is in the wrong data type, but i need your help to work out a alternative to this.
The code where i'm using the setText:
package com.mastermind;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class Guess_database extends Activity {
public ArrayList<Guess> guess_list;
public static int guess_counter = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (Guess g : guess_list) {
View stub = inflater.inflate(R.layout.guess_stub, null);
((TextView) stub.findViewById(R.id.guess)).setText(g.v1 + ", "+ g.v2);
((TextView)stub.findViewById(R.id.guess_positions)).setText(g.c1);
}
}
}
And here is the class for my Enum variable:
package com.mastermind;
import java.io.Serializable;
public class Guess implements Serializable
{
static int v1, v2, v3, v4;
public static GuessStatus c1,c2, c3, c4;
enum GuessStatus
{
CORRECT,
WRONG,
CORRECT_WRONG_PLACE;
}
}
Any help will be appreciated.
((TextView)stub.findViewById(R.id.guess_positions)).setText(g.c1.toString());

Getting the position of a list item in Android

So I have this class here:
package phil.droid.game;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class GameList extends GamesTrialActivity
{
private ListView lv1;
protected String Game_names[]={"God of War","FOS RO DAH", "dhwaud"};
private String Game_pics[]={"God of War","God of War II"};
private int pos;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gamelist);
lv1=(ListView)findViewById(R.id.thegamelist);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , Game_names));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
pos = position;
Intent i = new Intent(GameList.this, game_viewer.class);
startActivity(i);
}
});
}
}
And then this VclassV extends the one ^above^
package phil.droid.game;
import android.os.Bundle;
import android.widget.TextView;
public class game_viewer extends GameList
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_template);
TextView game_title = (TextView)findViewById(R.id.GameTitle);
game_title.setText("" + pos);
}
}
The problem is at the moment that the last bit recognizes "pos" as "0" no matter what option I click on. Can someone suggest a way to make it so pos is recognized as the number element that's clicked on in the previous class?
Make pos protected, not private.
What you're trying to do can't work: The newly launched activity will not share the same storage as the parent, even if they inherit. The only way it would be possible is if the value was static, but that's not a good idea either.
What you should do instead is to send the data as part of the intent before starting the activity, e.g.:
intent.putExtra("pos", position);
and then you can pull it out in the new activity with
getIntent().getIntExtra("pos", -1); // -1 is used as default value
Also, game_viewer should most likely be a separate activity, rather than inherit from GameList.

Categories