Android custom Imageview throws 'can't cast' exception - android

I'd like to use the ImageViewTopCrop class described in this question:
ImageView scaling TOP_CROP
My custom view is based on this code:
Custom Image View class
Here is a layout file using this custom view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_background"
android:background="#000"
android:orientation="vertical">
<FrameLayout
android:id="#+id/details_frame"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<com.grayraven.imagetest.ImageViewTopCrop
android:id="#+id/grid_item_image"
android:src="#drawable/hobbit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
Here's how I'm trying to display it in code:
com.grayraven.imagetest;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class DetailsActivity extends ActionBarActivity {
private TextView titleTextView;
private com.grayraven.imagetest.ImageViewTopCrop imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_view);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
String title = getIntent().getStringExtra("title");
String imageUrl = getIntent().getStringExtra("imageUrl");
titleTextView = (TextView) findViewById(R.id.title);
imageView = (com.grayraven.project1.ImageViewTopCrop) findViewById(R.id.grid_item_image);
//Exception thrown here:
//android.widget.ImageView cannot be cast to com.grayraven.imagetest.ImageViewTopCrop
//can't go any further
}
}
What simple thing am I missing?

I think you are type casting with wrong class.
You used
com.grayraven.imagetest.ImageViewTopCrop
in your xml but type casting with com.grayraven.project1.ImageViewTopCrop

I was missing a constructor in my custom view class. Never mind.

Related

React-native Android Splashscreen - Custom fonts

I created a splashscreen for my react-native app and have to implement TextView with custom fonts.
I created an Android layout specifically for the splashscreen with res/layout/launch_screen.xml and the TextViews appears but without custom fonts then i launch that on splashActivity file.
But it's strange because when i inject the layout directly on the mainActivity file the fonts seems be loaded.
So i think it's due to the fonts path in SplashActivity file but the code seems be ok.
Here are the files :
SplashActivity.js :
package com.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.graphics.Typeface;
import android.content.res.AssetManager;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch_screen);
TextView txt_1 = (TextView) findViewById(R.id.din_bold);
Typeface font_1 = Typeface.createFromAsset(getAssets(), "fonts/DinCondBold.otf");
txt_1.setTypeface(font_1);
TextView txt_2 = (TextView) findViewById(R.id.din_light);
Typeface font_2 = Typeface.createFromAsset(getAssets(), "fonts/DinCondLight.otf");
txt_2.setTypeface(font_2);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
MainActivity.js
package com.app;
import android.app.Activity;
import com.facebook.react.ReactActivity;
import android.os.Bundle;
import org.devio.rn.splashscreen.SplashScreen;
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.show(this, R.style.SplashScreenTheme);
}
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
#Override
protected String getMainComponentName() {
return "app";
}
}
And launch_screen.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:background="#drawable/background"
android:gravity="center"
android:orientation="vertical"
tools:context="com.mitsu.MainActivity">
<TextView
android:id="#+id/din_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SCAN PRODUITS"
android:textColor="#eb212e"
android:textSize="36sp"
android:textStyle="bold" />
<TextView
android:id="#+id/din_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIENVENUE"
android:textColor="#717171"
android:textSize="36sp" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_marginTop="14dp"
android:src="#drawable/splash_logo" />
</LinearLayout>
Does anyone know what's going on?

EditText change setError text position

So so what im trying to implement has two parts
One im giving my edittext the passwordToggle for which im using
android.support.design.widget TextInputLayout+TextInputEditText
So this is how my edittext looks like
Part two is i want to add validation and set appropriate error message.
I need the error message to be shown as follows
My layout code is as follows
<android.support.design.widget.TextInputLayout
    style="#style/editTextBold"
    android:id="#+id/input_pwd_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintAnimationEnabled="false"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">
    <android.support.design.widget.TextInputEditText
        android:id="#+id/input_pwd"
        style="#style/editTextBold"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#android:color/transparent"
        android:ems="10"
        android:hint="#string/hint_pwd"
        android:inputType="textPassword"
        android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
So what i want to know is
1.How do i hide/unhide the password toggle icon in the edittext via code?
2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)
Ended up creating a custom view
The component can be found here
https://github.com/vnh1991/CustomValidatorEditText
Import the lib project as a module ,
in your layout create a container
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.v2dev.customedittextdemo.MainActivity">
<LinearLayout
android:id="#+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"></LinearLayout>
<Button
android:id="#+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/llContainer"
android:padding="10dp"
android:text="Validate" />
</RelativeLayout>
And in your activity load the component into the container
package com.v2dev.customedittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
public class MainActivity extends AppCompatActivity {
private LinearLayout llContainer;
private Button btnValidate;
private CustomEdittext inputPwd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContainer = (LinearLayout) findViewById(R.id.llContainer);
btnValidate = (Button) findViewById(R.id.btnValidate);
inputPwd = new CustomEdittext(llContainer, this);
inputPwd.setHint("PASSWORD");
btnValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputPwd.validate()){
Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}

List Crashes App when updated

I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.

Android Studio, adding multiple images on click

JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);

findViewById is not working

I have a TextView in my app and I want to change its textSize programmatically using the setTextSize method, but everytime I ran my code, I got an error message : "java.lang.NullPointerException". I tried to print the value of the TextView and it got NULL value.
Here's my code:
LoadActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text);
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
setContentView(R.layout.activity_load);
}
}
activity_load.xml
<?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"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Wait"
android:id="#+id/loadTextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:typeface="monospace"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
What do you think is the problem?
setContentView(R.layout.activity_load); should be declared before initializing your views(TextView) and System.out.println("TextView: "+text); is not correct form to print value. The correct form is System.out.println("TextView: "+text.getText().toString());
So your corrected code will be like this
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Use this in your onCreate Method
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
text.setTextSize(30);
In order for you to find views by their id, you need to write
setContentView(R.layout.activity_load);
This is because it essentially gets the layout resource file that defines your UI, and then allows you to retrieve other views within that layout (with findViewById();)
Look here for more information on how to properly set up Activites
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load); // Retrieves layout that defines your UI
// After setContentView is called, you can find views within the layout
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}

Categories

Resources