android image display as per user choice on activity - android

I am new to android and would like to know as to why thiswont work.If i have a set of images in my res folder and i want to display them based on users choice such that the Mainactivity is like below:-
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.drawable.img1);//here i put the image name
}
}
Suppose i use this code snippet
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(R.id.text1=='1')
setContentView(R.drawable.img1);
else
setContentView(R.drawable.img2);
}
}
This doesnt work.But i would like to know why,how is the android stuff really working.This does seem to me logically right.

You can't set drawable to setContentView, but you can do this instead:
setContentView(R.layout.MyLayout);
ImageView view = (ImageView)findViewById(R.id.myviewid);
view.setImageResource(R.drawable.img1);
In each click add view.setImageResource(R.drawable.img1); to change the imageView resources.

if you see more carefully the documentation, can see the definition for setContentView method:
public void setContentView (int layoutResID)
if you note, the method receive a layoutResId, the key word is layout, remember that android used the logic (view - controller), an activity is the controller and a layout is the view. You can have visual elements in your view, like an image (in android can be contained in an ImageView).
For example, you can define an xml layout to the next way:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selected_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</ImageView>
this xml only contain an ImageView, in this ImageView with the id selected_image you will show the choice image.
In your activity you need put the next code (imagine the xml layout call image_layout.xml):
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_layout);
//here you need find the ImageView in this layout
((ImageView) findViewById(R.id.selected_image).setImageResource(R.drawable.img1);
}
}

Related

ImageView not changing with setImageResource()?

Here's the code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageView)findViewById(R.id.startButton);
button.setOnClickListener(this);
button.setImageResource(R.drawable.play);
}
public void onClick(View v) {
button.setImageResource(R.drawable.pause);
}
}
Apparently if I put the button.setImageResource(R.drawable.pause);in the onCreate it works and changes images but not in the onClick? I tried debugging it and it ran the onClick code but didn't change the button image. I'm probably making a very stupid mistake but I can't figure it out.
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.larry.app.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
Make sure you use
android:src
to set the initial image in the xml.
you had better set ImageView self OnClickListener
Could you try this code and tell me the result then :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageView)findViewById(R.id.startButton);
button.setOnClickListener(this);
button.setImageResource(R.drawable.play);
}
public void onClick(View v) {
button.setImageResource(R.drawable.pause);
}
}
and in the xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.larry.app.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
In the OnClick there is a switch statement, and when I put the button.setImageResource(R.drawable.pause); before the switch it doesn't always activate even when OnClick activates. When I click one button android.os.SystemClock.sleep is executed which prevents the image from swapping. When the other button is pressed the clock doesn't sleep, so the image can switch successfully.
I stumbled into the same problem Larry faced when using the setImageResource method when changing the source of an imageView. For me this happened when trying to use a switch but for a different scenario. Incase someone would find it helpful, i'll explain how I used a work around for this from the code.
SCENARIO
For me, I had a recyclerView in one activity and based on the selection, it would start another activity using an intent and the widgets in the new activity will be manipulated by the click of the recycler view including an imageView
SOLUTION
Using an If or a Switch inside the new class did not work (Which was the problem). What I did was inside the Custom Adapter for my recycler view, I used an If condition (Again a Switch will not work) as follows,
THE CLASS OF THE ACTIVITY WITH THE RECYCLER VIEW
`#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position)
{
holder.indexTextView.setText(String.valueOf(position + 1));
holder.nameTextView.setText(String.valueOf(name.get(position)));
holder.gradeTextView.setText(String.valueOf(grade.get(position)));
holder.itemView.setOnClickListener(v ->
{
Intent i = new Intent(context, DisplayingProductDetails.class);
if(novaGroup.get(position).equals(1))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_1);
else if(novaGroup.get(position).equals(2))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_2);
else if(novaGroup.get(position).equals(3))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_3);
else if(novaGroup.get(position).equals(4))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_4);
activity.startActivityForResult(i, 1);
});
}`
The Important Part here is passing the Resource ID as a String from the putExtra method.
if(novaGroup.get(position).equals(1))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_1);
else if(novaGroup.get(position).equals(2))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_2);
else if(novaGroup.get(position).equals(3))
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_3);
else
i.putExtra("PRODUCT_NOVA_GROUP", R.drawable.nova_grade_4);
FROM THE CLASS OF THE NEW ACTIVITY
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaying_product_details);
Bundle extras = getIntent().getExtras();
novaGroupImage = findViewById(R.id.novaGroupImage);
novaGroup = extras.getInt("PRODUCT_NOVA_GROUP");
novaGroupImage.setImageResource(novaGroup);
}
The important part here being using the resource ID inside the onCreate Method,
Bundle extras = getIntent().getExtras();
novaGroupImage = findViewById(R.id.novaGroupImage);

Various activities use same views from one layout - how to refactor?

I have six activities like this. I tried to make custom activity that hold ImageViews so I won't have to repeat myself in every activity. Should I leave it as it is or can I make it somehow be in one place and let it be used by everyone (like layout is - it's just one and works):
public class ActivityOne extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.iv1) ImageView iv1;
#Bind(R.id.iv2) ImageView iv2;
#Bind(R.id.iv3) ImageView iv3;
#Bind(R.id.iv4) ImageView iv4;
#Bind(R.id.iv5) ImageView iv5;
#Bind(R.id.iv6) ImageView iv6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.c1));
iv2.setImageDrawable(getResources().getDrawable(R.drawable.c2));
iv3.setImageDrawable(getResources().getDrawable(R.drawable.c3));
iv4.setImageDrawable(getResources().getDrawable(R.drawable.c4));
iv5.setImageDrawable(getResources().getDrawable(R.drawable.c5));
iv6.setImageDrawable(getResources().getDrawable(R.drawable.c6));
}
You can create an abstract activity ImageryActivty that needs to override some method like getContentView which provides a layout id:
public abstract class ImageryActivity extends AppCompatActivity {
#Bind(R.id.iv1) ImageView iv1;
#Bind(R.id.iv2) ImageView iv2;
#Bind(R.id.iv3) ImageView iv3;
#Bind(R.id.iv4) ImageView iv4;
#Bind(R.id.iv5) ImageView iv5;
#Bind(R.id.iv6) ImageView iv6;
public abstract int getContentView();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
ButterKnife.bind(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.c1));
iv2.setImageDrawable(getResources().getDrawable(R.drawable.c2));
iv3.setImageDrawable(getResources().getDrawable(R.drawable.c3));
iv4.setImageDrawable(getResources().getDrawable(R.drawable.c4));
iv5.setImageDrawable(getResources().getDrawable(R.drawable.c5));
iv6.setImageDrawable(getResources().getDrawable(R.drawable.c6));
}
}
And your child activities must inherit from this one:
public class ActivityOne extends ImageryActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public int getContentView() {
return R.layout.activity_one;
}
}
Of course this layout must contain all the ImageView's with the proper id's. For this I´d recommend you to create a reusable layout imagery_layout and include it in each of your child activities:
<?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">
<include
layout="#layout/imagery_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- And here it comes the content for this particular activity in case there's one -->
</LinearLayout>
You can create one abstract BaseActivity activity that will have all the functionality common to those activities and then you can just extend the other activities with it that needs to have that common functionality
or you can simply use one activity and maintained all the states in it using some sort of switch statements all depends on your requirement
You should be able to do this with one Activity. You can pass arguments to Activities as Intent extras. Define some String constants in the Activity:
public static final String ARG_IN_IMAGE_ONE = "ActivityOne.ImageOne";
public static final String ARG_IN_IMAGE_TWO = "ActivityOne.ImageTwo";
...
Set your Drawable ids on creating the Intent:
intent.putIntExtra(ARG_IN_IMAGE_ONE, R.drawable.c1);
...
And read from the intent in onCreate:
iv1.setImageDrawable(getResources().getDrawable(getIntent().getIntExtra(ARG_IN_IMAGE_ONE)));
...
You can also optionally make a static builder which takes the 6 image ids and returns the Intent.

EditText behaving strange on orientation change

See the following Activity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.root);
for (int i = 0; i < 8; i++) {
EditText editText = (EditText) LayoutInflater.from(this).inflate(R.layout.edittextlayout, null);
editText.setText("#" + i);
linearLayout.addView(editText);
}
}
}
The layout R.layout.activity_main:
<?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">
<LinearLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
and the layout R.layout.edittext_layout:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
After starting the app it looks like I would expect: every EditText being filled with it's index.
After rotating the device though, the Activity looks like this:
All the EditTexts are there, but they all contain the same text.
What baffles me even more is that this doesn't happen when creating the EditTexts programmatically with
EditText editText = new EditText(this)
instead of inflating it from a layout.
What's happening there?
You can check out my example and try for yourself here.
EDIT: This is not a duplicate of this question as in my case the text in the EditText does not double but get mixed up between different EditTexts.
Try to set some ID for each View.
For example:
view.setId(id);
Or use
onSaveInstanceState() - onRestoreInstanceState()
for saving info.
The problem is your activity recreates on every orientation and saved instance data handled wrong by Android. You can avoid it by setting an id to your dynamically created views or you can
Override onSaveInstanceState method to save your data like text in your edittexts and recover it on onCreate method like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if we're recreating
if (savedInstanceState != null) {
// Restore value of edittext from saved state
editText.setText(savedInstanceState.getString("edittext"));
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("edittext", editText.getText().toString());
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
If you encounter any problem similar after doing this try to Override onRestoreInstanceState method to prevent android from doing your work.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
//do nothing
}

android wear gridviewpager onClick Listener

I'm developing a simple application in which youb have different spots placed on google map.
When I click on a spot I get its details which are displayed in a GridViewPager.
For now my application is based on the GridViewPager sample available with the sdk.
Here is my layout for the spot details (nothing fancy)
<android.support.wearable.view.GridViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"/>
My problem now is that I'm not able to detect a Click event on a card.
I've tried this but it doesn't work.
public class DetailsActivity extends Activity implements GridViewPager.OnClickListener {
#Override
public void onClick(View v) {
}
I've also tried View.OnClickListener.
Have any idea ?
There are two issues here. First, if you really want to make the GridViewPager clickable, you need to tell it to listen for click events - just implementing the OnClickListener interface isn't sufficient. So you need to do something like this:
public class DetailsActivity ... {
#Override
public void onCreate(Bundle savedInstanceState) {
...
GridViewPager pager = (GridViewPager)findViewById(R.id.pager);
pager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put your onClick logic here
}
});
...
}
}
That being said, however, based on your description it sounds like what you actually want is to set up click handlers on individual pages within the grid, not on the entire grid. If so, then you'll need to do something similar but in each page's Fragment class. For example:
public class MyPageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result = inflater.inflate(...);
result.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put your onClick logic here
}
});
...
return result;
}
}
Note: if you are using CardFragments in your GridViewPager, then you would probably set the OnClickListener in your onCreateContentView override. Otherwise, the above Fragment-based example should apply.

Use single xml layout for multiple activities with different datas

I know this is a very basic question, however as a newbie i cant get to work around it.
So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).
And can somebody tell me what is the most practical way to learn android programming?
UPDATE
This is my current code:
public class TemakiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
}
}
For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?
You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.
If you want a different URL to open for each image button you could set it at runtime as follows
public void onCreate(Bundle b) {
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//different action for each activity
}
});
}
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.same_layout);
TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
aButton.setOnClickListener(aButtonListener);
}
private OnClickListener aButtonListener = new OnClickListener() {
public void onClick(View v) {
// go open url_1 here. In other activities, open url_x, url_y, url_z
finish();
}
};
Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.

Categories

Resources