Create new project in Android Studio as copy of another

  1. With AS closed, use file manager to copy project folder to create another project and change folder name to new name.
  2. Go into newproject/app and edit build.gradle (eg in Notepad) to change package name.
  3. In same way edit AndroidManifest.xml to change package name.
  4. Delete newproject.iml in newproject/.
  5. Open AS and refactor package name.
Posted in Android | Leave a comment

drawable not found

I was getting this stack dump, which made it difficult to work out what the problem was.

android.content.res.Resources$NotFoundException: Resource ID #0x7f060056
at android.content.res.Resources.getValue(Resources.java:1123)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)

Problem was fixed by moving 2 vector drawables (used in menu) from drawable-v24 to drawable.

Posted in Android | Leave a comment

no actionbar

When copying projects from an Android sample project I’ve found that when the app runs there is no action bar visible. Solved this by the following.

Into the top of the activity put

android.support.v7.app.ActionBar ab;

Into onCreate put

actionBarSetup();

Create method

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void actionBarSetup() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ab = getSupportActionBar();
    }

Change Activity to AppCompatActivity.

Posted in Android | Leave a comment

new wear app says default activity not found

After creating a new wearable app in Android Studio using its own templates for a watch face, the app would not run, saying ‘default activity not found.’

The simple answer (mentioned here) is to have a simple activity and its manifest entry includes an intent filter with:

action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.LAUNCHER"

 

Posted in Android | Leave a comment

Clickable link in textview

  1. Create resource in strings.xml.
<string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>

2. Create textview in layout and assign string.

    <TextView
        android:id="@+id/textLink1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txtCredits" />

3. Modify in activity.

link1 = (TextView) findViewById(R.id.textLink1);
link1.setMovementMethod(LinkMovementMethod.getInstance());

 

Posted in Android | Leave a comment

addRule(RelativeLayout.BELOW not working

I needed to create a view dynamically and that was working ok. Then I wanted to add some text beneath that view and I couldn’t get it to work: the text appeared over the top of the first view. The solution for me was to set in code the id of the first view.

Code in activity:

RelativeLayout.LayoutParams lpr, lpr2;
TextView tip;
ViewPager vp;
...
lpr = new RelativeLayout.LayoutParams(drawingWidth, drawingWidth);
lpr.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lpr.setMargins(10, 0, 0, 0);

vp = new ViewPager(this);
vp.setLayoutParams(lpr);
myLayoutr.addView(vp);

vp.setId(R.id.reservedNamedId);    //THIS IS THE LINE THAT FIXED IT

lpr2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpr2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lpr2.addRule(RelativeLayout.BELOW, vp.getId());

lpr.setMargins(10, 0, 0, 0);
tip = new TextView(this);
tip.setText(R.string.gallery_tip);
tip.setLayoutParams(lpr2);
myLayoutr.addView(tip);

This needs ids.xml to be created in values folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="reservedNamedId" type="id"/>
    <item name="reservedNamedId2" type="id"/>
    <item name="reservedNamedId3" type="id"/>
</resources>

 

Posted in Android | Leave a comment

Missed error due to logcat scrolling in Android Studio

I had an activity crashing but could only briefly see the crash report scroll by in the Android Studio logcat before it was cleared and replaced by the restarted activity. Changing ‘Show only selected application’ to ‘Edit filter configuration’ did not work, despite trying several settings.

Solution finally found in stackoverflow. Clicking 3 times on a line in logcat is supposed to stop the scrolling but it only worked for me – as for the person commenting in stackoverflow – when ‘Show only selected application’ was changed to ‘No filters’.

Posted in Android | Leave a comment

Restore scroll position on return to listview

Thanks again to stackoverflow. Wanted to position listview at same place it was left when the user clicked on an item to update/view it (in separate activity).

To save position – not just of top record but of the offset ie precise position relative to top:

int index = listview.fetFirstVisiblePosition();
View v = listview.getChildAt(0);
int offset = (v==null) ? 0 : (v.getTop() - listview.getPaddingTop();

Then when return (eg in onActivityResult):

listview.setSelectionFromTop(index, offset);

 

Posted in Android | Leave a comment

Update listview after item update

I couldn’t find a way to refresh the listview after returning from updating an item in a separate activity. I couldn’t get any variation of

adapter.notifyDataSetChanged()

to work, despite it being suggested in many places.

What worked for me was to useTo position listview at same position

startActivityForResult(intent, 0);

and then in onActivityResult update the cursor again and recreate the listview.

Posted in Android | Leave a comment

onItemClickListener not working

Had list view using custom adapter and found that onItemClickListener would not work. Found answer in stackoverflow.

Apparently the listener won’t work if any row item contains a focusable or clickable view. The solution is the following line, which I inserted into the top section of the row item layout.

android:descendantFocusability="blocksDescendants"

Apparently you can also do it programmatically:

listView.setDescendantFocusability(int focus);

where ‘focus’ is one of

  • ViewGroup.FOCUS_BEFORE_DESCENDANTS
  • ViewGroup.FOCUS_AFTER_DESCENDANTS
  • ViewGroup.FOCUS_BLOCK_DESCENDANTS

Another solution – one I haven’t tried – is for situations such as having a button on the row that you want to keep clickable. In such a case you add the following line in the button’s xml.

android:focusable="false"

 

Posted in Android | Leave a comment