I am trying to put image in TextView. I had done it using image span but my problem is i cannot put onClickListener in every image (in same TextView, there are multipal image in same TextView). Please suggest me how can i do that.
make a custom.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnail_view"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/message_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/thumbnail_view"
android:textSize="18sp"
android:text="MyText" />
</RelativeLayout>
then in main.xml , include this custom.xml
<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_horizontal">
<include
android:id="#+id/customView"
layout="#layout/custom"/>
</LinearLayout>
This is my mainActivity.class
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private String TAG = MainActivity.class.getSimpleName();
ImageView img;
ImageView img1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView)findViewById(R.id.message_view);
img = (ImageView) findViewById(R.id.thumbnail_view);
img1 = (ImageView) findViewById(R.id.thumbnail_view1);
img.setOnClickListener(this);
img1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v== img){
// do something for img
}
else if (v== img1){
//do something for img1
}
}
}
make a custom view instead. That would be much easier.
Related
This question already has answers here:
ImageView image not showing after loading with Picaso and linking to layout file
(4 answers)
Closed 3 years ago.
I have an ImageView Object with image loaded from URL with Picaso. and i would like to show this image next to a radio button as part of a radio button group. I like do this programmatically as the Imageview object is in code. In the code I just add the ImageView to the RadioGroup with addView. Is this the right way to go?
MainActivity.java
package com.example.test.imageslider;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.view.View;
import android.view.ViewGroup;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
private String[] imageUrls = {"http://localhost:8080/test.jpg"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//viewPager = (ViewPager) findViewById(R.id.viewPager);
//ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
//viewPager.setAdapter(viewPagerAdapter);
ImageView imageView = new ImageView(this);
Picasso.get()
.load(imageUrls[0])
.fit()
.centerCrop()
.into(imageView);
RadioGroup ll = new RadioGroup(this);
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
ll.addView(rdbtn);
ll.addView(imageView);
((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
}
}
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.test.imageslider.MainActivity">
<!--<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginBottom="8dp"/> -->
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RadioGroup>
</RelativeLayout>
I did not to try to run this yet as I do not how to link the ImageView object in the code with the one with the layout file. Can someone show how to do this?
If you are reffering to your xml
You must add an ID to your Image in xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myImage"
/>
In your activity
ImageView imageView;
//this will set the image to your ImageView in your xml
imageView = (ImageView)findViewById(R.id.myImage);
Picasso.get()
.load(imageUrls[0])
.fit()
.centerCrop()
.into(imageView);
I have an app in which i want to assign a bitmap named original to an image view whose id is img1 in the layout by taking the position of the image view in the layout(here it is child at position 0). When i click the save button,i want to assign bitmap named original to image view whose id is img1 in the layout.I know we can assign by using ourImageView.setImageBitmap(bitmaptobeassigned). But in my app,my situation is to assign the bitmap original by means of View object child only.Please help me. My main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View child=frame.getChildAt(0);
//Here i obtained image view as a child.But I do not know how to assign bitmap to it.
//please help me how to write code here.
}
});
}
}
and my xml file is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I have found my answer.my main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView child=(ImageView) frame.getChildAt(0);
child.setImageBitmap(original);
}
});
}
}
and my xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I have created a new .xml file named "new_game.xml" and a class named "New_Game.java"
These are not Main activity
new_game.xml:
<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"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="#+id/button"
android:layout_marginTop="27dp"
android:layout_alignRight="#+id/editText"/>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="16dp"
android:layout_below="#+id/button"
android:layout_marginLeft="11dp"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:background="#drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/button"/>
</RelativeLayout>
New_Game.java:
package com.example.logosquiz;
import android.app.Activity;
import android.app.ListActivity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class New_Game extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game);
Button button1 = (Button)findViewById(R.id.button);
final EditText editText1 = (EditText)findViewById(R.id.editText);
ImageView imageView1 = (ImageView)findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText1.setText("Hello Android");
}
});
}
Please explain it very well
I put on AndroidManifest.xml this
<activity android:name=".New_Game"> /activity>
and this on the event onClick
Intent myIntent = new Intent(v.getContext(), New_Game.class);
startActivityForResult(myIntent, 0);
Your XML is wrong. Remove the last / from the first RelativeLayout in your XML.
<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">
instead of
<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"/>
Although Eclipse should have warned you about this.
your code is correct but
button android:layout_marginTop="27dp"
edittext android:layout_marginTop="16dp"
edit text is placed on the button so when you click you are clicking on edittext not button change edittext marginTop to 30dp and check
public class New_Game extends Activity {
EditText editText1;
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game);
Button button1 = (Button)findViewById(R.id.button);
editText1 = (EditText)findViewById(R.id.editText);
ImageView imageView1 = (ImageView)findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
editText1.setText("Hello Android");
}
});
}
I am developing an application for children. Basic maths for children to learn with fun.
In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.
Is any one know solution for this or any one having source code thn help me
this is the way you can do it ,quite simple though :)
your xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input2" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Result"/>
<HorizontalScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear3"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and you Activity looks like this
package com.example.stackanswer;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AppleActivity extends Activity {
private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
final EditText input1 = (EditText)findViewById(R.id.input1);
final EditText input2 = (EditText)findViewById(R.id.input2);
scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
linear1 = (LinearLayout)findViewById(R.id.linear1);
linear2 = (LinearLayout)findViewById(R.id.linear2);
linear3 = (LinearLayout)findViewById(R.id.linear3);
Button output = (Button)findViewById(R.id.button);
output.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input1A = input1.getText().toString();
String input1B = input2.getText().toString();
int value1 = Integer.parseInt(input1A);
int value2 = Integer.parseInt(input1B);
Log.i("1",""+value1);
Log.i("2",""+value2);
linear1.removeAllViews();
linear2.removeAllViews();
linear3.removeAllViews();
linear1.setScrollContainer(true);
ImageView image ;
for(int i=0;i<value1;i++){
image = new ImageView(getApplicationContext());
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear1.addView(image);
scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
for(int i=0;i<value2;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear2.addView(image);
scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
int sum = value1+value2;
for(int i=0;i<sum;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear3.addView(image);
scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
});
}
}
Complete source code here
We have create a custom dialog box.
How do we set the custom dialog box at the Middle of the center position.
Sample code is dialog.getWindow().setGravity(Gravity.CENTER);
Now custom dialog box is displayed at the Top Of the center position.
Thanks in advance.
TestAActivity.java
package com.TestA;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TestAActivity extends Activity {
private Button btn;
/** Called when the activity is first created. */
// TwitPicResponse tpResponse = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
show_Dialog();
}
});
}
protected void show_Dialog() {
// TODO Auto-generated method stub
//Context mContext = getApplicationContext();
Dialog dialog = new Dialog(TestAActivity.this);
dialog.setContentView(R.layout.test);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
}
}
enter code here
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
see below links it is default in CENTER.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog