i have some imageviews in my project with this id's: stagebtn_1,stagebtn_2,stagebtn_3 and...
by default all of them points to an image nad works perfectly...
now i want to change picture of some of them inside in a loop with this code:
for (int i=1;i<=3;i++) {
int imageViewId = getResources().getIdentifier("stagebtn_" + i, "id", "com.english.game");
ImageView imageView = (ImageView) findViewById(imageViewId);
imageView.setBackgroundResource(R.drawable.pic1);
}
but it doesnt work in the way that i want, this code doesnt change pictures, instead creates 3 new imageview with new picture...
how i can solve this?
you can use this way
public class MainActivity extends Activity {
ImageView imageView;
Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imageView = (ImageView) findViewById(R.id.img);
for(i=0;i<2;i++){
imageView.setImageResource(image[i]);
}
}
}
Related
i am creating a CAROUSEL view, that can be swipe left or right, but the problem that i am having is, i have 5 images and 1 XML view to be need to put in the carousel function, the 5 images is already working but for the last bullet in the carousel a view should be loaded on it, and it can still be swipe if they want to go back to the 5th images or to the first image.
here is the code:
public class AboutHelp extends AppCompatActivity {
CarouselView carouselView;
int[] sampleImages = {R.drawable.help_1, R.drawable.help_2, R.drawable.help_3, R.drawable.help_4, R.drawable.help_5,R.layout.activity_send_feed_back};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_help);
androidx.appcompat.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
carouselView = findViewById(R.id.aboutCarouselHelp);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
}
ImageListener imageListener = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageResource(sampleImages[position]);
}
};
is this possible? can i load an XML to the last bullet? any help would be really appreciated.
You should use ViewListener instead of ImageListener. Try this
carouselView.setViewListener(viewListener);
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
if (position < 5) {
ImageView imageView = new ImageView(AboutHelp.this);
imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)); //setting image position
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(sampleImages[position]);
return imageView;
} else {
View customView = getLayoutInflater().inflate(sampleImages[5], null);
return customView;
}
}
};
I want to generate multiple TextViews based on how many semicolons the users enters between inputs in an EditText.
And it works so far, the only problem is that all TextViews are on top of each other, so I need to have the second TextView be positioned below the first and so on.
I tried to use setY but it did not change the position of the Views or they disappeared completely.
Is there a method to achieve this or is this a case for the LayoutInflater?
public class MainActivity extends AppCompatActivity {
private EditText et1;
private ScrollView sv1;
private ConstraintLayout cl1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.editText);
sv1 = findViewById(R.id.scrollView);
cl1 = new ConstraintLayout(this);
sv1.addView(cl1);
}
public void splitToChips(View v) {
String content = et1.getText().toString();
String[] products = content.split(";");
TextView[] textViews = new TextView[products.length];
for(int i = 0; i<products.length; i++) {
textViews[i] = new TextView(this);
cl1.addView(textViews[i]);
textViews[i].setText(products[i]);
}
}
}
You should add views to LinearLayout. In my case, I created a layout with a Button, EditText, and LinearLayout along with a vertical orientation inside of a ScrollView. Then, in the Java code, I simply added the following line to the loop:
mLinearLayout.addView(textViews[i]);
I have made image slider using viewPager and picasso. Images are loaded from my own(raw). I've put share button below image.
I want to make specific image be shared using share intent. I mean, if I'm in image of position 3 then only image of position 3 should be shared.
Activity where image slider is shown:
public class RajeshDaiActivity extends AppCompatActivity{
ViewPager viewPager;
private int[] imageUrls = new int[]{
R.drawable.oq,
R.drawable.oqqqq,
R.drawable.opt3
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rajesh_dai);
viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView ShareButton = findViewById(R.id.share);
ShareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent share = new Intent("android.intent.action.SEND");
//?? how??
}
});
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
}
From the code posted above, it looks like you need to implement an interface and capture the click event of the particular image and implement the logic for redirection in overridden implementation of the particular function in your fragment class. Hope this clarifies your question.
I have the following OnCreateMethod()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_siege_main);
mContext = getApplicationContext();
gridview = (GridView) findViewById(R.id.gridview_board);
initialBoard = new Board();
initialBoard.generate(Globals.rand_width,
Globals.rand_height,
Globals.rand_low,
Globals.rand_high);
gridview.setAdapter(new BoardAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//updateGameState(initialBoard);
}
});
updateGameState(initialBoard);
//typography
TextView boardName = (TextView) findViewById(R.id.textview_board_name);
boardName.setText("Uncharted Territory");
Toast.makeText(SiegeMainActivity.this, "Uncharted Territory loaded", Toast.LENGTH_SHORT).show();
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
boardName.setTypeface(face);
TextView pl1Name = (TextView) findViewById(R.id.textViewPlayer1Name);
face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
pl1Name.setTypeface(face);
TextView pl2Name = (TextView) findViewById(R.id.textViewPlayer2Name);
face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
pl2Name.setTypeface(face);
}
and my updateGameState() method is like this:
private void updateGameState(Board b) {
int [] vals = b.getLinearVals();
int [] plays = b.getLinearPlays();
int [] score = b.getScore();
int turns = b.getTurns();
Button tile;
//updating board
for(int i=0;i<gridview.getChildCount();i++){
tile = (Button)gridview.getChildAt(i);
tile.setText(""+vals[i]);
if(plays[i]==1)
tile.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_default_normal_green));
else if (plays[i]==2)
tile.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_default_normal_blue));
}
//Setting the score
Button pl1Score, pl2Score;
pl1Score = (Button) findViewById(R.id.buttonPlayer1Score);
pl2Score = (Button) findViewById(R.id.buttonPlayer2Score);
pl1Score.setText(""+score[0]);
pl2Score.setText(""+score[1]);
}
When I call updateGameState() in OnCreate after creating the adapter, it should fill the buttons with some values. But it doesn't seem to work. But when I do the same thing on a click (commented out in the click listener), it seems to work fine. I want to populate the values on loading the page. Can somebody help me?
You need to let the GridView know that the data has changed. If you are dynamically changing the data in the ArrayAdapter, you need to call BoardAdapter.notifyDataSetChanged() so the GridView can "refresh" itself with the new data.
In your case, you need to create a reference to you new BoardAdapter and then use that to call the notifyDataSetChange() method.
private BoardAdapter boardAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
// your things
boardAdapter = new BoardAdapter(this);
gridview.setAdapter(boardAdapter);
// more things
}
private void updateGameState(Board b) {
// update your data
boardAdapter.notifyDataSetChanged();
}
Edit: You mentioned in the comment that you tried using an ArrayAdapter and it didn't work for you... maybe try this, which says to use the GridViews invalidateViews() method. gridView.invalidateViews()
Have a look at this SO question & answer here:
How to refresh a GridView?
I had a similar problem, but couldn't get it to even refresh on a button click, even tho items were being changed and added. In my case, the trick, as it turns out, was to recreate the (in your case) BoardAdapter with the new/updated list of items, and then reassign it to the gridview.
gridViewAdapter = new MyGridViewAdapter(this, blah, yourUpdatedItems);
discoverGridView.InvalidateViews();
discoverGridView.Adapter = gridViewAdapter;
I am sure there has to be a better, more reactive, way of doing it, but this at least got my proof of concept up and running.
Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}