How can i use bangla font as a string value in android - java

how can i use bangla font in a android app as a string value in my string.xml file and also read in my UI.Advance thanks to answer

First open assets folder and create a new folder named font and then put Rupali.ttf in the fontfolder.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/DefaultFontText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Here is some text." />
<TextView
android:id="#+id/CustomFontText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="ডিরেক্টর মমনক(!?) করতেছি অন্তত ঘড়িটা যেন বানাতে পারি আমি চেষ্টা করছি">
</TextView>
</LinearLayout>
And,
package com.amader;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class Fonts extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Typeface tf = Typeface.createFromAsset(getAssets(),
"font/Rupali.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
}
}
Shortcoming: All bangla combined words does not work properly. If anybody has the solution please let me know.

Please follow the process given below:
create folder named assets under app folder
create folder named fonts under assets folder
now put your desire bangla font file like durga.ttf or otf in fonts folder
Now go where you declare your textview and put this code:
yourTextView.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Durga.ttf"));

Related

Tabs > fragments with different layout + actions

I'm new to android studio / java language.
I need to set up a pretty straightforward app, but the information I find doesn't let me solve the problem.
Can any of you help :)?
I want to make an app with 3 tabs
(first tab) user enters a decimal number, and after click of a button the result shows a value calculated with the means of a formula
(second tab) same as first tab but with values and formulas
(third tab) information of each formula
I've implemented a (for this use, simplified) code for the first tab.
I know how to code all the three tabs separately, but I don't know how
to merge them together in one app with 3 tabs.
I started with the tabs-template given in android studio, but it demands that every tablayout is the same. I've seen a lot of answers how to have different layouts for each tab, but how do I code the different tabs (e.g. setonclicklistener).
Second problem is that every solution uses android and I have androidx, so the imports won't take. And in dependencies I don't find design V7 or anything of that sort.
Mainactivity.java:
package com.example.soloapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
DecimalFormat numberFormat = new DecimalFormat("#.0");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
EditText editNum = (EditText) findViewById(R.id.editNum);
TextView resultTextView = (TextView) findViewById(R.id.resultTextView);
double cysC = Double.parseDouble(editNum.getText().toString());
double tempResult = Math.pow(cysC,-0.931);
double resultLong = 70.69 * tempResult;
String result = numberFormat.format(resultLong);
resultTextView.setText(result);
}
});
}
}
activy_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintBottom_toTopOf="#+id/resultTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="168dp"
android:text="Result"
app:layout_constraintBottom_toTopOf="#+id/calcButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/calcButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="200dp"
android:text="CALCULATE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'll add what you need to learn in order to create what you told.
You need to use ViewPager in Android to create swipeable tabs. (If you know WhatsApp, those swipeable three tabs are ViewPager).
You can learn more about ViewPager here.
For using ViewPagers, you need to learn what Fragments are, Fragments in Android are like small activities(but not activities). You embed these Fragments inside your Activities, so you need to put these Fragments inside the Activity that contains your ViewPager.
You can learn about Fragments here.
Although, the first ViewPager link will be sufficient for you to learn everything that you need to learn about creating Swipeable Tabs.
About the second problem that you mentioned.
According to Migrating to AndroidX,
Androidx only maps the original support library API packages into the
androidx namespace.
Basically, they just renamed the package name so that it'd be easy for them to support the updates to libraries.
You can easily find the corresponding androidx package from here.
Migrating to Androidx.

Cannot find symbol errors in Android Studio Tutorial?

