1 Replies - 186 Views - Last Post: 10 September 2017 - 09:28 AM

#1 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

i am trying to create a Font Changer app with spinner

Posted 10 September 2017 - 07:06 AM

I am all new to this app stuff and i am not getting the expected output. The expected output is when user enters some text he can select his desired font in drop down menu and when clicked the button it should display the input text with desired font as text view. I think i am missing somewhere between edit text,button and spinner. i first tried with just one edit text and a button and thought to get this to the next step by adding a drop down .But I seem I am unable to do this. Please help me to solve this..thank you.

MainActivity.java

package com.pappu5.fontchanger;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Spinner sp ;
String names[] = {"Chiller Regular","Magneto Bold","Old English","FixedSys Regular","Rosewood"};

String record = "";
ArrayAdapter<String> adapter;


TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sp = (Spinner)findViewById(R.id.spinner);
    final EditText ed = (EditText)findViewById(R.id.editText);
    final Button bt = (Button)findViewById(R.id.button);

    tv = (TextView)findViewById(R.id.pappuText);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);

    sp.setAdapter(adapter);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

            //String item = adapterView.getItemAtPosition(position).toString();
            switch (position){
                case 0 :
                     record = String.valueOf(ed.getText());
                    selectFont(record);
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    bt.setonclickListener(new View.onclickListener() {
        @Override
        public void onclick(View view) {
            tv.setText(record.toString());
        }
    });
}
public void selectFont(String pos){
    Typeface tf = Typeface.createFromAsset(getAssets(),"GOUDYSTO.TTF");
    Typeface tf1 = Typeface.createFromAsset(getAssets(),"MAGNETOB.TTF");
    Typeface tf2 = Typeface.createFromAsset(getAssets(),"OLDENGL.TTF");
    Typeface tf3 = Typeface.createFromAsset(getAssets(),"CHILLER.TTF");

    if(pos=="Chiller Regular"){
        tv.setTypeface(tf3);
    }else if (pos=="Magneto Bold"){
        tv.setTypeface(tf1);
    }else if (pos=="OLD ENGLISH"){
        tv.setTypeface(tf2);
    }else if(pos=="ROSEWOOD"){
        tv.setTypeface(tf);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: i am trying to create a Font Changer app with spinner

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 563
  • Posts: 2,298
  • Joined: 13-June 14

Re: i am trying to create a Font Changer app with spinner

Posted 10 September 2017 - 09:28 AM

Lines 71-79: you're comparing strings with ==. This is incorrect in Java; you should be using the equals method to compare identity.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1