Rotating spinner arrow in android by 180 - android-layout

Issue:
I needed to implement Spinner for RTL languages such that text is aligned to right and down arrow towards left.I am able to acheive this alignment but the arrow is pointing upwards.
I do not want to use setRotation since it's available only from API level 11.
PFB the code I am using:
//Creating spinner
Spinner s=new RTLSpinner(this);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
//Fetching the background drawable of Spinner
NinePatchDrawable drawable=(NinePatchDrawable)(((StateListDrawable)s.getBackground()).getCurrent());
//Converting it into Bitmap
Bitmap bmpOriginal = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpOriginal);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
//Rotating the image
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(180, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
s.setBackgroundDrawable(new BitmapDrawable(bmResult));
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId,
List<String> textArray) {
super(context, textViewResourceId, textArray);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView viewObj = (TextView) super.getView(position, convertView,
parent);
TextView optionView = (TextView) viewObj
.findViewById(android.R.id.text1);
optionView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
return viewObj;
}
}
Thanks in Advance!

Related

How do I put the adapter into the viewpager?

This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.

dynamically adding textviews to listview item

I have this arryalist of items, each item has hour as string and string of minutes. I can split minutes to array and for each minute I want to add textview with on clicklistener.
I have implemented arrayadapter for listview. Everything works like expected but only one thing : I don't see all textviews as they are not shown properly on the end of the display.
ListView row item layout has LinearLayout and textview to show hours.
Code for listview adapter :
public class DotazAdapter extends ArrayAdapter<Hours> {
private final ArrayList<Hours> model;
private final Context context;
public DotazAdapter(Context context, ArrayList<Hours> model) {
super(context, R.layout.hours_minutes,model);
this.model=model;
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = null;
row = inflater.inflate(R.layout.hours_minutes, parent,false);
LinearLayout riadok = (LinearLayout)row.findViewById(R.id.horny);
TextView hodiny = (TextView)row.findViewById(R.id.hodinyBTV);
hodiny.setText(model.get(position).getHour());
String[] minuty = model.get(position).getMinutes().split(" ");
for(int i = 0; i<minuty.length;i++){
final TextView minutka = new TextView(context);
minutka.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
minutka.setText(minuty[i]+" ");
minutka.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Stlacil si riadok "+position+ " a cislo v riadku :", Toast.LENGTH_SHORT).show();
}
});
riadok.addView(minutka, );
}
return row;
}
}
Layout for listitem :
xml version=1.0 encoding=utf-8
LinearLayout xmlnsandroid=httpschemas.android.comapkresandroid
androidid=#+idhorny
androidlayout_width=match_parent
androidlayout_height=wrap_content
TextView
androidid=#+idhodinyBTV
androidlayout_width=wrap_content
androidlayout_height=wrap_content
androidlayout_marginRight=5dp
androidbackground=#F00C0C
androidtextColor=#FFFFFF
androidtextSize=18sp
LinearLayout

Imageview does not resize after ScaleTransition

