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
Related
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.
The problem is that, the listView is not displaying correctly in the design tab of my activity_main xml, but is displaying correctly in the emulator that is running. This is inconvenient since, its pretty convenient to see the design in the Design tab.
Please tell me how to solve this.
emulator with design tab image
My code is as follows:`
1.Main_activity.java:
package com.example.lemonade.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.lemonade.app1.R;
public class MainActivity extends Activity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
main_activity.xml:
<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="vertical"
tools:context=".ListActivity" >
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ListDisplay</string>
<string name="action_settings">Settings</string>
</resources>
activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
I can think on 2 problems that you may have:
1)Please check the min SDK in you'r app module and the phone you are trying to run the app on maybe the min SDK is higher then you'r phone.
2)You trying to show image View on you'r list View and they coped as "V21",When you trying to copy image into your drawable library it give you 2 option regular image or V21 for some reason the v21 don't show on phones and on emulators they do,well for me at least because i had the same problem and it solve it to me.
If one of these didnt helped try to debug it and tell me what results you got.
Are you referring to the action bar showing in your design but not the emulator? It sounds like you have your app theme set to include an action bar and your design preview is showing it assuming you are using it.
I can't test it right now but I believe if you don't explicitly set up your options in your activity by overriding onCreateOptionsMenu(), which you aren't, it will not show the action bar when you run your app.
You can check your app theme in your manifest and/or styles.xml, look for
"android:theme="#style/AppTheme"
Which points to styles.xml in my case.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
If you change the parent to something without the action bar, Theme.AppCompat.NoActionBar for example, it will not show in your design window.
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
My app is like a photo Album. In every activity is one picture and I can go from one activtiy to the next over a button. The pictures I use are 640x480 and not bigger than 150KB. But very often the app stops working because of an OutOfMemory-Error. Probably are the Bitmaps too large. What do I have to add into my code to add large Bitmaps efficently? Momentarily I even didn't define the Bitmaps in my Java-Code. They are only in XML-Code. I probably have to write s.th in my Java-Code. But what?
package com.example.xxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class PictureOne extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pictureone);}
public void Picture0 (View view){
Intent i = new Intent(this, PageZero.class);
startActivity(i);}}
public void Picture2 (View view){
Intent i = new Intent(this, PageTwo.class);
startActivity(i);}}
XML-Code:
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_marginLeft="14dp"
android:src="#drawable/pic1" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/left"
android:onClick="Picture0"/>
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/right"
android:onClick="Picture2"/>
</RelativeLayout>
I believe that you should use universal image loader by nostra and for viewing just images there is no point of stacking up activities.You can simply use view pager or image flipper.
Look at this link..
https://github.com/nostra13/Android-Universal-Image-Loader.
yes you have to add nostra image loader as a library to your android project.
For loading large Bitmap's efficiently, just refer to this article:
Loading Large Bitmaps Efficiently
As you can see from there:
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.
Here is the full section on Android Developers on displaying Bitmaps.
But also, you can try to use Picasso library which is very useful for this.
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"));