Android Create a TextView for every string in my Sqlite DB - java

I am trying to display a new toast for every item in my cursor, how would I do this? I've searched SO, but can't find any relevant, useful answers. Here is my code, but it doesn't work:
while(mNotesCursor.moveToNext()){
Toast.makeText(getActivity(),
mNotesCursor.getString(mNotesCursor.getColumnIndex("title")),
Toast.LENGTH_LONG).show();
}

Toasting while iterating through the the cursor is not the best idea. Here's why:
You are using LENGTH_LONG, and the that means that a toast would last for approx 3 seconds.
Whereas your for loop would probably finish execution in a fraction of a second. So the toast would be displayed in order, but they would transition so slowly that it probably wouldn't make sense.
So i would suggest you to display the content in an alert dialog or the activity itself so the user would be able to make more sense out of the content.
EDIT:
I assume you are executing this on the main thread.
LinearLayout root = (LinearLayout) getActivity().findviewById(R.id.rootLayout);
while(mNotesCursor.moveToNext()){
TextView tv = new TextView(getActivity());
tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
root.addView(tv);
}

if you are looking to add textview to your view dynamically then here is how you can do it
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/lineralayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
inside your activity class
LinearLayout l = (LinearLayout) findViewById(R.id.lineralayout1);
while(mNotesCursor.moveToNext()){
TextView tv = new TextView(this);
tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
l.addView(tv);
}

Related

how to set android ImageView visibility inside an included layout?

We are using android API 17 in our application. I have defined a layout containing two images vies as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_container_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_1_resource"/>
<ImageView
android:id="#+id/image_2"
android:layout_marginTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_container_layout"
android:src="#drawable/image_2_resource"/>
This layout is included inside another layout as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/wizard_content_style"
tools:context=".ui.Wizard"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
>
<include layout="#layout/image_container_layout"
android:id="#+id/included_view"
/>
<TextView
style="#style/wizard_content_text_style_medium"
android:id="#+id/text_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/included_view"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="#string/instruction"
android:layout_marginBottom="15dp"/>
The reason that the layout is included is that we want to reuse it in two more layouts.
Now based on some condition I want to hide or show the image views inside image_container_layout.
The java code looks like this:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
switch (accuracy) {
case 1:
log().i(getClass().getSimpleName(), "case 1 chosen");
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
log().i(getClass().getSimpleName(), "image 1 has been shown");
break;
case 2:
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
break;
case 3:
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.VISIBLE);
break;
}
I am debugging this code and I am sure the code is running. The log messages are printed in Logcat as well, but nothing happens no change in the images. Also, both images are always shown.
I wonder if there is something that I have to do when working with the included layout?
Thanks for any help in advance.
Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface.
Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active.
Why are you complexing with so much code. If you include some layout in your xml then you can use those widgets also same as the xml have. There is no need to inflate.
ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);
You said at this comment the code not inside activity but wherever it is you inflated a new layout to your view currently displaying by this line:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
When you try to change visibility of those images actually it works, i think so. But if your activity or fragment layout contains image_container_layout maybe you see
those images.
And I wonder that what do you do with inflated view containerLayout. Do you add it to inside of any other view. If you dont it wont be visible for you.
you have to use it like this:
View included_view1 = findViewById(R.id.included_view1);
ImageView image_1 = included_view1.findViewById(R.id.image_1);
ImageView image_2 = included_view1.findViewById(R.id.image_2);
image_1.setVisibility(View.VISIBLE);
image_1.setVisibility(View.GONE);
image_2.setVisibility(View.VISIBLE)
image_2.setVisibility(View.GONE)
View included_view2 = findViewById(R.id.included_view2);
ImageView image_11 = included_view2.findViewById(R.id.image_1);
ImageView image_22 = included_view2.findViewById(R.id.image_2);
image_11.setVisibility(View.VISIBLE);
image_11.setVisibility(View.GONE);
image_22.setVisibility(View.VISIBLE)
image_22.setVisibility(View.GONE)
Above code will be helpful in the case of multiple time you want to use same layout.

How to add an element between two elements android

