application starts in black screen - no view visible - android

I'm trying to setup a simple view onto the root of phoneGap but i get black screen.
This is the code of a class to inflate and load from xml:
public TopBar(Context context){
myActivity = (Activity)context;
}
protected View createTopBarView() {
RelativeLayout mainLayout = new RelativeLayout(myActivity);
mainLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LayoutInflater inflater = myActivity.getLayoutInflater();
View layout = inflater.inflate(R.layout.top_bar, null);
topBarView = (LinearLayout) layout.findViewById(R.id.linearLayoutTopBar);
/**
* Setting Buttons....
*/
//Adding the View
mainLayout.addView(topBarView);
return mainLayout;
}
The method returns a view, and in a class that extneds droidGap i do:
private void createLayout(){
mainRelative = new RelativeLayout(this);
mainRelative.setBackgroundResource(R.drawable.background);
View topBarView = new TopBar(this).createTopBarView();
mainRelative.setVisibility(View.VISIBLE);
mainRelative.addView(topBarView);
root.addView(mainRelative);
}
This function is called from onCreate, But i get a black screen when running.

Can you try to add
mainLayout.addView(layout);
instead of
mainLayout.addView(topBarView);
I might to help
EDIT:
Have you done this: setContentView(yourLayout, LayoutParams); in onCreate?

Related

LayoutParams dont update after switching fragments

I have some code on an image click handler to move a "selected tick" image next to the image that was clicked. This code works fine but after I switch to a new fragment and back the image no longer moves.
I believe that the layout parameters to not update immediately on calling SetLayoutParams and that this happens somewhere down the line but I dont understand why this stops happening when the fragement is reloaded given that its the same code that is being called.
public void Item_Click(View view){
ImageView button = (ImageView) view;
Selector = (ImageView) findViewById(getResources().getIdentifier("Selector_Connected, "id", this.getPackageName()));
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); //new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
Selector.setVisibility(View.VISIBLE);
}
A fragment is swapped by next / previous buttons
bundle.putInt("answer",current_survey.get_question1() );
QuestionOne Q1Frag = new QuestionOne();
Q1Frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, Q1Frag).commit();
When the new fragment is loaded this too moves the selector based on the information passed into it.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.questionone, container, false);
Selector = (ImageView) view.findViewById(R.id.Selector_Connected);
int answer = getArguments().getInt("answer");
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); // new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// lp.addRule(RelativeLayout.LEFT_OF, button.getId());
ImageView button = (ImageView) view.findViewById(getResources().getIdentifier("Connected_" + i, "id", view.getContext ().getPackageName()));
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
return view;
}
As mentioned, all of this code works fine until moving to a new fragment and moving back again, the code is still called but the selector never moves!
Any help is appreciated.
I was having this problem before
just make a new instance of LayoutParams and set all the properties you have in the xml to it programmatically so for example
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
and than
Selector.setLayoutParams(params);
hope it helps

Clear View in Android

I am calling the function setLayoutData() for many places i want to clear the view whenever it is called.What is happening now the layout get appends .But i don't want to append how can we clear the view .Please help me this is my function
void setLayoutData() {
resultTrips = (LinearLayout) findViewById(R.id.trip_list);
LinearLayout.LayoutParams mainTripDetailsLayout = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0;i<4;i++) {
final LinearLayout tripdetailsdata = (LinearLayout) inflater
.inflate(R.layout.mytripinner, null);
tripdetailsdata.setId(i);
resultTrips.addView(tripdetailsdata);
i++;
}
TextView dummy = new TextView(this);
dummy.setLayoutParams(mainTripDetailsLayout);
resultTrips.addView(dummy);
}
If I understand you right, you want to clear your LinearLayout?
so put in your second line after findView... resultTrips.removeAllViews();
The better aproach would be only adding one TextView to your Linearlayout and edit this one.
You can use resultTips.removeAllViews(); to remove the resultTips layout or resultTips.removeView(dummy); to remove the TextView. Hope it helps.

how to insert a new LinearLayout after a specific one everytime i click a button?

