Moving library files to main project in android - android

I am using opensource OI ShoppingList template and trying to modify it from http://www.openintents.org/.
There are two Libraries and one main project, I am trying to move the libraries to main project which is "OI ShoppingList" but it gives me error in code.
I managed to move one of them without problems "ShoppingListLibrary". I had no problems moving DistributionLibrary, but when I move layout files ("oi_distribution_eula", "oi_distribution_infoactivity") to main project 3 source files broke.
Any ideas what I should change to make this work been searching everywhere but no luck.
Here is dropbox link to source.
https://www.dropbox.com/s/iisqncajbbd9gel/ShoppingList-source-1.6%20not%20working.rar
Thanks in advance
edit1: thanks for the feedback. Here is one of the codes the error is in setContentView(R.layout.oi_distribution_infoactivity);
under the layout the error reads "layout cannot be resolved or is not a field"
I have tried importing R class, cleaning the project but still i have that error
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDistribution.setFirst(MENU_DISTRIBUTION_START, DIALOG_DISTRIBUTION_START);
// Check whether EULA has been accepted
// or information about new version can be presented.
if (mDistribution.showEulaOrNewVersion()) {
return;
}
setContentView(R.layout.oi_distribution_infoactivity);
init();
mApplicationStrings = new String[mApplications.length];
for (int i = 0; i < mApplications.length; i++) {
mApplicationStrings[i] = getString(mApplications[i]);
}
setListAdapter(new FontArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mApplicationStrings));
ListView listview = getListView();
listview.setOnItemClickListener(this);
// Set message of activity
String appname = VersionUtils.getApplicationName(this);
String message = getString(R.string.oi_distribution_info_activity_text,
appname);
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(message);
/*
TypedArray a = obtainStyledAttributes(mTheme, R.styleable.ShoppingList);
String typefaceName = a.getString(R.styleable.ShoppingList_textTypeface);
mTextSizeMedium = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeMedium, 23);
mTextSizeLarge = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeLarge, 28);
mTextColor = a.getColor(R.styleable.ShoppingList_textColor, Color.BLACK);
Drawable background = a.getDrawable(R.styleable.ShoppingList_background);
View v = findViewById(R.id.background);
v.setBackgroundDrawable(background);
mTypeface = Typeface.createFromAsset(getResources().getAssets(), typefaceName);
TextView tv = (TextView) findViewById(R.id.text);
tv.setTypeface(mTypeface);
tv.setTextSize(mTextSizeMedium);
tv.setTextColor(mTextColor);
*/
}

Related

Best way to have lots of pictures in an App? Android Studio

So I am making an app that has over 200 sunset pictures (taken by myself:P). I want to display them all via android.R.layout.simple_gallery_item I tried tossing them all in the drawable folder, then bitmapping each one and drawing it on the screen, but I get an Out of memory error.
What would be the best way to store these jpg?
The only two ways I can think of are:
A separate zip file download from the app?
Drawable File? (Using bitmap wouldn't be the right way?)
Somehow storing them in the apk then pushing them onto the device before installing the app?(If possible, because if I am correct(which I am probably not), when building the apk, everything comes together and some files morph into one)
(PS: If can't tell, I am new to Android Developing)
Thanks!
Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Noobs");
ArrayList<Image> img = new ArrayList<Image>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier("#drawable/bm" + String.valueOf(i), null, getPackageName());
img.add(ImageView.setImageResource(imageResource));
System.out.println("Did Pic "+String.valueOf(i));
}
System.out.println("Come so far!");
ListAdapter picAdapt = new ArrayAdapter<Image>(this, android.R.layout.simple_gallery_item, img);
ListView picListView = (ListView) findViewById(R.id.gallery);
picListView.setAdapter(picAdapt);
}
}
I recommend you use picasso http://square.github.io/picasso/
or glide https://github.com/bumptech/glide
those two help you to get and optimize your images
ArrayList<Integer> img = new ArrayList<Integer>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier(mContext.getPackageName() + ":drawable/bm" + String.valueOf(i), null, null);
img.add(imageResource);
}
ImageView imageView = findViewById(R.id.<UR_ID>);
imageView.setImageResource(img.get(0));

Trying to add multiple checkbox into an array on Android Studio

