Changing ImageView size - java

I'm trying to have an image show up when the name of it (TextVIew) is clicked. I have the code set up to what I believe is the right thing but the app force closes when I launch it. This is just a test code, so its not my actual app, but the idea is the same so can someone help me figure out why this is crashing?
java file:
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
image.getLayoutParams().height=20;
}
});
}
xml file:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:text="show image" />

You have to have this code (below) inside onCreate()
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
Try it.
Update:
The code could look like this:
public class MainActivity extends Activity {
ImageView image = null;
TextView text = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
image = (ImageView) findViewById(R.id.image);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (image.getVisibility() == View.GONE) {
image.setVisibility(View.VISIBLE);
text.setText("hide the image");
} else {
image.setVisibility(View.GONE);
text.setText("show the image");
}
}
});
}
}
and layout is here:
<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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show the image" />
</LinearLayout>

Fill your elements in onCreate, not as a field, after setting the content view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
...
And try setting the size of the ImageView not directly from layoutparams but with setHeight/setWidth functions

Related

Android, set text for an EditText on startup

So I have a simple android app where I take text from a text file (that part is working) and I want to set that text to EditText.setText(). The problem I'm having is that I cannot do this in the onCreate() method because the EditText field and the UI hasn't been created yet(from what it seems).
My question is where would be a good place to do this? Is there a function that is called right after the GUI elements are created?
public class MainActivity extends ActionBarActivity
{
private final String FILE_NAME = "awayReply";
private EditText myEditMessage;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
protected void onStart()
{
String message = readMessage();
myEditMessage = (EditText)findViewById(R.id.edit_message);
if(myEditMessage != null)
{
myEditMessage.setText(message, TextView.BufferType.EDITABLE);
}
}
XML Layout:
<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"
android:orientation="horizontal"
android:paddingBottom="16dp"
android:paddingTop="16dp" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/lbl_message"
android:inputType="textImeMultiLine" />
<Button
android:id="#+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:onClick="updateMessage"
android:text="#string/lbl_update" />
<ToggleButton
android:id="#+id/toggle_away"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/lbl_enable"
android:layout_alignBottom="#+id/lbl_enable"
android:layout_toRightOf="#+id/edit_message"
android:textOff="Disabled"
android:textOn="Enabled"
android:onClick="toggleEnabled" />
</RelativeLayout>
You can't set your text in a TextView until you inflate your layout with
setContentView(R.layout.main_activity);
in the Activity's OnCreate
Once that happens you can do:
EditText editText = (EditText)findViewById(R.id.myEditText)
Full Example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // this needs to happen first
EditText editText = (EditText)findViewById(R.id.myEditText)
editText.setText("my text", TextView.BufferType.EDITABLE); // <-- needs that second parameter
}
Edit: updated for EditText instead of TextView
I think I figured out my problem. I need to do this work in the PlaceholderFragment where onCreateView is defined. When I call findviewbyid here it dosen't return null.

java.lang.ClassCastException only on Android 2.3?

When i run my app on Android 2.3 devices.I am getting an java.lang.ClassCastException: android.widget.ImageView .But it works perfect in other Devices.
If i Comment the below Lines it is working.But i can't find any Error's in these Lines.
Any Suggestions ???
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
up.setOnClickListener(this);
down.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
Activity
public class Presentation extends MainActivity implements ControllerDroidActionReceiver, OnClickListener
{
Button up, down, start, stop;
private ControllerDroid application;
private ControlView controlView;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.application = (ControllerDroid) this.getApplication();
ActionBar ab = getSupportActionBar();
ab.setTitle("Presentation");
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.presentation, null, false);
mDrawer.addView(contentView, 0);
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
up.setOnClickListener(this);
down.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if (v == up)
{
application.sendAction(new KeyboardAction(19));
// Log.i("Button", "UP");
}
else if (v == down)
{
application.sendAction(new KeyboardAction(20));
}
else if (v == start)
{
application.sendAction(new KeyboardAction(26));
// Log.i("Button", "STARTED PPT");
}
else if (v == stop)
{
application.sendAction(new KeyboardAction(23));
}
}
#Override
protected void onResume()
{
super.onResume();
this.application.registerActionReceiver(this);
}
protected void onPause()
{
super.onPause();
this.application.unregisterActionReceiver(this);
}
public void receiveAction(ControllerDroidAction action)
{
if (action instanceof ScreenCaptureResponseAction)
{
this.controlView.receiveAction((ScreenCaptureResponseAction) action);
}
}
}
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" >
<Button
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="UP" />
<Button
android:id="#+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="DOWN" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="start" />
<Button
android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="Stop" />
</LinearLayout>
//put setContentview(R.layout.presentation)
or do it as below
up = (Button)contentView. findViewById(R.id.up);
down = (Button)contentView. findViewById(R.id.down);
start = (Button)contentView. findViewById(R.id.start);
stop = (Button)contentView. findViewById(R.id.stop);
Please try with contentView.findViewById() because you are not doing setContentView().
up = (Button) contentView.findViewById(R.id.up);
down = (Button) contentView.findViewById(R.id.down);
start = (Button) contentView.findViewById(R.id.start);
stop = (Button) contentView.findViewById(R.id.stop);
May be some where you used at least one of them id for ImageView.
It may help you..