I am trying to display three images in sequence with some scale transformation to each of them(images of 1,2,3). I am using ScaleTransition on single instance of imageview, after each successive transformation the imageView height get scaled which is not resetting
My Code is
public void start(Stage primaryStage) {
final IntegerProperty intProperty = new SimpleIntegerProperty(0);
final Image[] array = {
(new Image(WaitEffect.class.getResource("1.png")
.toExternalForm())),
(new Image(WaitEffect.class.getResource("2.png")
.toExternalForm())),
(new Image(WaitEffect.class.getResource("3.png")
.toExternalForm())) };
Group root = new Group();
Scene scene = new Scene(root, 400, 400, Color.BLACK);
StackPane pane = StackPaneBuilder.create()
.layoutX(scene.getWidth() / 2).layoutY(scene.getHeight() / 2)
.build();
final ImageView imageView = new ImageView();
pane.getChildren().add(imageView);
Timeline timeline = new Timeline();
int count = 0;
KeyValue value = new KeyValue(intProperty, count++);
KeyFrame frame = new KeyFrame(Duration.millis(2000),
new EventHandler<ActionEvent>() {
int co = 0;
#Override
public void handle(ActionEvent paramT) {
imageView.setImage(array[co]);
ScaleTransition st = ScaleTransitionBuilder.create()
.byX(2).byY(2).node(imageView)
.duration(Duration.millis(500)).build();
st.play();
imageView.setFitHeight(150);
imageView.setFitWidth(150);
imageView.resize(150, 150);
co++;
}
}, value);
timeline.getKeyFrames().add(frame);
timeline.setCycleCount(3);
timeline.play();
root.getChildren().addAll(pane);
primaryStage.setScene(scene);
primaryStage.show();
}`
ImageView doesn't reset it's properties after setting new Image. You need to reset it yourself, e.g. by setting from properties of ScaleTransition:
ScaleTransition st = ScaleTransitionBuilder.create()
.fromX(1).fromY(1)
.byX(2).byY(2).node(imageView)
.duration(Duration.millis(500)).build();

Set gravity of a layout

I'm trying to set right gravity to a linear layout in the OnPreExecute method,
but the dialog still shows my hebrew text in the left side.
What is the problem with my code ?
private ProgressDialog dialog;
private Context context;
private LinearLayout layout;
public MyTask(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
layout = new LinearLayout(context);
}
protected void onPreExecute() {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -2);
layout.setGravity(Gravity.RIGHT);
TextView loadMsg = new TextView(context);
loadMsg.setText("טוען...");
loadMsg.setGravity(Gravity.RIGHT);
layout.addView(loadMsg, params);
dialog.setView(layout);
// dialog.setContentView(R.layout.loaddialog);
// dialog.show();
}
I have tried to fix your problem try to use it. It is working for me.
LinearLayout lp = new LinearLayout(getApplicationContext());
lp.setMinimumWidth(100);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setGravity(Gravity.RIGHT);
TextView loadMsg = new TextView(this);
loadMsg.setText("my msg");
loadMsg.setTextColor(Color.WHITE);
loadMsg.setGravity(Gravity.RIGHT);
lp.addView(loadMsg, params);
Dialog dialog = new Dialog(this);
dialog.setContentView(lp);
dialog.show();

Android : set visibility of individual buttons in a custom ListView

I have a ListView with a custom Adapter (extending BaseAdapter), and the layout of each row consists of a TextView and a Button. After the list is created, I want to change the visibility of specific buttons.
For this, I would need to programatically access the View of an individual row. I am not able to find out how to do it. This answer mentions getView(int position), but I can't find that method; getView() needs 3 parameters. What do I pass as convertView to getView(int position, View convertView, ViewGroup parent)?
Could you please point me in right direction?
UPDATE: The View obtained by View v = myListView.getChildAt(myListView.getFirstVisiblePosition()); is null. Also, myListView.getChildCount() is returning 0.
onItemClick will pass you the View in the ListView that was clicked. Then you can grab that View and do whatever you want with it, including changing visibility, etc.
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
Here is an example I of a custom adapter I used in a list view in which the user can select items in the listView and mark them for Delete. The key is to add the onClickListener after you have a view. Then you can use that to not only change the view, but update the data of the adapter too.
Hopefully you can modify this code to suit your particulars.
private class DeletePlayerAdapter extends ArrayAdapter<Player> {
Context context;
int layoutResourceId;
ArrayList<Player> data;
public DeletePlayerAdapter(Context context, int layout,
ArrayList<Player> list) {
super(context, layout, list);
this.layoutResourceId = layout;
this.context = context;
this.data = list;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = convertView;
PlayerHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PlayerHolder();
holder.player_name = (TextView) row
.findViewById(R.id.player_name);
holder.player_number = (TextView) row
.findViewById(R.id.player_number);
holder.seeded_button = (ImageButton) row
.findViewById(R.id.delete_toggle);
holder.player_name.setTypeface(tf);
holder.player_number.setTypeface(tf);
row.setTag(holder);
players_array.get(position).marked_for_delete = false;
} else {
Log.d("PLAYER_ADAPTER", "NOT_NULL ROW");
holder = (PlayerHolder) row.getTag();
}
holder.seeded_button.setOnClickListener(new OnClickListener() {
private int pos = position;
public void onClick(View v) {
ImageButton b = (ImageButton) v;
if (b.isSelected()) {
b.setSelected(false);
players_array.get(pos).marked_for_delete = false;
} else {
b.setSelected(true);
players_array.get(pos).marked_for_delete = true;
}
}
});
Player p = data.get(position);
holder.player_name.setText(p.name);
holder.player_number.setText(String.valueOf(position+1));
holder.seeded_button
.setSelected(players_array.get(position).marked_for_delete);
return row;
}
}
static class PlayerHolder {
TextView player_number;
TextView player_name;
ImageButton seeded_button;
}

Resources