Best way of making a class for an object? - android

I'm trying to create a game. I need a kitten object with a image displayed.
I need to use this object within multiple activities so I decided it's probably better using an outside class.
I used something like this. But the function findViewById(int) is not aviable from outside an activity. How to do this?
public class Kitten {
Context parent;
View parentView;
ImageView imgBody, imgHead;
Kitten(Context context, View view) {
parent = context;
parentView = view;
//Create body & head
imgBody = new ImageView(parent);
imgBody.setImageResource(R.mipmap.img_kitty_body_white);
addImage(imgBody);
imgHead = new ImageView(parent);
imgHead.setImageResource(R.mipmap.img_kitty_head_white);
addImage(imgHead);
}
private void addImage(ImageView img) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.adoptView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.adoptView);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rl.addView(img, lp);
}
}
The main problem I have, is within the addImage method...

Have you tried using the method from your specific view? Try parentView.findViewById()

You should store the image ID in the "Kitten" object and retrieve it where you need with a getter method, or you can pass the parent View if you want to use the findViewById from there.
But, you don't usually store the Views itself.

Related

How to set position of view while using addView

I'm adding multiple Views by code into Layout. I need each new View to be above previous one(top of the parent layout).
EDIT: To be more accurate I'll describe what the app module should does. User start with clean screen and one button at the bottom of the screen. The button adds a View at the top of the screen. Next clicks should add next views above previous ones to make the newest View be on the top of a container. The app saves state and on restart user see views in the same order.
Call the following method from Button's onClick Event.
private final int LAYOUT_TOP_INDEX = 0;
private void addViewOnTop(View view){
if(layout != null && view !=null)
layout.addView(view, LAYOUT_TOP_INDEX);
}
where 'layout' is your Layout (e.g., LinearLayout) to which the View is to be added.
Would really need more information from you to give a more accurate answer, but if you're saying what i think you are then you can just add these views to a LinearLayout with orientation set to vertical.
And assuming you're iterating through a list to dynamically add views, instead of incrementing from 0, increment down from the size of the list.
for(int i = size; i >= 0; i--){
linearLayout.add(new TextView(Context));
}
View positions inside ViewGroups are defined by the LayoutParams
How does this happen? Views pass their LayoutParams to their parent ViewGroups
//100% programatic approach with simple LayoutParams
LinearLayout myLinearLayout = new LinearLayout(this);
//if the **parent** of the new linear layout is a FrameLayout
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//or if you have the XML file you don't have to worry about this
//myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);
//you could have a LinkedList<TextView>
LinkedList<TextView> textViewList = new LinkedList<>();
//assuming the order is the correct order to be displayed
Iterator<TextView> descendingIterator = textViewList.descendingIterator();
while(descendingIterator.hasNext())
{
//just add each TextView programatically to the ViewGroup
TextView tView = descendingIterator.next();
myLinearLayout.addView(tView);
}
Just like we defined LayoutParams for the LinearLayout we could also define LayoutParams for the TextView
IMPORTANT: when setting LayoutParams you need to be sure they fit the VIEWGROUP, that is the parent of the View being added
private TextView textViewFactory(String myText) {
TextView tView = new TextView(getBaseContext());
//controling the position relatively to the PARENT
//because you are adding the textview to a LINEAR LAYOUT
LinearLayout.LayoutParams paramsExample =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tView.setLayoutParams(paramsExample);
//configuring the insides of the textview
//you can also do all kinds of stuff programatically
tView.setGravity(Gravity.CENTER_HORIZONTAL);
tView.setPadding(10, 10, 10, 10);
tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
tView.setTypeface(Typeface.SANS_SERIF);
tView.setTypeface(null, Typeface.ITALIC);
tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
tView.setId(R.id.aux_info);
tView.setText(myText);
//.........all kinds of stuff really
return tView;
}
If you mean adding a view programmatically so that the new one is added above the previous one, instead of below it, then I suggest this:
Maintain an ArrayList with the items you want to turn into views
Put them into a ListView
When you want to add a new view that must appear at the top of the list, insert it as the first element of your ArrayList and recreate the ListView from it.

