Android Subclass Listview problem - java

I have a subclass of ListView. My only problem is I can't get it to work (not the listview code, but the code to use the listview. Android won't inflate it (app crashes). I'm sure there is a simple way to get this to work, but I don't know how.
PullToRefreshListView list;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cliplistmain);
context = this;
list = (PullToRefreshListView)findViewById(R.id.clipListMain);
list.setOnItemClickListener(this);
list.setOnScrollListener(this);
list.setOnRefreshListener(new OnRefreshListener(){
#Override
public void onRefresh()
{
ClipStore.getInstance().getClips(SugarLoafContext.currentCamera);
}
});
Button btn = (Button)findViewById(R.id.findclipsbtn);
btn.setOnClickListener(this);
SugarLoafContext.lastView = SugarLoafContext.LAST_VIEW_CLIP_LIST;
}
Here is my xml:
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/clipListMain" >

This is only a part of your layout xml file right, so you do have header and namespace information in your XML.
I guess you get a ClassCastException or similar right? Since you've got a ListView in your xml but cast it into your PullToRefreshListView class.
Your layout xml (i.e. ./res/layout/foo.xml) should be something like
<?xml version="1.0" encoding="utf-8"?>
<com.yourpackage.foo.PullToRefreshListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.yourpackage.foo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/clipListMain" />

IMO you forget about adding this subclass name to xml config

Related

Add an ImageView statically in Android Studio

I want to change the resource of an image view from a variable in android studio.
Let me explain you, i have first activity "selector activity" which has 21 images, on every image click, a variable god_name is changed to the particular god, now i am sending that variable using putExtra, now i want that to change the resource of an ImageView in MainActivity depending on the variable, for example if the variable is "two", then i want to change the resource to "R.drawable.two".
Something like that, I have some experience in python so I used f strings there, here can I do something?
MainActivity.class
(Note-I have not Included imports so the code is allowed to be uploaded.)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
god.setImageResource();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".MainActivity">
<ImageView
android:id="#+id/god"
android:layout_width="413dp"
android:layout_height="496dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/hanuman" />
</androidx.constraintlayout.widget.ConstraintLayout>
I can use multiple if conditions and switch case statement but that would be so much large as I would have to write it 21 times so it is very difficult!
Thanks in advance for anyone kind of help!
That sounds like you need to use the resources.getIdentifier() to make this work.
I wrote once an answer to a related topic: https://stackoverflow.com/a/4865350/180538
Anyway what you need is to pass the name of the resource and then resolve the identifier to use it. So something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
// so for R.mipmap.two you need to pass "two" as "chosen_god" extra
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
god.setImageResource(resId);
}

How to change the gravity of a TextView in another xml file from Main Activity?

I have an XML file that has a TextView, and I want to change its gravity from the main activity.
I used this code in the main activity:
// I just coppied the related codes
TextView tvValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvValue = findViewById(R.id.tv_value);
// the tv_value id is from the (word_item.xml) file
}
private void OnClickFunc(View view) {
tvValue.setGravity(Gravity.RIGHT);
}
and the problem is that I get a NullPointerException error on a null object reference.
I know that I should somehow connect the xml file with the main activity.
word_item.xml file:
<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word" />
</LinearLayout>
Thank you and I appreciate your help.
if you havent included the word_item.xml in your activity_main.xml then the error is obvious
so this is how you can include another file in any xml file in android
<include layout="#layout/word_item.xml" />
then things ll work as you expect
Try to first inflate the other layout as follows the initialize the view
LayoutInflater inflater = LayoutInflater.from(MainActivity.this); final View v = inflater.inflate(R.layout.word_item, null);
Then
Textview tvValue = findViewById(R.id.tv_value); private void OnClickFunc() { tvValue.setGravity(Gravity.RIGHT);
Where did you initialize textView variable?? Variable must be initialize after setContentView in OnCreate method in Activity.

How to use a ScrollView?

This is suppose to be a scroll view with all the content added from the Java code when response is received from the API.
The problem is that I can't find a way to display the information like this in a ScrollView. I tried using an ImageButton but I couldn't get the content in it then I tried using a Button but still couldn't achieve the desired effect please can someone suggest a way I could do this.
private Button makeButton(String targetName, final String i, LinearLayout.LayoutParams buttonLayoutParams) {
Button in = new Button(this);
in.setBackground(getResources().getDrawable(R.drawable.rectangle14));
in.setText(targetName);
in.setWidth(360);
in.setHeight(72);
in.setLayoutParams(buttonLayoutParams);
in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(HomeActivity.this,XSavingDetailsActivity.class);
myIntent.putExtra("i" ,i);
HomeActivity.this.startActivity(myIntent);
}
});
return in;
}
You should use a RecyclerView .Each and every component within the RecyclerView is a CardView . Also you should learn about Material Design.
Apart from the above some useful links:
https://developer.android.com/guide/topics/ui/layout/cardview.html
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
https://medium.com/#nileshsingh/android-cardview-101-everything-you-should-know-5bbf1c873f5a
Just make the top-level layout a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<!-- everything else -->
</TableLayout>

