Android Studio: Font changing error - java

So I have two objects on my activity, a TextView, and a Button. I did change the font of both of these using a font style from my assets folder. The project would load fine no problems. However now all of a sudden changing the TextView font makes the game crash and not load. I can't understand what could have caused this and how to solve it other than maybe using a button to display text, which is in practical.
My java code:
public class Main_Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
Button n=(Button) findViewById(R.id.start_button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
n.setText("Start");
n.setTypeface(typeface);
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
TextView title = (TextView) findViewById(R.id.title);
title.setTypeface(typeface2);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
}
I did use the typeface to change both texts fonts but I tried just making it again with typeface2 and it still crashes. Not sure if I need to show any other part of my project but will if you wish to see it. Thank you for the help.

It seems like you forgot "setText" to the title

Related

Android: How to call a different layout for 2 devices (tablet and phone)

I have an app that I want to run on both a phone and a tablet. I have 2 separate layouts (landscape) for both which are essentially the same except for their dimensions.
My question is how do I make the MainActivity.java detect the device that I'm running on and accordingly fire the activity based on the device being used.
I have looked into fragments but I couldn't really understand how to create them nevermind how to use them.
This is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
Intent intent;
FirebaseDatabase rootNode;
DatabaseReference reference;
public void displayScore() {
intent = new Intent(getApplicationContext(), Score.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
final FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
name = findViewById(R.id.editTextTextPersonName);
oneStar = findViewById(R.id.imageView1);
twoStar = findViewById(R.id.imageView2);
threeStar = findViewById(R.id.imageView3);
fourStar = findViewById(R.id.imageView4);
fiveStar = findViewById(R.id.imageView5);
oneStar.setTag(1);
twoStar.setTag(2);
threeStar.setTag(3);
fourStar.setTag(4);
fiveStar.setTag(5);
class OnClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, view.getTag().toString());
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.push().setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
sleep(2500);
displayScore();
}
}
}
oneStar.setOnClickListener(new OnClickListener ());
twoStar.setOnClickListener(new OnClickListener ());
threeStar.setOnClickListener(new OnClickListener ());
fourStar.setOnClickListener(new OnClickListener ());
fiveStar.setOnClickListener(new OnClickListener ());
}
}
Any help would be appreciated.
Thanks in advance.
You should not be looking at two different activities for tablet and phone. There are different resource layout folders for this exact purpose. You create a layout for landscape in layout-land and your portrait is just in the normal layout folder. Then you have just one activity and the OS chooses the layout to use based on the screen and orientation
Your layout should be flexible to adapt to different sizes. You should have only a single layout for all sizes, avoid hardcoded sizes.
This is an excerpt from https://developer.android.com/training/multiscreen/screensizes
"Avoid hard-coded layout sizes. To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes.
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view."

Unable to see textview widget on screen when i write code in java?

Unable to see textview widget on screen when i write code in java
**#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConstraintLayout Layout=new ConstraintLayout(this);
Button bt=new Button(this);
TextView tv=new TextView(this);
Layout.addView(bt);
Layout.addView(tv);
setContentView(Layout);
}}**
When you create a view by calling its constructor and passing only a Context object, you're going to get the simplest default version of that view. A default TextView has no text, and is therefore invisible for all intents and purposes. To be able to see it, you will have to do something that makes it visible, like add text or define a width + add a background color.
You can see the Button because a default button has a minimum width and a background applied by default.

Android get page source inside loop

I am new to Android programming. When my app starts it crashed.maybe this is a loop problem.app works fine when i remove code which get source from page
is it possible to get page source inside loop, for example every minute?
This is one of the most frequent errors observed here
With this line
TextView mTextView = (TextView) findViewById(R.id.textView3);
you are trying to access a view before the content view has been set. So you'll have to move this line like
private MediaPlayer mediaPlayer;
TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security_system);
mTextView = (TextView) findViewById(R.id.textView3);

How does setContentView work in android?

I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.

Setting an Image to ImageView dynamically

Please forgive me if this is a duplicate post but I tried and could not find anything on this subject.
I have a blank ImageView in a layout and now I want to put an image there dynamically. Since there is
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText("my text");
is there a way to do it for an ImageView like the way you would do it for a TextView?
ie...
ImageView image = (ImageView) v.findViewById(R.id.pPicture);
image.setImage(R.drawable.myImage); // I know this isn't correct.
Any help is much appreciated.
Try this
image.setImageResource(R.drawable.yourimage);
or
image.setImageDrawable(getResources().getDrawable(R.drawable.yourimage);
image.setImageResource(int resId)
If you want to display an image file on the phone, you can do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageResource(R.drawable.imageFileId);
To avoid every problem you can use the sure way
Resources res = getActivity().getResources();
Drawable drawable= res.getDrawable(R.drawable.myImage);
image.setImageDrawable(drawable);

Categories