Im doing an App on Android Studio and im trying to add all my checkbox IDs into an array, so i can use it , without doing it manually.
I tried to do it on another way, but i didn´t find nothing on google that help me,
So here is my goal:
I want to get all my Checkbox ids, so i can get their text. And i don´t want to do it manually because i got alot of checkboxs.
I tried to write a code by myself but i´m getting an message error.
Here is my code:
CheckBox[] MinhaCheckBox;
SharedPreferences Dados;
String MinhaPasta = "Pasta";
String valor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_Gerar(View v)
{
Dados = getSharedPreferences(MinhaPasta,0);
SharedPreferences.Editor Edita = Dados.edit();
int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
for(int i = 0 ; i < IDTexBoz.length; i++)
{
*//I GOT AN ERRO HERE. please help me.*
* // when i try to put mycheckbox on my array, i got th error!*
MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);
}
for (int a= 0; a < MinhaCheckBox.length;a++)
{
if(MinhaCheckBox[a].isChecked())
valor += MinhaCheckBox[a].getText().toString() + ";";
}
Edita.putString("Dado", valor);
Edita.commit();
Intent MeuIntent = new Intent(this,Main2Activity.class);
startActivity(MeuIntent);
}
}
You need to initialize your checkbox array.
Maybe this example could help, you don't need to store the ID name, do something like this..
for(int i=1;i<=12 ;i++){
int resID = getResources().getIdentifier("checkBox"+i, "id",getPackageName());
CheckBox cb = (CheckBox) findViewById(resID);
//Handle cb object here
}
It's Very Simple.
int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
//Here is code for array initialization.
MinhaCheckBox = new CheckBox[IDTexBoz.length];

Is there a way to change a reference pathway using a variable? [duplicate]

This question already has answers here:
How to get a resource id with a known resource name?
(10 answers)
Closed 8 years ago.
Is there a way to change a reference to an ID in the Android manifest using a variable?
As in:
for(int counter6=1;counter6 <= 12; counter6++)
value = bundle.getString("value"+counter6);
TextView text1 = (TextView) findViewById(R.id.textView+counter6);
text1.setText(value);
Is it possible to have the counter6 variable used in the ID directory, so the for loop can loops through all the different text view making each one text1 respectively then setting their text to the string value?
Its not really a problem if it cant work this way it just means more lines of code to write.
You can't really make a loop on the id and increment it as it is generated but you can make an array of references and by getting that array find each TextView and update the text:
<array name="array_text_views">
<item>#id/text_view_1</item>
<item>#id/text_view_2</item>
<item>#id/text_view_3</item>
<array>
In your code, something like that:
ArrayList<TextView> myTextViews = new ArrayList<TextView>();
TypedArray ar = context.getResources().obtainTypedArray(R.array.array_text_views);
int len = ar.length();
for (int i = 0; i < len; i++){
myTextViews.add(findById(ar[i]));
}
ar.recycle();
I would usually just put a small int[] array of Ids into the code somewhere. If you have a lot of them, consider creating them programmatically (layout.addView(new TextView(..).
For example if you want to start an Activity and tell it what strings to display via the Extras Bundle you can put them directly as an array.
void startOther(String[] texts) {
Intent i = new Intent( /* ... */);
i.putExtra("texts", texts);
// start via intent
}
Now inside that Activity I would put the ids as a "constant".
// hardcoded array of R.ids
private static final int[] TEXT_IDS = {
R.id.text1,
R.id.text2,
// ...
};
And then use both the Bundle and the id Array for example like this:
// a List of TextViews used within this Activity instance
private List<TextView> mTextViews = new ArrayList<TextView>(TEXT_IDS.length);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.something);
// find all TextViews & add them to the List
for (int id : TEXT_IDS) {
mTextViews.add((TextView)findViewById(id));
}
// set their values based on Bundle
String[] stringArray = savedInstanceState.getStringArray("texts");
for (int i = 0; i < mTextViews.size() && i < stringArray.length; i++) {
mTextViews.get(i).setText(stringArray[i]);
}
}

Obscure issue with string formatting from SQLite database