I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. I am just wondering if this is possible, as i have been unable to find any related questions?
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3 then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.
Inside Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?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"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want :
android:visibility="invisible" it won't be visible but it's going to take place in your view
android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});

I would like to add multiple Strings to my android Button programmatically (not through XML)

I'm trying to format the three Strings into an android button. The first string should be about 30% of the button, second should be about 50%, and the rest to the third button. Each text should be contrained within a certain length. How can I do this?
After searching through many possible solutions, what I came up with was to create 3 different buttons and put them together. This wouldn't be ideal but it's a start. Are there better ways to do this?
This is what I have tried so far:
The Java:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout_buttons);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button addButton = new Button(this);
Button numPlayerButton = new Button(this);
Button lobbyButton = new Button(this);
Button locationButton = new Button(this);
ll.addView(addButton, lp);
ll.addView(numPlayerButton, lp);
ll.addView(lobbyButton, lp);
ll.addView(locationButton, lp);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearlayout_buttonlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearlayout_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- THIS IS WHERE THE BUTTONS GO!!!!!! -->
</LinearLayout>
</LinearLayout>
</ScrollView>
My goal with this code was to have the buttons created horizontally with the inner linear layout, and I thought the outer linear layout will create the next iteration of buttons on the next line (vertically). I have the buttons created programmatically through a loop.
If you want to click a widget then do something(as a button does), you can use a Layout and add a onClickListener to it. In this Layout, you can add strings as you want.
Simply do it like that:
Write a RelativeLayout in your XML and give it a id.
Add three textViews in RelativeLayout and place them as you want.
Register this RelativeLayout in Java and add a onClickListener to it.
For Java part:
RelativeLayout ThreeStringLayout = (RelativeLayout) view.findViewById(R.id.three_string_layout);
ThreeStringLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
//do things here
}
});
I implemented it in a Fragment, you need to modify it if you use it in Activity.
Good Luck.
This cannot be achieved using a standard Android Button (that I know of). You will have to use a linear layout with textviews. I can explain how that would work if need help

String is too long for Toast - How to display end of string rather than beginning

Ok this is probably a really simple one. I have a string value that I want to display in a toast. For Example...
String tempstring = "qwertyuiopasdfghjklzxcvbnm123456789012345678901234567890";
toast = Toast.makeText(getApplicationContext(), tempstring , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
The problem I have is that tempstring in my program can often get quite big and the amount of text is sometimes bigger than the toast can display. When this happens the toast displays the text from the beginning and cuts of the end. I want the toast to always show the end of the string but cut of the beginning if the text is too much to display at one time in the toast. Is there an easy way to do this please?
Ok worked this out myself.
Its not too hard...
The answer is to create a custom view for the toast. First create an XML file like so... NB The important bit to note is the gravity setting.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="horizontal"
android:padding="5dp"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
android:gravity="bottom"
/>
</LinearLayout>
Then reference the view like so from your toast...
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(themessage);
toast = Toast.makeText(getApplicationContext(),
themessage, Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Job done...
This is what you need. It is implemented in gmail app.
https://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java
And so you can do your custom layout of toast, with as much number of lines, as you want, without
even cutting stuff.

TextView setGravity() doesn't work in java

I'm stuck on a problem and I don't know, what causes it.
I have a very simple Layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="NOTE_THIS"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:text="TestText"/>
</LinearLayout>
which is included inside another layout. If I change the gravity inside the xml I see same Result in Layout-Editor and on my phone. If I wanna apply the Gravity programatically like with myTextView.setGravity(Gravity.CENTER) it doesn't Change anything. And I cannot set LayoutGravity in Java on a TextView
I'll tried for debug purposes to include them three times each with another gravity which does work even. So I assume everything is alright with my Layout and there has to be a Bug or something else I miss.
Can someone give me a hint what I also can try, or what causes this problem?
Set your TextView's width as android:layout_width="fill_parent" then you can set it programmatically using myTextView.setGravity(Gravity.CENTER)
You need to set the gravity of the LayoutParams object instead of the View itself:
TextView tv = new TextView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);
When you use LinearLayout as parent, then layout_gravity comes in picture which align control but not content inside the control so,
Instead using android:layout_gravity use android:gravity.

Categories