App gets force close when clicked on imageview in android

I have a "Menu" imageview in all the pages of my app, if clicked on menu, all the menuitems like login, home, jobs, about will get open and If i click on "about" menuitem the app gets force close and getting null pointer exception on "OnClicklistener of menu" but other Menuitems are not having this issue, please can anybody solve this?
public class About extends Activity {
LinearLayout line1, line2;
ImageView menu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});
ImageView home = (ImageView) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Home.class));
}
});
ImageView jobs = (ImageView) findViewById(R.id.jobs);
jobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Jobs.class));
}
});
ImageView log = (ImageView) findViewById(R.id.log);
log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Login.class));
}
});
ImageView about = (ImageView) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(getApplicationContext(), About.class));
}
});
}
XML file
<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"
android:background="#color/black" >
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/menu" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_toRightOf="#+id/ll1"
android:visibility="gone"
>
<ImageView
android:id="#+id/about"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/jobs"
android:src="#drawable/about" />
<ImageView
android:id="#+id/jobs"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/jobs" />
<ImageView
android:id="#+id/log"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/log" />
<ImageView
android:id="#+id/home"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/home" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/white"
android:textColor="#color/white"/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="710dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:text="#string/AboutPage"
android:textColor="#color/white" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll1"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="#string/WelcometoRebuixcom"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
ImageView menu;
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
Look at these code lines, the Second one is cause of Force Close, as menu is NULL.
Just interchange it like,
menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
Update:
You are declaring ImageView menu;
Now the second line, menu.setOnClickListener(new View.OnClickListener() {
without defining ImageView menu you are setting setOnCLickListener() to it, which cause NullPointerException.
So you have to define ImageView menu like menu = (ImageView)findViewById(R.id.menu); after declaring.
Just go through for basic Android Programming and Core Java tutorial for How to declare and define Objects and Member Variables. As I think you have poor programming concepts.
you should move the code
ImageView menu = (ImageView)findViewById(R.id.menu);
before
menu.setOnClickListener(new View.OnClickListener() {}
try this
ImageView menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {............
you should put
ImageView menu = (ImageView)findViewById(R.id.menu);
befor
menu.setOnClickListener
Initialize your imageview befor onclick listener as below:
ImageView menu = (ImageView)findViewById(R.id.menu); <<---- Here
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});

How to reference the image from the xml?

This is the oncreate from my application.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
//ImageView img = (ImageView)controlInflater.
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
control.xml
<?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"
android:gravity="center"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
</LinearLayout>
how to reference the ImageView in control.xml from java code.
View viewControl = controlInflater.inflate(R.layout.control, null);
ImageView img =(ImageView) viewControl.findViewById(R.id.img);
use this to get reference to imageView.
There is a findViewById method in View as well:
ImageView img = (ImageView)viewControl.findViewById(R.id.img)

i am not getting icon for customized tab

In my app i am using the customized tab. Here i am adding icons pro-grammatically to the image view. the images are adding correctly except to the first tab. please can anybody help me.
Following is my code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/tab_bg_selector"
android:padding="2dip"
android:gravity="center"
android:orientation="vertical">
<ImageView android:id="#+id/tabicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="12dip"
android:textColor="#drawable/tab_text_selector"
/>
</LinearLayout>
Java code
public class CustomTabActivity extends Activity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.main);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new TextView(this), "Find Gifts",R.drawable.findgifts);
setupTab(new TextView(this), "Tracked Items",R.drawable.items);
setupTab(new TextView(this), "Preferences",R.drawable.preferences);
}
private void setupTab(final View view, final String tag,int drawable) {
View tabview = createTabView(mTabHost.getContext(), tag, drawable);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text,int drawable) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
ImageView tabicon = (ImageView) view.findViewById(R.id.tabicon);
tabicon.setBackgroundResource(drawable);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
TRY TO use form that
that provide the make tab with image
http://www.codeproject.com/KB/android/AndroidTabs.aspx

Categories