I want to build some sort of twitter application. In DashboardActivity i need to add a status box everytime i click the "Post" button.My dashboard xml looks like this:
<RelativeLayout>
<LinearLayout></LinearLayout> -->header layout
<LinearLayout></LinearLayout> -->some layout with some titles
<LinearLayout></LinearLayout> --> post status layout with the post button
<LinearLayout></LinearLayout> --> layout with a horizontal rule
<LinearLayout></LinearLayout> --> this is the layout with id "rootStatusBox" where i want to add the status box
</RelativeLayout>
Now, i want to be able to add a new LinearLayout after the horizontal rule layout everytime i click the "Post" button.
I tried something like this in my DashboardActivity:
postStatus.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addUserStatusBox(firstname,lastname,status);
}});
And addUserStatusBox() looks like this:
public void addUserStatusBox(String firstname, String lastname,String status) {
LinearLayout rootStatusBox = (LinearLayout)findViewById(R.id.rootStatusBox);
LinearLayout userStatusBox = new LinearLayout(this);
userStatusBox.setOrientation(LinearLayout.HORIZONTAL);
userStatusBox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 300, 0, 0); // llp.setMargins(left, top, right, bottom);
userStatusBox.setLayoutParams(layout);
TextView friendName = new TextView(this);
TextView friendStatus = new TextView(this);
TextView dataCrearePost = new TextView(this);
friendName.setText(firstname+ " " + lastname);
friendName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
friendName.setTextSize(10);
friendName.setTypeface(null, Typeface.BOLD);
friendStatus.setText(status);
friendStatus.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(-70, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
friendStatus.setLayoutParams(llp);
friendStatus.setTextSize(10);
userStatusBox.addView(friendName);
userStatusBox.addView(friendStatus);
rootStatusBox.addView(userStatusBox);
}
This is working only for the first time when i add a status.I don't know how to add more posts after the horizontal rule layout and to be able to see the old posts below my new one.I would appreciate a little bit of help.Thank you
I would use a customized list view for this purpose.
You need to create the following:
Layout for ListItem: This represents single row in the list. You can customize it by creating separate layout for this. Say you create: listitem_post.xml
Adapter: Write an adapter by extending BaseAdapter class (say: PostsAdapter.java). Fill in all the overridden methods. Most importantly, in the getView() method, inflate the post_listitem. Assign that to convertView object (which is passed in as an argument).
public View getView(int index, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem_post, parent, false);
}
//Code other parts
return convertView;
}
Activity: In your xml code of activity, insert a ListView say listview_posts. In the java file for the activity, set adapter created in step 2 for listview_posts inside onCreate() method.
PostsAdapter postsListAdapter = new PostsAdapter();
ListView postsListView = (ListView) this.findViewById(R.id.listview_posts);
postsListView.setAdapter(postsListAdapter);
That is how you specify that each list element is listitem_post.
Follow this tutorial

Setting Layouts in ViewPagerIndicator?

Hi there I am having a bit of struggle trying to use Jake's ViewPagerIndicator. My first problem is how can I set different layouts for different page in the ViewPagerIndicator ?.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setText("Hello!");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.addView(text);
return layout;
}
I have edited the sample code very briefly but I do not know how to change layouts between different page swipes and I would also like to know how to set the id of the items that I am creating in the ViewPagerIndicator so I can make responses ?.
I have tried to say something like
text.onclicklistener
But I keep on having errors with this code. I am now also wondering whether or not I will have to set an ID of all my items that I create inside of his class. I also tried this but with no success. Do i have to use an ID in order to use the OnClickListener .etc?
text.setId("android:id#hello");
Thanks
You can do so by tweaking the code !
First comment this part:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
Second add this: fragment.mContent = content; // below the loop
third you have to inflate your layouts inside onCreateView():
View view=null;
if(mContent.equals("title1"))
{
view = inflater.inflate(R.layout.title1, container, false);
}
else if(mContent.equals("title2"))
{
view = inflater.inflate(R.layout.title2, container, false);
}
return view;

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);
}
}

Categories

Resources