https://developer.android.com/training/basics/firstapp/building-ui.html#Weight
I am doing the tutorial above and to get it to build I had to comment out these lines inside MainActivity.java inside src folder (This code is in the MainActivity class inside OnCreate()).
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
//
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
If I do not comment out these lines, I get 'cannot find' errors and can't build, example:
error: cannot find symbol variable toolbar
Can someone explain in plain english why this is happening and how I can fix it? I have tried various import.R fixes that people found to combat Eclipse randomly adding that, but they don't work. My imports:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
I am using Android Studio like the tutorial says and get the same error on a clean install. Is it my automatically generated imports? Is the tutorial wrong or incompatible with the latest Android Studio? Are my build settings wrong?
The R class is auto-generated based on what is in your *.xml files, whether they are strings, dimensions, colors, ids within layouts, etc.
When you are calling code like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
You are saying, "Set the toolbar variable to the widget with an id of toolbar".
In an xml, say you had three textviews like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_1" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_2" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_3" />
When I build the project, my R.java file will then be updated to have references in java code to those ids that I defined in xml, like this:
public static final int text_view_1=0x7f0c0066;
public static final int text_view_2=0x7f0c0067;
public static final int text_view_3=0x7f0c0068;
To explain it at a very basic level, the R.java file is java code that is generated to reference xml elements within actual java code.
I am guessing that when you created a project in Android Studio, it came with the MainActivity.java class. That's where that code is coming from that you posted above. The part right above that in the default MainActivity.java class is
setContentView(R.layout.activity_main);
This is setting the activity's view to the xml layout file defined in activity_main.xml. Inside of this layout is the following
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
My guess is that perhaps your activity_main.xml does not contain this definition for the Toolbar and the FloatingActionButton widget.
If you want to really start from scratch so that you can follow the tutorial you linked, you should delete the MainActivity.java and just go create your own.
I know this is old, but I'm going to answer because this tutorial is still the best for android studios, and it poorly explains this part.
OnCreate() creates the elements you define in XML in Java, by creating the default activity, you still have a
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
and a
fab.setOnClickListener(new View.OnClickListener()
defined in OnCreate, android studios as not created these resources in android.R because you deleted there definitions in XML as part of the tutorial!
Run>Clean and Rerun'app' or ctrl+F5 on Android Studio

Cannot find id with R.id

This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>
TextView has in id of myText and from my Java file I am trying to access this view:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View myTextView = (TextView)findViewById(R.id.myText); // here
myTextView.setText("New Text in Text View");
}
But that myText has red color font meaning it is not resolved.
Why is this so? How can I fix it?
UPDATE:
My imports:
package ru.startandroid.androidlessons;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
I was following some tutorial and created lots of modules in one project. So, I got confused and created layouts in one module and tried to access it from another module's java file. So, using the right java file did it
But I was still unable to import R.java of another module. Is that possible? or restricted?
Module name: p0061_layouts, Package name: ru.startandroid.layouts
Maybe you've imported android.R instead of your library.
It happens to me sometimes in Eclipse :-)
if setContentView(R.layout.activity_main) worked? then their may be have some mistake first of all use
TextView instead of View
if still have same error manually clean your project then try if you are using eclipse in menu bar project-> cleanproject and check automatically build

In Android How to set Text? [duplicate]

This question already has answers here:
Issue when using a custom font - "native typeface cannot be made"
(16 answers)
Closed 8 years ago.
I am developing my app with Maven build tool..
In my assets folder I have fonts/DroidSansFallback.ttf..
And my activity_main.xml is:--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bck54"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="99dp"
android:layout_marginTop="78dp"
android:text="hello world" />
</RelativeLayout>
and my HelloAndroidActivity.java is:--
public class HelloAndroidActivity extends Activity {
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/DroidSansFallback.ttf");
tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
}
}
Here I want to change my text fonts..But the app is stopped..
My logcat error is:--
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arijit.patra.love/com.arijit.patra.love.HelloAndroidActivity}: java.lang.RuntimeException: native typeface cannot be made
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2346)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2398)
at android.app.ActivityThread.access$800(ActivityThread.java:159)
Can anyone tell me where is the problem??
may be your font file is corrupted that's why you get this error message.
delete that file and download again from somewhere else and put it again in your asset folder.
Are you created a folder called fonts in assets or directly placed in assets folder??if folder not created create a folder and place your font file there I think that is the mistake..In your example he is not created any fonts folder he is directly accessing but you are accessing from fonts folder..
If not fonts folder then change this line to.
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");
like this..
Typeface font = Typeface.createFromAsset(getAssets(), "amal.TTF");
it its not working than,
The font file is either corrupt or unsupported for some reason.

TextView Not Accessible

I am new to Android.I am making a random generator program which on button click generates a number and display it on textview. I am unable to access textview when i type R.id. i don't get the name of the textview but i get the name of the other components. My layout code is:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<TextView android:id="#+id/txt02" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Java code is:-
public void sendMessage(View view) {
// Do something in response to button
EditText editmessage = (EditText)findViewById(R.id.edit_message);
String message = editmessage.getText().toString();
Random r=new Random();
int i1=(r.nextInt(80) +65);
message += "\n " + i1;
// Create the text view
TextView textView = (TextView) findViewById(R.id.); // not able to access R.id.txt02
}
The problem most likely is that your R.java file hasn't been updated with the EditText you have added. In eclipse, you can force an update to do this. Try Project->Clean as mentioned by Michal Z. in the comments.
You can also set the option to build automatically in eclipse by checking the Project->Build Automatically option. This will ensure that eclipse will automatically build your project and update your R.java file every time you save your project.
If cleaning your project doesn't help, I'd check your source files to make sure there isn't a line saying "import android.R" in your imports because that will import the standard Android R object (which doesn't include any of your declared resources). Eclipse does this sometimes when you cut and paste code and it can be frustrating, but deleting the import and cleaning / building your project should fix it.

Categories