How to make a View's Height Dynamic

The code is:
View v = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
v = inflater.inflate(R.layout.newsfeed_custom_listview_facebook, parent, false);
TextView username = (TextView) v.findViewById(R.id.username);
TextView prayerTitle = (TextView) v.findViewById(R.id.prayerTitle);
TextView createdOn = (TextView) v.findViewById(R.id.createdOn);
TextView prayerMessage = (TextView) v.findViewById(R.id.prayerMessage);
ScrollView scrollView = (ScrollView) v.findViewById(R.id.scrollView);
MixedObj menuItemsBean = menuItems.get(position);
RoundedImageView img = (RoundedImageView) v.findViewById(R.id.image);
username.setText(menuItemsBean.name);
prayerTitle.setText(menuItemsBean.title);
createdOn.setText(menuItemsBean.created_on);
TextView textView = new TextView(context);
//prayerMessage.setText(menuItemsBean.message);
RelativeLayout.LayoutParams p = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.image);
textView.setLayoutParams(p);
textView.setPadding(5, 5, 5, 5);
textView.setTextColor(Color.parseColor("#ffffff"));
//textView.setText(menuItemsBean.message);
String message = "please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life end";
message = menuItemsBean.message;
textView.setText(message);
ViewGroup viewGroup = (ViewGroup) v;
viewGroup.addView(textView);
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)
textView.getLayoutParams();
AbsListView.LayoutParams layoutParams = (AbsListView.LayoutParams)
viewGroup.getLayoutParams();
//layoutParams.height += ();
return v;
I want to Change Height of a Custom view Inflated for a listView.
I have tried this but its not working I want a different height on every cell depended upon the message TextView Size.
Anyone please Help..
The problem of your realization it's basics of ListView.
From base object, ListView invokes just single size for all views, and invalidate every action. For your task you need to use your own list. For ex.
1)
Using several layouts:
<ScrollView>
<LinearLayout>
//Add in loop all your views in code above!
</LinearLayout>
</ScrollView>
2)
Using LinkedListView:
This view like ListView, but contains link to every views, which you added, and doesn't invalidate them. So you can use this, and more than simple ListView - you may changes any views in runtime without artifacts. Check this list on Github - LinkedListView
UPDATE:
Using LinkedListView, just import to your project. Than create adapter
public class YourAdapter extends LinkedListView.Adapter {
private Context mainContext;
private LinkedList<View> mainViewList;
public CirclePagerAdapter (Context mainContext) {
this.mainContext = mainContext;
mainViewList = new LinkedList<>();
}
//CALL THIS TO ADD YOUR OWN VIEW
public void addSimpleView (View v) {
mainViewList.add(v)
notifyDatasetChanged();
}
//CALL THIS TO DELETE VIEW
public void deleteView(int index) {
mainViewList.remove(index);
notifyDatasetChanged();
}
#Override
public View getObjectView(int position) {
if (position < mainViewList.size())
return mainViewList.get(position);
return null;
}
#Override
public int getObjectCount() {
return mainViewList.size();
}
After creating adapter call in your Activity!
LinkedListView listView = new LinkedListView (getContext());
LinkedListView.Adapter adapter = new MyBaseAdapter ();
listview.setViewPager(adapter);
Work pretty well!

Android programmatic relative layout: BELOW rule not working?

I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout

Activity.addContentView(View) == ViewGroup.addContentView(View)?

I have a question regarding Android Activitys:
An Activity has the Method addContentView(View) while a ViewGroup has a (similar?) addView(View) Method.
Unfortunately its undocumented where the View from addContentView is placed. Is it like a LinearLayout just adding the View to the bottom, or is it more like a FrameLayout, which adds its Views "onTop" ? Does it depend on the ViewGroup set by setContentView?
If I dive into the sources I see that addContentView will call Window's abstract Method addContentView. Unfortunately I cannot see which class is implementing this Method. So whats the behaviour of Activitys addContentView exactly?
The base layout of every activity is a FrameLayout. This means the layout you usually set via setContentView() is a child of this layout. addContentView() adds just another child, therefore it behaves like a FrameLayout (which means it adds new UI elements above existing ones).
You can check this by using a tool called hierachyviewer from your ANDROID_SDK\tools folder. Here are two screenshots:
This is the layout before calling addContentView(), my activity consists of the default FrameLayout, holding a LinearLayout with a Button (my layout here). This is reflected in the bottom row here, the other elements above are the title/statusbar.
After adding a TextView via addContentView() it looks like this. You can see that the base FrameLayout got a new child.
public void addContentView(View view,
LayoutParams params) {
mActivity.addContentView(view, params);
}
//
public static void SetActivityRoot(Activity c) {
View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup) v.getParent()).removeAllViews();
sv.addView((View) v);
c.addContentView(sv, lp);
}
//
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout =
(LinearLayout)findViewById(R.id.mainlayout);
//newButton added to the existing layout
Button newButton = new Button(this);
newButton.setText("Hello");
mainLayout.addView(newButton);
//anotherLayout and anotherButton added
//using addContentView()
LinearLayout anotherLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button anotherButton = new Button(this);
anotherButton.setText("I'm another button");
anotherLayout.addView(anotherButton);
addContentView(anotherLayout, linearLayoutParams);
}
}