I appreciate there's a lot of helpful stack questions and answers to my question but I'm running into problems I've not had in the past.
The Problem:
I am using a cursor to populate textviews in rows on a view (without using listview - that's crazy I know). I am trying to format the string value(s) taken from from the database column STUDENT_POINTS that are put into a textview tpoints. Here is the code I am using:
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(Students.STUDENT_ID));
final String name = c.getString(c.getColumnIndex(Students.STUDENT_NAME));
final String age = c.getString(c.getColumnIndex(Students.STUDENT_AGE));
final String points = c.getString(c.getColumnIndex(Students.STUDENT_POINTS));
final String teachernote = c.getString(c.getColumnIndex(Students.TEACHERNOTE));
final byte[] image = c.getBlob(c.getColumnIndex(Students.IMAGE));
ImageView iv = (ImageView) v.findViewById(R.id.photo);
if (image != null) {
if (image.length > 3) {
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
}
}
TextView tname = (TextView) v.findViewById(R.id.name);
tname.setText(name);
TextView tage = (TextView) v.findViewById(R.id.age);
tage.setText(age);
TextView tpoints = (TextView) v.findViewById(R.id.points);
tpoints.setText(String.format(points, "%1$,.2f"));
final StudentsConnector sqlCon = new StudentsConnector(context);
The rest of bindView is for buttons so I have not included it here. The problem is with the line:
tpoints.setText(String.format(points, "%1$,.2f"));
I'm intending to have commas to separate out large numbers but this does nothing! If anybody has the time could you please tell me what I'm doing wrong?
Thanks in advance.
You have your two parameters backwards-- you should have the format string followed by the data string: String.format("%1$,.2f", points );
This formatted nicely for me with this little snippet in my code:
double points = 56789.45f;
String boogie = String.format("%1$,.2f", points );
and it generated a number 56,789.45
But bigger numbers don't work well due to precision in the formater. You may want to split the mantissa off of their, format them separately and combine them.

How to create Custom Text Views in android?

Hai Friends,
I am parsing the url to display the contents in it, my requirement i have to display the each content in separate textviews.
For Instance:
Let us assume the contents in that url are FootBall, Carom , chess, VolleyBall and so on . I want to display FootBall as a individual textview similarly others. so i cannot declare the textviews in xml what i usually do.
(<TextView android:text=" " android:layout_width="wrap_content"
android:gravity="center_horizontal" android:paddingLeft="7dp" android:layout_height="wrap_content"
/>).
so i planned to create textview via java code
This is my parsing code which parse the url contents and store the result in a string array namely san_tagname; depending upon the length of this variable i want to create number of textviews.
List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
l_obj_tagname = obj_parse1.parse_tagname();
System.out.println("l_obj_tagname"+l_obj_tagname.size());
String[] san = new String[l_obj_tagname.size()];
Iterator<Message_category> it_id1 = l_obj_tagname.iterator();
i=-1;
while (it_id1.hasNext()) {
i++;
san[i] = it_id1.next().toString();
System.out.println("Id="+san[i].toString());
san_tagname[i]=san[i];
//vm.setTitle(it.next().toString());
}
for(int z=0;z<san_tagname.length;z++)
{
//how to create textview here ...............
}
I am really struggling on this, pls help me regarding on this friends.................
Thanks In Advance
Tilsan The Fighter...
TextView tv = new TextView(context);
tv.setText(myText);
parent.addView(tv, {LayoutParams for parent container type})
Here is the answer,
Parse the Contents from web
//manipulation to parse id
List<Message_category> l_obj_id = new ArrayList<Message_category>();
l_obj_id = obj_parse1.parse_id();
VAL1 = new String[l_obj_id.size()];
Iterator<Message_category> it_id = l_obj_id.iterator();
while (it_id.hasNext()) {
i++;
VAL1[i] = it_id.next().toString();
System.out.println("Id="+VAL1[i].toString());
//vm.setTitle(it.next().toString());
}
//manipulation to parse tagname
List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
obj_parse1.parse_tagname();
obj_parse1.storedata();
san_tagname= new String[obj_parse1.santagname.length];
for(int k=0;k<obj_parse1.santagname.length;k++)
{
san_tagname[k]=ParsingHandler.temptag[k];
if(san_tagname[k].contains("%20"))
{
san_tagname[k]=san_tagname[k].replace("%20"," ");
System.out.println("San_tagName1"+san_tagname[k]+"S"+k);
}
else
{
System.out.println("San_tagName2"+san_tagname[k]+"S"+k);
}
}
gal_lay = (LinearLayout) findViewById(R.id.rl_1);
navagtion_bar= (LinearLayout) findViewById(R.id.san_tag);
hv = (HorizontalScrollView)findViewById(R.id.gv);
// This is the Code i needed finally i stirkes with the help of hackbod
**for(int z=0;z<san_tagname.length;z++)
{
TextView san_text[]= new TextView[san_tagname.length];
san_text[z]= (TextView) new TextView(this);
san_text[z].setText(" "+san_tagname[z]+" ");
san_text[z].setTextSize(15);
navagtion_bar.addView(san_text[z]);
}**

Categories

Resources