Get all text from online text file and show it all in a TextView

I want to get all the text from a webpage and save it to a String array/list and then print it in a View/Activity..
Let's say I have URL www.my.url/html.php and on that page is this:
Paul
Caroline
Steve'
...
...
And now I want them to be written in the same position in an View/Activity.
I have this simple Activity:
public class PlayerListActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playerlist);
}
}
And this XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#121E26"
tools:context="com.tominocz.stonequestapp.MainActivity"
tools:ignore="MergeRootFrame,ContentDescription,HardcodedText,SpUsage,TextViewEdits,Deprecated,SmallSp" >
</FrameLayout>
This is what I want it to look like. Do you think this is possible?
if it is simple html data you may download whole HTML. also put TextView inside FrameLayout and use
textView.setText(Html.fromHtml(stringWithHtml));
doc of method here
note that not all HTML tags are supported, unofficial list HERE

android setContentView not working

Not a duplicate: my question is simpler than all of the others.
I've been trying to follow the android hello world tutorial, and I can't get the very first example to work.
This is my code:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
As you can see, I copied and pasted directly out of the tutorial.
The problem is, that instead of displaying Hello, Android, it displays whatever is in the layout/main.xml file. If that file doesn't exist, it closes without displaying anything.
WHY IS THIS NOT WORKING?
As I've copied this directly from the official docs, I have no idea where to even start trying to debug it. Any pointers or suggestions you can give will be greatly appreciated!
Edit: posting my main.xml as requested
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloAndroid"
/>
</LinearLayout>
Note that this was created automatically when I started the project, I didn't put it there.
Why do you have two TextViews with the same text? You shouldn't be doing this, if you're only going to use one, then only use one.
You XML is fine as is, but your Activity code needs to change:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Heres an explanation of what you were doing...
You were building a TextView dynamically and setting the ContentView to that TextView is fine, but it was not your original intention. Your original intention or the original intention of the sample was to use the layout file and then set the contentview of that activity to that layout file, not just that textview.
Update
In lieu of what the OP said, he does want to build a TextView instance dynamically and apply it to the screen. If this is the case then you have to...:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
tv.setText("Hello, Android");
setContentView(tv);
}
}
The only difference here is that the TextView has properties that are defining how it will appear on the screen. You can either user FILL_PARENT or WRAP_CONTENT.
you haven't set the xml file or layout in your application. You are directly calling the view.You need to call your view under layout.
Syntax: setContentView(R.layout.your xml file name);
Regarding the question about logcat, you'll find it in the Debug perspective in Eclipse. It's the log of error messages from system and user code.
Try this: You don't need to do setContentView at end. If you do that, it will override with what you have in XML.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxmlfile);
TextView tv = (TextView)findViewById(R.id.helloText);
tv.setText("Hello, Android");
}
<TextView
android:id="#+id/helloText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloAndroid" />

Categories

Resources