How to align a dynamically create UserInterface using RelativeLayout in Android

I want to create a relative Layout dynamically through code with 2 Textviews one below the other.How to implement android:layout_below property through code in Android.
can anyone help me in sorting out this issue.
Thanks in Advance,
final TextView upperTxt = (...)
upperTxt.setId(12345);
final TextView lowerTxt = (...);
final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(this, null);
params.addRule(RelativeLayout.BELOW, 12345);
lowerTxt.setLayoutParams(params);
Here is my solution for my special Problem.
In case the username wouldn't be found in the db i had to create a RelativeLayout that looks like the xml-generated one.
// text view appears on top of the edit text
enterNameRequest = new TextView(mainActivity.getApplicationContext());
// fill the view with a string from strings.xml
enterNameRequest.setText(mainActivity.getResources().getString(R.string.enterNameRequest));
// edit text appears below text view and above button
enterName = new EditText(mainActivity.getApplicationContext());
enterName.setId(667);
// button appears at the bottom of the relative layout
saveUserName = new Button(mainActivity.getApplicationContext());
saveUserName.setText(mainActivity.getResources().getString(R.string.useUserName));
saveUserName.setId(666);
// generate the relative layout
RelativeLayout layout = new RelativeLayout(mainActivity.getApplicationContext());
layout.setId(668);
// set a background graphic by its id
layout.setBackgroundDrawable(mainActivity.getApplicationContext().getResources().getDrawable(R.drawable.background_head_neutral));
// runtime told me that i MUST use width and height parameters!
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ABOVE, 666);
enterName.setLayoutParams(params2);
LayoutParams params3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ABOVE, 667);
enterNameRequest.setLayoutParams(params3);
LayoutParams params4 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 668);
saveUserName.setLayoutParams(params4);
// add views
layout.addView(enterNameRequest);
layout.addView(enterName);
layout.addView(saveUserName);
/* todo: set button action */
mainActivity.setContentView(layout);
What i found out additionally:
It is not so good to manipulate the layout manually from within java!
You should better use a new Activity and set a new layout in it.
This way, the application-code is readable a lot better!
I even tried to set several layouts (not manually, but wit setContentView) in one activity, and it turned out that i didn't know where what was accessing what else... Also, i had a great problem in adding onClickListeners... so you better use -- android:onClick="myButtonMethod" -- in your button tag in the xml and have a method in your according activity, which uses the layout, like this:
public void myButtonMethod(View v){
// do stuff
}
This improves performance because you are not using additional Listeners - but you use the already available Listener that is bound to your activity in every case.
u can try this
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);``
leftMarginParams.leftMargin = 50;
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)

Categories

Resources